Debian Codegraph @

R/shinyui.R

raw
1 #' @include globals.R
2 NULL
3
4 #' Load the MathJax library and typeset math expressions
5 #'
6 #' This function adds MathJax to the page and typeset the math expressions (if
7 #' found) in the content `...`. It only needs to be called once in an app
8 #' unless the content is rendered *after* the page is loaded, e.g. via
9 #' [renderUI()], in which case we have to call it explicitly every
10 #' time we write math expressions to the output.
11 #' @param ... any HTML elements to apply MathJax to
12 #' @export
13 #' @examples withMathJax(helpText("Some math here $$\\alpha+\\beta$$"))
14 #' # now we can just write "static" content without withMathJax()
15 #' div("more math here $$\\sqrt{2}$$")
16 withMathJax <- function(...) {
17 path <- paste0(
18 getOption("shiny.mathjax.url", "https://mathjax.rstudio.com/latest/MathJax.js"),
19 "?",
20 getOption("shiny.mathjax.config", "config=TeX-AMS-MML_HTMLorMML")
21 )
22 tagList(
23 tags$head(
24 singleton(tags$script(src = path, type = 'text/javascript'))
25 ),
26 ...,
27 tags$script(HTML('if (window.MathJax) MathJax.Hub.Queue(["Typeset", MathJax.Hub]);'))
28 )
29 }
30
31 renderPage <- function(ui, showcase=0, testMode=FALSE) {
32 lang <- getLang(ui)
33
34 # If the ui is a NOT complete document (created by htmlTemplate()), then do some
35 # preprocessing and make sure it's a complete document.
36 if (!inherits(ui, "html_document")) {
37 if (showcase > 0)
38 ui <- showcaseUI(ui)
39
40 # Wrap ui in body tag if it doesn't already have a single top-level body tag.
41 if (!(inherits(ui, "shiny.tag") && ui$name == "body"))
42 ui <- tags$body(ui)
43
44 # Put the body into the default template
45 ui <- htmlTemplate(
46 system_file("template", "default.html", package = "shiny"),
47 lang = lang,
48 body = ui,
49 # this template is a complete HTML document
50 document_ = TRUE
51 )
52 }
53
54 shiny_deps <- c(
55 list(jqueryDependency()),
56 shinyDependencies()
57 )
58
59 if (testMode) {
60 # Add code injection listener if in test mode
61 shiny_deps[[length(shiny_deps) + 1]] <-
62 htmlDependency(
63 "shiny-testmode",
64 get_package_version("shiny"),
65 src = "www/shared",
66 package = "shiny",
67 script = "shiny-testmode.js",
68 all_files = FALSE
69 )
70 }
71
72 if (in_devmode() || in_client_devmode()) {
73 # If we're in dev mode, add a simple script to the head that injects a
74 # global variable for the client to use to detect dev mode.
75 shiny_deps[[length(shiny_deps) + 1]] <-
76 htmlDependency(
77 "shiny-devmode",
78 get_package_version("shiny"),
79 src = "www/shared",
80 package = "shiny",
81 head="<script>window.__SHINY_DEV_MODE__ = true;</script>",
82 all_files = FALSE
83 )
84 }
85
86
87 html <- renderDocument(ui, shiny_deps, processDep = createWebDependency)
88 enc2utf8(paste(collapse = "\n", html))
89 }
90
91 jqueryDependency <- function() {
92 version <- getOption("shiny.jquery.version", 3)
93 if (version == 3) {
94 return(htmlDependency(
95 "jquery", version_jquery,
96 src = "www/shared",
97 package = "shiny",
98 script = "jquery.min.js",
99 all_files = FALSE
100 ))
101 }
102 if (version == 1) {
103 return(htmlDependency(
104 "jquery", "1.12.4",
105 src = "www/shared/legacy",
106 package = "shiny",
107 script = "jquery.min.js",
108 all_files = FALSE
109 ))
110 }
111 stop("Unsupported version of jQuery: ", version)
112 }
113
114 shinyDependencies <- function() {
115 list(
116 bslib::bs_dependency_defer(shinyDependencyCSS),
117 busyIndicatorDependency(),
118 htmlDependency(
119 name = "shiny-javascript",
120 version = get_package_version("shiny"),
121 src = "www/shared",
122 package = "shiny",
123 script =
124 if (isTRUE(
125 get_devmode_option(
126 "shiny.minified",
127 TRUE
128 )
129 ))
130 "shiny.min.js"
131 else
132 "shiny.js",
133 all_files = FALSE
134 )
135 )
136 }
137
138 shinyDependencySass <- function(bs_version) {
139 bootstrap_scss <- paste0("shiny.bootstrap", bs_version, ".scss")
140
141 scss_home <- system_file("www/shared/shiny_scss", package = "shiny")
142 scss_files <- file.path(scss_home, c(bootstrap_scss, "shiny.scss"))
143 lapply(scss_files, sass::sass_file)
144 }
145
146 shinyDependencyCSS <- function(theme) {
147 version <- get_package_version("shiny")
148
149 if (!is_bs_theme(theme)) {
150 return(htmlDependency(
151 name = "shiny-css",
152 version = version,
153 src = "www/shared",
154 package = "shiny",
155 stylesheet = "shiny.min.css",
156 all_files = FALSE
157 ))
158 }
159
160 bs_version <- bslib::theme_version(theme)
161
162 bslib::bs_dependency(
163 input = shinyDependencySass(bs_version),
164 theme = theme,
165 name = "shiny-sass",
166 version = version,
167 cache_key_extra = version
168 )
169 }
170
171 #' Create a Shiny UI handler
172 #'
173 #' @description `r lifecycle::badge("superseded")`
174 #'
175 #' @description Historically this function was used in ui.R files to register a user
176 #' interface with Shiny. It is no longer required as of Shiny 0.10; simply
177 #' ensure that the last expression to be returned from ui.R is a user interface.
178 #' This function is kept for backwards compatibility with older applications. It
179 #' returns the value that is passed to it.
180 #'
181 #' @param ui A user interface definition
182 #' @return The user interface definition, without modifications or side effects.
183 #' @keywords internal
184 #' @export
185 shinyUI <- function(ui) {
186 if (in_devmode()) {
187 shinyDeprecated(
188 "0.10.0", "shinyUI()",
189 details = paste0(
190 "When removing `shinyUI()`, ",
191 "ensure that the last expression returned from ui.R is a user interface ",
192 "normally supplied to `shinyUI(ui)`."
193 )
194 )
195 }
196
197 .globals$ui <- list(ui)
198 ui
199 }
200
201 uiHttpHandler <- function(ui, uiPattern = "^/$") {
202
203 force(ui)
204
205 allowed_methods <- "GET"
206 if (is.function(ui)) {
207 allowed_methods <- attr(ui, "http_methods_supported", exact = TRUE) %||% allowed_methods
208 }
209
210 function(req) {
211 if (!isTRUE(req$REQUEST_METHOD %in% allowed_methods))
212 return(NULL)
213
214 if (!isTRUE(grepl(uiPattern, req$PATH_INFO)))
215 return(NULL)
216
217 showcaseMode <- .globals$showcaseDefault
218 if (.globals$showcaseOverride) {
219 mode <- showcaseModeOfReq(req)
220 if (!is.null(mode))
221 showcaseMode <- mode
222 }
223
224 testMode <- getShinyOption("testmode", default = FALSE)
225
226 # Create a restore context using query string
227 bookmarkStore <- getShinyOption("bookmarkStore", default = "disable")
228 if (bookmarkStore == "disable") {
229 # If bookmarking is disabled, use empty context
230 restoreContext <- RestoreContext$new()
231 } else {
232 restoreContext <- RestoreContext$new(req$QUERY_STRING)
233 }
234
235 withRestoreContext(restoreContext, {
236 uiValue <- NULL
237
238 if (is.function(ui)) {
239 if (length(formals(ui)) > 0) {
240 # No corresponding ..stacktraceoff.., this is pure user code
241 uiValue <- ..stacktraceon..(ui(req))
242 } else {
243 # No corresponding ..stacktraceoff.., this is pure user code
244 uiValue <- ..stacktraceon..(ui())
245 }
246 } else {
247 if (getCurrentRestoreContext()$active) {
248 warning("Trying to restore saved app state, but UI code must be a function for this to work! See ?enableBookmarking")
249 }
250 uiValue <- ui
251 }
252 })
253 if (is.null(uiValue))
254 return(NULL)
255
256 if (inherits(uiValue, "httpResponse")) {
257 return(uiValue)
258 } else {
259 html <- renderPage(uiValue, showcaseMode, testMode)
260 return(httpResponse(200, content=html))
261 }
262 }
263 }