/
rsc_filter.R
105 lines (97 loc) · 3.08 KB
/
rsc_filter.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
#' Filter widget
#'
#' Filter content rows with owner, content type and tags,
#' expects the exact same frame passed to the view widget being filtered.
#'
#' @param content A data frame from Connect's content. Although optional,
#' expects an "owner_username", "app_mode" and "tags" columns.
#'
#' @export
rsc_filter <- function(content) {
if (missing(content)) {
stop(
"rsc_filter() expects a data frame or a crosstalk shared data object."
)
}
if (crosstalk::is.SharedData(content)) {
key <- content$key()
group <- content$groupName()
content <- content$origData()
} else {
if (!is.data.frame(content)) {
stop(
"rsc_filter() expects a data frame or a crosstalk shared data object."
)
}
ctalk_group <- digest::digest(toString(content), "md5")
shared <- crosstalk::SharedData$new(content, group = ctalk_group)
key <- shared$key()
group <- shared$groupName()
content <- shared$origData()
}
cols <- colnames(content)
warning_widget_input(
"rsc_filter()",
cols,
c("owner_username", "app_mode", "tags")
)
warning_large_content(content)
component <- reactR::component(
"Filter",
list(
data = content,
crosstalkKey = key,
crosstalkGroup = group
)
)
dependencies <- crosstalk::crosstalkLibs()
dependencies[["rsc_filter-theme"]] <- resolve_theme_dependency("rsc_filter")
# create widget
htmlwidgets::createWidget(
name = "rsc_filter",
reactR::reactMarkup(component),
width = "auto",
height = "auto",
package = "connectwidgets",
dependencies = dependencies
)
}
# nolint start
#' Called by HTMLWidgets to produce the widget's root element.
#' @noRd
widget_html.rsc_filter <- function(id, style, class, ...) {
htmltools::tags$div(
id = id, class = class, style = style,
# Necessary for RStudio viewer version < 1.2
reactR::html_dependency_corejs(),
reactR::html_dependency_react(),
reactR::html_dependency_reacttools()
)
}
#' Shiny bindings for rsc_filter
#'
#' Output and render functions for using rsc_filter 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 rsc_filter
#' @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 rsc_filter-shiny
#'
#' @export
rscfilterOutput <- function(outputId, width = '100%', height = '400px'){
htmlwidgets::shinyWidgetOutput(outputId, 'rsc_filter', width, height, package = 'connectwidgets')
}
#' @rdname rsc_filter-shiny
#' @export
renderRscfilter <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
htmlwidgets::shinyRenderWidget(expr, rscfilterOutput, env, quoted = TRUE)
}
# nolint end