Skip to content

Commit a045467

Browse files
committed
simplify reset implementation
1 parent 8d5153c commit a045467

1 file changed

Lines changed: 41 additions & 49 deletions

File tree

R/reset.R

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -98,70 +98,58 @@ reset <- function(id = "", asis = FALSE) {
9898
type <- messages[[x]][['type']]
9999
value <- messages[[x]][['value']]
100100

101-
# checkbox values need to be manually converted to TRUE/FALSE
101+
# list of inputs that can have multiple values and need the value parsed from a
102+
# comma-separated list to a vector
103+
inputsStrToVec <- c("Slider", "SliderDate", "SliderDateTime", "DateRange", "NoUiSlider", "NumericRange", "SliderText", "SlimSelect", "VirtualSelect")
104+
105+
if (type %in% inputsStrToVec) {
106+
value <- strToVec(value)
107+
}
108+
109+
# list of inputs that use a 'selected' instead of 'value' argument
110+
inputsParamSelected <- c("RadioButtons", "CheckboxGroup", "Select", "RadioGroupButtons", "SliderText", "SlimSelect", "Spectrum", "VirtualSelect")
111+
# list of inputs that don't use 'selected' or 'value' arguments
112+
inputsParamOther <- c("DateRange")
113+
114+
# native shiny inputs
102115
if (type == "Checkbox") {
103116
value <- as.logical(value)
104-
}
105-
if (type == "Date") {
117+
} else if (type == "Date") {
106118
if (value == "NA") {
107119
value <- NA
108120
}
109-
}
110-
111-
# most input update functions use 'value' argument, but some require special handling
112-
113-
if (type == "RadioButtons") {
121+
} else if (type == "RadioButtons" || type == "RadioGroupButtons") {
114122
if (is.null(value) && utils::packageVersion("shiny") > "1.5.0") {
115123
value <- character(0)
116124
}
117-
funcParams[['selected']] <- value
118125
} else if (type == "CheckboxGroup" || type == "Select") {
119126
if (value == '""') {
120-
funcParams[['selected']] <- ""
127+
value <- ""
121128
} else {
122-
funcParams[['selected']] <- jsonlite::fromJSON(value)
129+
value <- jsonlite::fromJSON(value)
123130
}
124-
} else if (type == "Slider") {
125-
funcParams[['value']] <- strToVec(value)
126131
} else if (type == "SliderDate") {
127132
type <- "Slider"
128-
value <- strToVec(value)
129-
funcParams[['value']] <- as.Date(as.POSIXct(as.numeric(value) / 1000, origin = "1970-01-01"))
133+
value <- as.Date(as.POSIXct(as.numeric(value) / 1000, origin = "1970-01-01"))
130134
} else if (type == "SliderDateTime") {
131135
type <- "Slider"
132-
value <- strToVec(value)
133-
funcParams[['value']] <- as.POSIXct(as.numeric(value) / 1000, origin = "1970-01-01")
136+
value <- as.POSIXct(as.numeric(value) / 1000, origin = "1970-01-01")
134137
} else if (type == "DateRange") {
135-
dates <- strToVec(value)
136-
dates[dates == "NA"] <- NA
137-
funcParams[['start']] <- dates[1]
138-
funcParams[['end']] <- dates[2]
138+
value[value == "NA"] <- NA
139+
funcParams[['start']] <- value[1]
140+
funcParams[['end']] <- value[2]
139141
}
140142

141143
# {shinyWidgets} inputs
142144
else if (type == "CalendarPro") {
143-
funcParams[['value']] <- strToVec(value, " ")
144-
} else if (type == "NoUiSlider") {
145-
funcParams[['value']] <- strToVec(value)
146-
} else if (type == "NumericRange") {
147-
funcParams[['value']] <- strToVec(value)
148-
} else if (type == "RadioGroupButtons") {
149-
funcParams[['selected']] <- value
150-
} else if (type == "SliderText") {
151-
funcParams[['selected']] <- strToVec(value)
152-
} else if (type == "SlimSelect") {
153-
funcParams[['selected']] <- strToVec(value)
154-
} else if (type == "Spectrum") {
155-
funcParams[['selected']] <- value
156-
} else if (type == "VirtualSelect") {
157-
if (is.null(value)) {
158-
funcParams[['selected']] <- ""
159-
} else {
160-
funcParams[['selected']] <- strToVec(value)
161-
}
145+
value <- strToVec(value, " ")
162146
}
163147

164-
else {
148+
if (type %in% inputsParamSelected) {
149+
funcParams[['selected']] <- value
150+
} else if (type %in% inputsParamOther) {
151+
# assume the correct argument was already added above
152+
} else {
165153
funcParams[['value']] <- value
166154
}
167155

@@ -177,30 +165,34 @@ reset <- function(id = "", asis = FALSE) {
177165
}
178166

179167
strToVec <- function(str, sep = ",") {
168+
if (is.null(str) || str == "") {
169+
return("")
170+
}
180171
unlist(strsplit(str, sep))
181172
}
182173

183174
getUpdateFunc <- function(type) {
184175

185-
# get the name of the function
176+
# list of inputs whose update function doesn't include the word "Input" at the end
186177
inputsShortUpdateName <- c("RadioButtons", "CalendarPro", "ColorPickr", "RadioGroupButtons", "SlimSelect", "VirtualSelect")
178+
187179
if (type %in% inputsShortUpdateName) {
188-
updateFunc <- sprintf("update%s", type)
180+
updateFuncName <- sprintf("update%s", type)
189181
} else {
190-
updateFunc <- sprintf("update%sInput", type)
182+
updateFuncName <- sprintf("update%sInput", type)
191183
}
192184

193-
# get the package it's from
194-
pkg <- ""
185+
# list of inputs whose update function is taken from {shinyWidgets}
195186
shinyWidgetsInputs <- c("CalendarPro", "ColorPickr", "Knob", "NoUiSlider", "NumericRange", "RadioGroupButtons", "SliderText", "SlimSelect", "Spectrum", "Time", "VirtualSelect")
187+
196188
if (type == "Colour") {
197189
pkg <- "colourpicker"
198190
} else if (type %in% shinyWidgetsInputs) {
199191
pkg <- "shinyWidgets"
192+
} else {
193+
pkg <- "shiny"
200194
}
201-
if (pkg != "") {
202-
updateFunc <- utils::getFromNamespace(updateFunc, pkg)
203-
}
195+
updateFunc <- utils::getFromNamespace(updateFuncName, pkg)
204196

205197
updateFunc
206198
}

0 commit comments

Comments
 (0)