-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathinput-drop.R
234 lines (230 loc) · 7.16 KB
/
input-drop.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
#' @title Dropdown Input
#'
#' @description A dropdown menu for selecting a value.
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param choicesNames A \code{tagList} of HTML tags to show in the dropdown menu.
#' @param choicesValues Vector corresponding to \code{choicesNames}
#' for retrieving values server-side.
#' @param selected The initial selected value, must be an element of \code{choicesValues},
#' default to the first item of \code{choicesValues}.
#' @param dropUp Open the menu above the button rather than below.
#' @param dropWidth Width of the dropdown menu.
#' @param dropMaxHeight Maximal height for the menu.
#' @param dropPreScrollable Force scroll bar to appear in the menu.
#' @param btnClass Class for buttons in dropdown menu, default is \code{"btn-link"},
#' you can use for example \code{"btn-default"} to display regular buttons.
#' @param width The width of the input.
#'
#' @export
#'
#' @importFrom htmltools validateCssUnit tags tagList singleton css
#'
#' @seealso \code{\link{updateDropInput}}
#'
#' @examples
#' if (interactive()) {
#'
#' library(shiny)
#' library(esquisse)
#'
#' ui <- fluidPage(
#' tags$h2("Drop Input"),
#' dropInput(
#' inputId = "mydrop",
#' choicesNames = tagList(
#' list(icon("home"), style = "width: 100px;"),
#' list(icon("flash"), style = "width: 100px;"),
#' list(icon("cogs"), style = "width: 100px;"),
#' list(icon("fire"), style = "width: 100px;"),
#' list(icon("users"), style = "width: 100px;"),
#' list(icon("info"), style = "width: 100px;")
#' ),
#' choicesValues = c("home", "flash", "cogs",
#' "fire", "users", "info"),
#' dropWidth = "220px"
#' ),
#' verbatimTextOutput(outputId = "res")
#' )
#'
#' server <- function(input, output, session) {
#' output$res <- renderPrint({
#' input$mydrop
#' })
#' }
#'
#' shinyApp(ui, server)
#'
#' }
dropInput <- function(inputId,
choicesNames,
choicesValues,
selected = NULL,
dropUp = FALSE,
dropWidth = NULL,
dropMaxHeight = NULL,
dropPreScrollable = FALSE,
btnClass = "btn-link",
width = NULL) {
if (length(choicesValues) != length(choicesNames))
stop("dropInput: 'choicesNames' and 'choicesValues' must have same length!", call. = FALSE)
if (is.null(selected))
selected <- choicesValues[1]
if (!selected %in% choicesValues)
stop("dropInput: 'selected' must be an element of 'choicesValues'", call. = FALSE)
if (!inherits(choicesNames, "list"))
stop("dropInput: 'choicesNames' must be a list or a tagList", call. = FALSE)
tagSelected <- choicesNames[[which(choicesValues == selected)]]
btn <- do.call(
what = tags$button,
args = c(tagSelected, list(
style = if (!is.null(width))
paste0("width: ", validateCssUnit(width), ";"),
class = "btn btn-default btn-outline-secondary text-dark drop-input-main dropdown-toggle",
style = "font-size: .75rem;",
`data-toggle` = "dropdown",
`data-bs-toggle` = "dropdown",
`data-value` = selected
))
)
dropTag <- tags$div(
class = "dropdown-menu",
class = if (isTRUE(dropPreScrollable)) "pre-scrollable",
style = css(
padding = "5px",
maxHeight = validateCssUnit(dropMaxHeight),
width = validateCssUnit(dropWidth)
),
lapply(
X = seq_along(choicesNames),
FUN = function(i) {
do.call(
what = tags$button,
args = c(choicesNames[[i]], list(
class = "btn drop-input-button",
class = btnClass,
style = "text-decoration: none !important;",
`data-value` = choicesValues[i]
))
)
}
)
)
tagList(
singleton(
tags$head(
tags$script(src = "esquisse/drop/dropInput-bindings.js")
)
),
tags$div(
id = inputId,
class = "drop-input",
class = ifelse(dropUp, "dropup", "dropdown"),
btn, dropTag
)
)
}
#' Change the value of a drop input on the client
#'
#' @param session The \code{session} object passed to function given to \code{shinyServer}.
#' @param inputId The id of the input object.
#' @param selected The initially selected value.
#' @param disabled Choices (\code{choicesValues}) to disable.
#'
#' @export
#'
#' @seealso \code{\link{dropInput}}
#'
#' @examples
#' if (interactive()) {
#'
#' library(shiny)
#' library(esquisse)
#'
#' myChoices <- tagList(
#' list(icon("home"), style = "width: 100px;"),
#' list(icon("flash"), style = "width: 100px;"),
#' list(icon("cogs"), style = "width: 100px;"),
#' list(icon("fire"), style = "width: 100px;"),
#' list(icon("users"), style = "width: 100px;"),
#' list(icon("info"), style = "width: 100px;")
#' )
#'
#'
#' ui <- fluidPage(
#' tags$h2("Update Drop Input"),
#' fluidRow(
#' column(
#' width = 6,
#' dropInput(
#' inputId = "mydrop",
#' choicesNames = myChoices,
#' choicesValues = c("home", "flash", "cogs", "fire", "users", "info"),
#' dropWidth = "220px"
#' ),
#' verbatimTextOutput(outputId = "res")
#' ),
#' column(
#' width = 6,
#' actionButton("home", "Select home"),
#' actionButton("flash", "Select flash"),
#' actionButton("cogs", "Select cogs"),
#' actionButton("fire", "Select fire"),
#' actionButton("users", "Select users"),
#' actionButton("info", "Select info"),
#' checkboxGroupInput(
#' inputId = "disabled",
#' label = "Choices to disable",
#' choices = c("home", "flash", "cogs", "fire", "users", "info")
#' ),
#' actionButton("disable", "Disable")
#' )
#' )
#' )
#'
#' server <- function(input, output, session) {
#'
#' output$res <- renderPrint({
#' input$mydrop
#' })
#'
#' observeEvent(input$home, {
#' updateDropInput(session, "mydrop", "home")
#' })
#' observeEvent(input$flash, {
#' updateDropInput(session, "mydrop", "flash")
#' })
#' observeEvent(input$cogs, {
#' updateDropInput(session, "mydrop", "cogs")
#' })
#' observeEvent(input$fire, {
#' updateDropInput(session, "mydrop", "fire")
#' })
#' observeEvent(input$users, {
#' updateDropInput(session, "mydrop", "users")
#' })
#' observeEvent(input$info, {
#' updateDropInput(session, "mydrop", "info")
#' })
#'
#' observeEvent(input$disable, {
#' if (!is.null(input$disabled)) {
#' updateDropInput(session, "mydrop", disabled = input$disabled)
#' } else {
#' updateDropInput(session, "mydrop", disabled = character(0))
#' }
#' })
#' }
#'
#' shinyApp(ui, server)
#'
#' }
updateDropInput <- function(session, inputId, selected = NULL, disabled = NULL) {
if (!is.null(disabled) && length(disabled) == 1)
disabled <- list(disabled)
message <- dropNulls(list(
selected = selected,
disabled = disabled
))
session$sendInputMessage(inputId, message)
}