-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.R
63 lines (51 loc) · 2.86 KB
/
defs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Custom functions for the greenT app
# Confusingly, we have three possible ways to name our color inputs
## `inputIds` is the literal names of the input objects. Have to have the numbers spelled out because you can't have in input called "1" #XXX can you?
inputIds <- c(letters, c("zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"))
## `displayNames` is how I want the labels of the input objects to display: capital letters and digits.
displayNames <- c(LETTERS, 0:9)
## `charactersOut` is how I want the characters to display when they print out to a csv. This isn't critical at all, and maybe I'm being too picky, but I kind of like having uppercase letters for display vs. lowercase letters for collecting data.
charactersOut <- tolower(displayNames)
# Read in my colors, and convert the `character` column from charactersOut (the way it was written out) to `inputIds` (so we can use these colors to set the initial values of the input selectors)
kaijaColors <- read.csv(here("data", "kaijaColors.csv")) %>%
mutate(character = as.character(fct_recode(character,
!!! setNames(charactersOut, inputIds))))
# Function to reorganize the inputs horizontally --------------------------
horiz <- function(vec = inputIds, nrow = 6, ncol = 6){
mat <- matrix(vec, nrow = nrow, ncol = ncol, byrow = T)
horizVec <- c(mat)
return(horizVec)
}
# Function to create inputs -----------------------------------------------
colorInit <- function(x = .x, y = .y){
colourpicker::colourInput(x, y, value = randomColor(), showColour = "background")
}
# Mandatory fields for contribution form ----------------------------------
fieldsMandatory <- c("name", "yesSetColors", "handedness", "gender", "sex", "synesthesia", "consent")
labelMandatory <- function(label) {
tagList(
label,
span("*", class = "mandatory_star")
)
}
fieldsAll <- c("name", "yesSetColors", "email", "birthYear", "handedness", "gender", "genderSelfDescribe", "sex", "strong", "consistent", "synesthesia", "howLong", "family", "synesthesiaTypes", "otherSynesthesia", "comments", "consent")
# dateTimeFormat ----------------------------------------------------------
dateTimeFormat <- function(){
dateTime <- str_replace(str_replace_all(as.character(Sys.time()),
":", "-"),
" ", "_")
return(dateTime)
}
# Function to save form data ----------------------------------------------
saveData <- function(data) {
fileName <- sprintf("%s_%s_%s",
"greenT",
dateTimeFormat(),
digest::digest(data))
# Create an empty spreadsheet
ss <- gs4_create(name = fileName,
sheets = "data")
# Put the data.frame in the spreadsheet and provide the sheet_id so it can be found
sheet_write(data, ss, sheet = "data")
}