forked from walkerke/bsselectR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsselect.R
110 lines (98 loc) · 4.68 KB
/
bsselect.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
#' bsselect
#'
#' Function to generate a drop-down menu with the bootstrap-select jQuery plugin in an R Markdown document
#'
#' @param vector A named vector of values to send to the bootstrap-select dropdown menu.
#' @param selected The selected option; if NULL defaults to the first element of \code{vector}.
#' @param type One of \code{"text"}, \code{"img"}, or \code{"iframe"}.
#' @param frame_height The height of the image or iframe. Defaults to 500px.
#' @param frame_width The width of the image or iframe.
#' @param align_right Whether to align the menu to the right instead of the left. Defaults to FALSE.
#' @param dropup_auto Creates a drop-up menu automatically if there is not enough space to drop down. Defaults to TRUE.
#' @param header Adds a header to the top of the menu.
#' @param live_search When TRUE, adds a search box to the drop-down menu. Defaults to FALSE.
#' @param box_width One of "auto", "fit", a css-width in pixels, or FALSE.
#' @param live_search_style One of "contains" (the default) or "startsWith".
#' @param show_tick Whether to show a check mark next to the selected option. Defaults to FALSE.
#' @param size The number of items to show in the window. Defaults to "auto"; if FALSE, shows every option.
#' @param style Add the value to the button's style. Options include "btn-primary", "btn-info", "btn-success", "btn-warning", and "btn-danger".
#' @param width The width of the htmlwidget.
#' @param height The height of the htmlwidget.
#' @param elementId The element ID.
#'
#' @seealso \url{https://silviomoreto.github.io/bootstrap-select/}
#'
#' @import htmlwidgets htmltools
#'
#' @export
bsselect <- function(vector, selected = NULL,
type = c("text", "img", "iframe"),
frame_height = "500", frame_width = "100%", align_right = FALSE,
dropup_auto = TRUE, header = FALSE, live_search = FALSE, box_width = FALSE,
live_search_style = "contains", show_tick = FALSE, size = "auto",
style = NULL, width = NULL, height = NULL, elementId = NULL, ...) {
# forward options using opts
opts = list(
)
# create widget
widg <- htmlwidgets::createWidget(
name = 'bsselect',
x = opts,
width = width,
height = height,
package = 'bsselectR',
elementId = elementId
)
# Prepend the HTML content to the widget
out <- htmlwidgets::prependContent(widg, buildHTML(choices = vector,
selected = selected,
type = type,
dropdownAlignRight = align_right,
dropupAuto = dropup_auto,
header = header,
liveSearch = live_search,
liveSearchStyle = live_search_style,
showTick = show_tick,
size = size,
style = style,
height = frame_height, width = frame_width
))
out
}
#' Shiny bindings for bsselect
#'
#' Output and render functions for using bsselect within Shiny
#' applications and interactive Rmd documents.
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#' string and have \code{'px'} appended.
#' @param expr An expression that generates a bsselect
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
#' is useful if you want to save an expression in a variable.
#'
#' @name bsselectR-shiny
#'
#' @export
bsselectOutput <- function(outputId, width = '100%', height = '400px'){
htmlwidgets::shinyWidgetOutput(outputId, 'bsselect', width, height, package = 'bsselectR')
}
#' @rdname bsselectR-shiny
#' @export
renderBsselect <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
htmlwidgets::shinyRenderWidget(expr, bsselectOutput, env, quoted = TRUE)
}
#' @keywords internal
bsselect_html <- function(id, style, class, ...){
htmltools::attachDependencies(
htmltools::tagList(
tags$div(id=id, style=style, class=class, ...)
),
value = list(
rmarkdown::html_dependency_jquery(),
rmarkdown::html_dependency_bootstrap("default")
)
)}