/
shiny-inputbindings.R
269 lines (250 loc) · 8.46 KB
/
shiny-inputbindings.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Defines shiny bindings
get_shiny_input_bindings <- function() {
bindings <- get(".shiny_input_bindings")
if(bindings$size() == 0) {
bindings$mset(
'shiny.textInput' = list(
binding = "shiny.textInput",
update_function = "shiny::updateTextInput"
),
'shiny.textAreaInput' = list(
binding = "shiny.textareaInput",
update_function = "shiny::updateTextAreaInput"
),
'shiny.passwordInput' = list(
binding = "shiny.passwordInput",
update_function = "shiny::updateTextInput"
),
'shiny.numericInput' = list(
binding = "shiny.numberInput",
update_function = "shiny::updateNumericInput"
),
'shiny.checkboxInput' = list(
binding = "shiny.checkboxInput",
update_function = "shiny::updateCheckboxInput"
),
'shiny.sliderInput' = list(
binding = "shiny.sliderInput",
update_function = "shiny::updateSliderInput"
),
'shiny.dateInput' = list(
binding = "shiny.dateInput",
update_function = "shiny::updateDateInput"
),
'shiny.dateRangeInput' = list(
binding = "shiny.dateRangeInput",
update_function = 'shiny::updateDateRangeInput'
),
'shiny.selectInput' = list(
binding = "shiny.selectInput",
update_function = 'shiny::updateSelectInput'
),
'shiny.selectizeInput' = list(
binding = "shiny.selectInput",
update_function = 'shiny::updateSelectizeInput'
),
'shiny.varSelectInput' = list(
binding = "shiny.selectInput",
update_function = 'shiny::updateVarSelectInput'
),
'shiny.varSelectizeInput' = list(
binding = "shiny.selectInput",
update_function = 'shiny::updateVarSelectizeInput'
),
'shiny.radioButtons' = list(
binding = "shiny.radioInput",
update_function = 'shiny::updateRadioButtons'
),
'shiny.checkboxGroupInput' = list(
binding = "shiny.checkboxGroupInput",
update_function = 'shiny::updateCheckboxGroupInput'
),
'shiny.actionButton' = list(
binding = "shiny.actionButtonInput",
update_function = 'shiny::updateActionButton'
),
'shiny.actionLink' = list(
binding = "shiny.actionButtonInput",
update_function = 'shiny::updateActionButton'
),
'shiny.fileInput' = list(
binding = "shiny.fileInputBinding",
update_function = NULL
),
'shiny.fileInput' = list(
binding = "shiny.fileInputBinding",
update_function = NULL
),
'shiny.textOutput' = list(
binding = "shiny.textOutput",
update_function = NULL
),
'dipsaus.compoundInput2' = list(
binding = "dipsaus.compoundInput2",
update_function = 'dipsaus.updateCompoundInput2'
),
'dipsaus.actionButtonStyled' = list(
binding = "shiny.actionButtonInput",
update_function = 'dipsaus.updateActionButtonStyled'
)
)
}
bindings
}
#' Register customized input to enable support by compound input
#' @param fname character, function name, such as \code{"textInput"}
#' @param pkg character, package name, like \code{"shiny"}
#' @param shiny_binding character, 'JavaScript' binding name.See examples
#' @param update_function character, update function such as \code{"shiny::textInput"}
#' @param quiet logical, whether to suppress warnings
#' @return a list of binding functions, one is `JavaScript` object key in
#' \code{Shiny.inputBindings}, the other is `shiny` update function in R end.
#' @examples
#'
#' # register shiny textInput
#' registerInputBinding('textInput', 'shiny',
#' 'shiny.textInput', 'shiny::updateTextInput')
#'
#' # Register shiny actionLink
#' # In "Shiny.inputbindings", the binding name is "shiny.actionButtonInput",
#' # Shiny update function is "shiny::updateActionButton"
#' registerInputBinding('actionLink', 'shiny',
#' 'shiny.actionButtonInput', 'shiny::updateActionButton')
#'
#' @export
registerInputBinding <- function(fname, pkg, shiny_binding, update_function = NULL, quiet = FALSE){
ns <- asNamespace(pkg)
if( !is.function(ns[[fname]]) ){
if(!quiet){
stop(sprintf('%s::%s is not a function', pkg, fname))
}
return(invisible())
}
binding <- list(
binding = shiny_binding,
update_function = update_function
)
# shiny_input_bindings[[sprintf('%s.%s', pkg, fname)]] <- binding
bindings <- get_shiny_input_bindings()
bindings$set(key = sprintf('%s.%s', pkg, fname), value = binding)
invisible(binding)
}
#' Obtain registered input bindings
#' @param fname input function name, character or quoted expression
#' such as \code{'shiny::textInput'} or \code{numericInput}.
#' @param pkg (optional), name of package
#' @param envir environment to evaluate \code{fname} if \code{pkg} is not provided
#'
#' @return a list containing: 1. `JavaScript` input binding name; 2. `R` updating function name
#'
#' @examples
#'
#' library(dipsaus)
#'
#' # Most recommended usage
#' getInputBinding('compoundInput2', pkg = 'dipsaus')
#'
#' # Other usages
#' getInputBinding('shiny::textInput')
#'
#'
#' getInputBinding(shiny::textInput)
#'
#' getInputBinding(compoundInput2, pkg = 'dipsaus')
#'
#' # Bad usage, raise errors in some cases
#' \dontrun{
#' ## You need to library(shiny), or set envir=asNamespace('shiny'), or pkg='shiny'
#' getInputBinding('textInput')
#' getInputBinding(textInput) # also fails
#'
#' ## Always fails
#' getInputBinding('dipsaus::compoundInput2', pkg = 'dipsaus')
#' }
#'
#' @export
getInputBinding <- function(fname, pkg = NULL, envir = parent.frame()){
if( length(pkg) != 1 || !is.character(pkg) ){
# need to get package from fname
if(is.character(fname)){
fname <- str2lang(fname)
}
fname_quoted <- substitute(fname)
if( !is.language(fname) ){
fname <- fname_quoted
}
# now we have quoted fname
if(is.call(fname) && all(as.character(fname[[1]]) %in% c('::', ':::'))){
pkg <- deparse(fname[[2]])
fname <- deparse(fname[[3]])
}else{
f <- eval(fname, envir = envir)
fenv <- environment(f)
if( isNamespace(fenv) ){
pkg <- fenv$.__NAMESPACE__.$spec[['name']]
}
fname <- deparse(fname)
}
}else{
fname_quoted <- substitute(fname)
if(!is.character(fname)){
fname <- deparse(fname_quoted)
}
}
# Check whether fname exists
if(is.null(pkg)){
cat2(sprintf('Cannot find function %s in any package loaded from envir. Please provide package name', fname), level = 'FATAL')
}
ns <- asNamespace(pkg)
if(!is.function(ns[[fname]])){
cat2(sprintf('Cannot find function %s in namespace %s', fname, pkg), level = 'FATAL')
}
binding_key <- sprintf('%s.%s', pkg, fname)
# binding_re <- shiny_input_bindings[[ binding_key ]]
bindings <- get_shiny_input_bindings()
binding_re <- bindings$get(key = binding_key, missing = NULL)
if(is.null(binding_re)){
cat2(sprintf('Cannot find input binding for %s. Please use\n\tdipsaus::registerInputBinding("%s", "%s", shiny_binding = "<JavaScript_binding>", update_function = "<R update function or NULL>")\n to register this input type.', binding_key, fname, pkg), level = 'FATAL')
}
binding_re$call_function <- sprintf('%s::%s', pkg, fname)
binding_re
}
#' Detect whether 'Shiny' is running
#' @return logical, true if current shiny context is active
#' @export
shiny_is_running <- function(){
if(requireNamespace('shiny', quietly = TRUE)){
return(isTRUE(!is.null(shiny::getDefaultReactiveDomain())))
}
return(FALSE)
}
#' Store/Get key-value pairs in 'shiny' session
#' @description If key is missing, it'll be created, otherwise ignored or
#' overwritten.
#' @param session 'Shiny' session
#' @param key character, key to store
#' @param val value to store
#' @param override if key exists, whether to overwrite its value
#' @return If session is shiny session, returns current value stored in
#' session, otherwise returns \code{NULL}
#' @export
add_to_session <- function(
session, key = 'rave_id',
val = paste(sample(c(letters, LETTERS, 0:9), 20), collapse = ''),
override = FALSE
){
if(missing(session)){
if(requireNamespace('shiny', quietly = TRUE)){
session <- shiny::getDefaultReactiveDomain()
} else {
stop('Please specify session')
}
}
if(!is.null(session)){
if(override || !exists(key, envir = session$userData)){
assign(key, val, envir = session$userData)
}
return(get(key, envir = session$userData))
}
return(NULL)
}