Compare commits

..

2 Commits

Author SHA1 Message Date
Barret Schloerke
936f985273 Merge branch 'master' into ts_unit
* master:
  Move `./srcts` configs to top level to support types installation from GitHub (#3425)
  insertTab() now handles position correctly when target is NULL (#3404)
  yarn add node-gyp; yarn build (#3424)
  Export TypeScript type definitions to local folder (#3418)
  TypeScript: Remove `any` types / improve type definitions (#3414)
  Better color constrasting in sliderInput() (#3366)
  Use ggplot2::get_alt_text() if available to provide better default alt text (#3398)
  Set selectize dropdownParent to "body" to prevent clipping
2021-06-15 14:21:30 -04:00
Barret Schloerke
b0cacdd2e9 Add mergeSort test 2021-06-10 14:58:46 -04:00
281 changed files with 3509 additions and 5079 deletions

View File

@@ -20,7 +20,6 @@ plugins:
- '@typescript-eslint'
- prettier
- jest-dom
- unicorn
rules:
"@typescript-eslint/explicit-function-return-type":
- off
@@ -28,7 +27,8 @@ rules:
- off
"@typescript-eslint/explicit-module-boundary-types":
- error
camelcase:
- error
default-case:
- error
indent:
@@ -51,58 +51,3 @@ rules:
dot-location:
- error
- property
camelcase:
# - error
- "off"
unicorn/filename-case:
- error
- case: camelCase
"@typescript-eslint/array-type":
- error
- default: array-simple
readonly: array-simple
"@typescript-eslint/consistent-indexed-object-style":
- error
- index-signature
"@typescript-eslint/sort-type-union-intersection-members":
- error
"@typescript-eslint/consistent-type-imports":
- error
"@typescript-eslint/naming-convention":
- error
- selector: default
format: [camelCase]
- selector: method
modifiers: [private]
format: [camelCase]
leadingUnderscore: require
- selector: method
modifiers: [protected]
format: [camelCase]
leadingUnderscore: require
- selector: variable
format: [camelCase]
trailingUnderscore: forbid
leadingUnderscore: forbid
- selector: parameter
format: [camelCase]
trailingUnderscore: allow
leadingUnderscore: forbid
- selector: [enum, enumMember]
format: [PascalCase]
- selector: typeLike
format: [PascalCase]
custom:
regex: "(t|T)ype$"
match: false

2
.gitattributes vendored
View File

@@ -1,6 +1,4 @@
/NEWS merge=union
/inst/www/shared/shiny.js -merge -diff
/inst/www/shared/shiny-*.js -merge -diff
/inst/www/shared/shiny*.css -merge -diff
*.min.js -merge -diff
*.js.map -merge -diff

View File

@@ -71,12 +71,13 @@ jobs:
shell: Rscript {0}
run: |
pak::local_install_dev_deps(upgrade = TRUE)
pak::pkg_install("sessioninfo")
pak::pkg_install("devtools")
- name: Session info
shell: Rscript {0}
run: |
options(width = 100)
pak::pkg_install("sessioninfo")
pkgs <- installed.packages()[, "Package"]
sessioninfo::session_info(pkgs, include_base = TRUE)
@@ -99,7 +100,6 @@ jobs:
- name: Document
run: |
Rscript -e 'pak::pkg_install("devtools")'
Rscript -e 'devtools::document()'
git add man/\* NAMESPACE
git commit -m 'Document (GitHub Actions)' || echo "No documentation changes to commit"
@@ -123,24 +123,14 @@ jobs:
key: ${{ matrix.config.os }}-${{ matrix.config.node }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ matrix.config.os }}-${{ matrix.config.node }}-yarn-
- name: Sync DESCRIPTION and package.json versions
run: |
Rscript -e 'pak::pkg_install("jsonlite")'
Rscript -e 'pkg <- jsonlite::read_json("package.json", simplifyVector = TRUE)' \
-e 'version <- as.list(read.dcf("DESCRIPTION")[1,])$Version' \
-e 'pkg$version <- gsub("^(\\d+).(\\d+).(\\d+).(.+)$", "\\1.\\2.\\3-alpha.\\4", version)' \
-e 'pkg$files <- as.list(pkg$files)' \
-e 'jsonlite::write_json(pkg, path = "package.json", pretty = TRUE, auto_unbox = TRUE)'
git add package.json && git commit -m 'sync package version (GitHub Actions)' || echo "No version changes to commit"
- name: Build JS
run: |
tree srcts
rm -r srcts/types
cd srcts
tree src
yarn install --immutable && yarn build
git add ./srcts/src && git commit -m 'yarn lint (GitHub Actions)' || echo "No yarn lint changes to commit"
git add ./srcts/types && git commit -m 'yarn tsc (GitHub Actions)' || echo "No type definition changes to commit"
git add ./inst && git commit -m 'yarn build (GitHub Actions)' || echo "No yarn build changes to commit"
git add ./src && git commit -m 'yarn lint (GitHub Actions)' || echo "No yarn lint changes to commit"
git add ./types && git commit -m 'yarn tsc (GitHub Actions)' || echo "No type definition changes to commit"
git add ../inst && git commit -m 'yarn build (GitHub Actions)' || echo "No yarn build changes to commit"
if [ -n "$(git status --porcelain)" ]
then
git status --porcelain
@@ -159,9 +149,9 @@ jobs:
if: github.event_name == 'push'
run: |
# Can't push to a protected branch
if [ -n "`git cherry origin/master`"]; then
if [ -z "`git cherry`"]; then
echo "Un-pushed commits:"
git cherry -v origin/master
git cherry -v
echo "\nCan not push to a protected branch. Exiting"
exit 1
fi
@@ -169,4 +159,5 @@ jobs:
# Execute after pushing, as no updated files will be produced
- name: Test TypeScript code
run: |
cd srcts
yarn test

View File

@@ -1,7 +1,7 @@
Package: shiny
Type: Package
Title: Web Application Framework for R
Version: 1.6.0.9022
Version: 1.6.0.9021
Authors@R: c(
person("Winston", "Chang", role = c("aut", "cre"), email = "winston@rstudio.com", comment = c(ORCID = "0000-0002-1576-2126")),
person("Joe", "Cheng", role = "aut", email = "joe@rstudio.com"),

View File

@@ -378,7 +378,6 @@ importFrom(htmltools,tags)
importFrom(htmltools,validateCssUnit)
importFrom(htmltools,withTags)
importFrom(lifecycle,deprecated)
importFrom(lifecycle,is_present)
importFrom(promises,"%...!%")
importFrom(promises,"%...>%")
importFrom(promises,as.promise)
@@ -387,18 +386,14 @@ importFrom(promises,promise)
importFrom(promises,promise_reject)
importFrom(promises,promise_resolve)
importFrom(rlang,"%||%")
importFrom(rlang,"fn_body<-")
importFrom(rlang,"fn_fmls<-")
importFrom(rlang,as_function)
importFrom(rlang,as_quosure)
importFrom(rlang,enexpr)
importFrom(rlang,enquo)
importFrom(rlang,enquo0)
importFrom(rlang,enquos)
importFrom(rlang,enquos0)
importFrom(rlang,eval_tidy)
importFrom(rlang,expr)
importFrom(rlang,fn_body)
importFrom(rlang,get_env)
importFrom(rlang,get_expr)
importFrom(rlang,inject)
@@ -413,8 +408,4 @@ importFrom(rlang,new_function)
importFrom(rlang,new_quosure)
importFrom(rlang,pairlist2)
importFrom(rlang,quo)
importFrom(rlang,quo_get_expr)
importFrom(rlang,quo_is_missing)
importFrom(rlang,quo_set_env)
importFrom(rlang,quo_set_expr)
importFrom(rlang,zap_srcref)

10
NEWS.md
View File

@@ -7,8 +7,6 @@ shiny 1.6.0.9000
* The `format` and `locale` arguments to `sliderInput()` have been removed. They have been deprecated since 0.10.2.2 (released on 2014-12-08).
* Closed #3403: `insertTab()`'s `position` parameter now defaults to `"after"` instead of `"before"`. This has the benefit of allowing us to fix a bug in positioning when `target = NULL`, but has the drawback of changing the default behavior when `target` is not `NULL`. (#3404)
### New features and improvements
* Bootstrap 5 support. (#3410 and rstudio/bslib#304)
@@ -17,17 +15,13 @@ shiny 1.6.0.9000
* Closed #3322, #3313, #1823, #3321, #3320, #1928, and #2310: Various improvements to `navbarPage()`, `tabsetPanel()`, `tabPanel()`, `navbarMenu()`, etc. Also, these functions are now powered by the `{bslib}` package's new `nav()` API (consider using `{bslib}`'s API to create better looking and more fully featured navs). (#3388)
* All uses of `list(...)` have been replaced with `rlang::list2(...)`. This means that you can use trailing `,` without error and use rlang's `!!!` operator to "splice" a list of argument values into `...`. We think this'll be particularly useful for passing a list of `tabPanel()` to their consumers (i.e., `tabsetPanel()`, `navbarPage()`, etc). For example, `tabs <- list(tabPanel("A", "a"), tabPanel("B", "b")); navbarPage(!!!tabs)`. (#3315 and #3328)
* `installExprFunction()` and `exprToFunction()` are now able to handle quosures, so `render`-functions which call these functions can now understand quosures, when they are called using `rlang::inject()`. This also means that `render` function no longer need `env` and `quoted` parameters; that information can be embedded into a quosure which is then passed to the `render` function. Additionally, the `getQuosure()` function was added, which makes it easier for developers to create `render` functions which understand quosures. Better documentation was added for how to create `render` functions. (#3462, #3466)
* `icon(lib="fontawesome")` is now powered by the `{fontawesome}` package, which will make it easier to use the latest FA icons in the future (by updating the `{fontawesome}` package). (#3302)
* Closed #3397: `renderPlot()` new uses `ggplot2::get_alt_text()` to inform an `alt` text default (for `{ggplot2}` plots). (#3398)
* `modalDialog()` gains support for `size = "xl"`. (#3410)
* Addressed #2521: Updated the list of TCP ports that will be rejected by default in runapp.R, adding 5060, 5061 and 6566. Added documentation describing the port range (3000:8000) and which ports are rejected. (#3456)
### Other improvements
* Shiny's core JavaScript code was converted to TypeScript. For the latest development information, please see the [README.md in `./srcts`](https://github.com/rstudio/shiny/tree/master/srcts). (#3296)
@@ -42,8 +36,6 @@ shiny 1.6.0.9000
* Closed #3345: Shiny now correctly renders `htmltools::htmlDependency()`(s) with a `list()` of `script` attributes when used in a dynamic UI context. This fairly new `htmlDependency()` feature was added in `{htmltools}` v0.5.1. (#3395)
* Fixed [#2666](https://github.com/rstudio/shiny/issues/2666) and [#2670](https://github.com/rstudio/shiny/issues/2670): `nearPoints()` and `brushedPoints()` weren't properly account for missing values (#2666 was introduced in v1.4.0). ([#2668](https://github.com/rstudio/shiny/pull/2668))
* Closed #3374: `quoToFunction()` now works correctly with nested quosures; and as a result, quasi-quotation with rendering function (e.g., `renderPrint()`, `renderPlot()`, etc) now works as expected with nested quosures. (#3373)
* Exported `register_devmode_option()`. This method was described in the documentation for `devmode()` but was never exported. See `?devmode()` for more details on how to register Shiny Developer options using `register_devmode_option()`. (#3364)

View File

@@ -292,11 +292,11 @@ utils::globalVariables(".GenericCallEnv", add = TRUE)
#' In some cases, however, the automatic cache hint inference is not
#' sufficient, and it is necessary to provide a cache hint. This is true
#' for `renderPrint()`. Unlike `renderText()`, it wraps the user-provided
#' expression in another function, before passing it to [createRenderFunction()]
#' expression in another function, before passing it to [markRenderFunction()]
#' (instead of [createRenderFunction()]). Because the user code is wrapped in
#' another function, `createRenderFunction()` is not able to automatically
#' another function, `markRenderFunction()` is not able to automatically
#' extract the user-provided code and use it in the cache key. Instead,
#' `renderPrint` calls `createRenderFunction()`, it explicitly passes along a
#' `renderPrint` calls `markRenderFunction()`, it explicitly passes along a
#' `cacheHint`, which includes a label and the original user expression.
#'
#' In general, if you need to provide a `cacheHint`, it is best practice to
@@ -310,19 +310,19 @@ utils::globalVariables(".GenericCallEnv", add = TRUE)
#'
#' ```
#' renderMyWidget <- function(expr) {
#' q <- rlang::enquo0(expr)
#' expr <- substitute(expr)
#'
#' htmlwidgets::shinyRenderWidget(
#' q,
#' htmlwidgets::shinyRenderWidget(expr,
#' myWidgetOutput,
#' quoted = TRUE,
#' cacheHint = list(label = "myWidget", userQuo = q)
#' env = parent.frame(),
#' cacheHint = list(label = "myWidget", userExpr = expr)
#' )
#' }
#' ```
#'
#' If your `render` function sets any internal state, you may find it useful
#' in your call to [createRenderFunction()] to use
#' in your call to [createRenderFunction()] or [markRenderFunction()] to use
#' the `cacheWriteHook` and/or `cacheReadHook` parameters. These hooks are
#' functions that run just before the object is stored in the cache, and just
#' after the object is retrieved from the cache. They can modify the data
@@ -339,8 +339,8 @@ utils::globalVariables(".GenericCallEnv", add = TRUE)
#' effects or modify some external state, and they must re-execute each time
#' in order to work properly.
#'
#' For developers of such code, they should call [createRenderFunction()] (or
#' [markRenderFunction()]) with `cacheHint = FALSE`.
#' For developers of such code, they should call [createRenderFunction()] or
#' [markRenderFunction()] with `cacheHint = FALSE`.
#'
#'
#' @section Caching with `renderPlot()`:

View File

@@ -9,19 +9,13 @@
#' @param details Additional information to be added after a new line to the displayed message
#' @keywords internal
shinyDeprecated <- function(
version,
what,
with = NULL,
details = NULL,
type = c("deprecated", "superseded")
version, what, with = NULL, details = NULL
) {
if (is_false(getOption("shiny.deprecation.messages"))) {
return(invisible())
}
type <- match.arg(type)
msg <- paste0("`", what, "` is ", type, " as of shiny ", version, ".")
msg <- paste0("`", what, "` is deprecated as of shiny ", version, ".")
if (!is.null(with)) {
msg <- paste0(msg, "\n", "Please use `", with, "` instead.")
}
@@ -38,20 +32,13 @@ deprecatedEnvQuotedMessage <- function() {
if (!in_devmode()) return(invisible())
if (is_false(getOption("shiny.deprecation.messages"))) return(invisible())
# Capture calling function
grandparent_call <- sys.call(-2)
# Turn language into user friendly string
grandparent_txt <- paste0(utils::capture.output({grandparent_call}), collapse = "\n")
# manually
msg <- paste0(
"The `env` and `quoted` arguments are deprecated as of shiny 1.7.0.",
"The `env` and `quoted` arguments are deprecated as of shiny 1.6.0.",
" Please use quosures from `rlang` instead.\n",
"See <https://github.com/rstudio/shiny/issues/3108> for more information.\n",
"Function call:\n",
grandparent_txt
"See <https://github.com/rstudio/shiny/issues/3108> for more information."
)
# Call less often as users do not have much control over this warning
rlang::inform(message = msg, .frequency = "regularly", .frequency_id = msg, .file = stderr())
rlang::inform(message = msg, .frequency = "always", .frequency_id = msg, .file = stderr())
}
@@ -73,7 +60,7 @@ diskCache <- function(
logfile = NULL
) {
shinyDeprecated("1.6.0", "diskCache()", "cachem::cache_disk()")
if (is_present(exec_missing)) {
if (lifecycle::is_present(exec_missing)) {
shinyDeprecated("1.6.0", "diskCache(exec_missing =)")
}
@@ -106,7 +93,7 @@ memoryCache <- function(
logfile = NULL)
{
shinyDeprecated("1.6.0", "diskCache()", "cachem::cache_mem()")
if (is_present(exec_missing)) {
if (lifecycle::is_present(exec_missing)) {
shinyDeprecated("1.6.0", "diskCache(exec_missing =)")
}

View File

@@ -512,7 +512,7 @@ MessageLogger = R6Class(
return(txt)
},
singleLine = function(txt) {
gsub("([^\\])\\n", "\\1\\\\n", txt)
gsub("[^\\]\\n", "\\\\n", txt)
},
valueStr = function(valueStr) {
paste0(

View File

@@ -20,6 +20,7 @@
#' `delay` milliseconds before sending an event.
#' @seealso [brushOpts()] for brushing events.
#' @export
#' @keywords internal
clickOpts <- function(id, clip = TRUE) {
if (is.null(id))
stop("id must not be NULL")

View File

@@ -267,7 +267,6 @@ nearPoints <- function(df, coordinfo, xvar = NULL, yvar = NULL,
stop("nearPoints: `yvar` ('", yvar ,"') not in names of input")
# Extract data values from the data frame
coordinfo <- fortifyDiscreteLimits(coordinfo)
x <- asNumber(df[[xvar]], coordinfo$domain$discrete_limits$x)
y <- asNumber(df[[yvar]], coordinfo$domain$discrete_limits$y)
@@ -393,7 +392,6 @@ nearPoints <- function(df, coordinfo, xvar = NULL, yvar = NULL,
# an input brush
within_brush <- function(vals, brush, var = "x") {
var <- match.arg(var, c("x", "y"))
brush <- fortifyDiscreteLimits(brush)
vals <- asNumber(vals, brush$domain$discrete_limits[[var]])
# It's possible for a non-missing data values to not
# map to the axis limits, for example:
@@ -416,43 +414,11 @@ asNumber <- function(x, levels = NULL) {
as.numeric(x)
}
# Ensure the discrete limits/levels of a coordmap received
# from the client matches the data structure sent the client.
#
# When we construct the coordmap (in getGgplotCoordmap()),
# we save a character vector which may contain missing values
# (e.g., c("a", "b", NA)). When that same character is received
# from the client, it runs through decodeMessage() which sets
# simplifyVector=FALSE, which means NA are replaced by NULL
# (because jsonlite::fromJSON('["a", "b", null]') -> list("a", "b", NULL))
#
# Thankfully, it doesn't seem like it's meaningful for limits to
# contains a NULL in the 1st place, so we simply treat NULL like NA.
# For more context, https://github.com/rstudio/shiny/issues/2666
fortifyDiscreteLimits <- function(coord) {
# Note that discrete_limits$x/y are populated iff
# x/y are discrete mappings
coord$domain$discrete_limits <- lapply(
coord$domain$discrete_limits,
function(var) {
# if there is an 'explicit' NULL, then the limits are NA
if (is.null(var)) return(NA)
vapply(var, function(x) {
if (is.null(x) || isTRUE(is.na(x))) NA_character_ else x
}, character(1))
}
)
coord
}
# Given a panelvar value and a vector x, return logical vector indicating which
# items match the panelvar value. Because the panelvar value is always a
# string but the vector could be numeric, it might be necessary to coerce the
# panelvar to a number before comparing to the vector.
panelMatch <- function(search_value, x) {
if (is.null(search_value)) return(is.na(x))
if (is.numeric(x)) search_value <- as.numeric(search_value)
x == search_value
}

View File

@@ -208,6 +208,15 @@ selectizeIt <- function(inputId, select, options, nonempty = FALSE) {
options$plugins <- c(options$plugins, list('selectize-plugin-a11y'))
}
# to prevent clipping of the selectize drop-down we set the dropdownParent
# to "body". This might be necessary if e.g. overflow-x: scroll is set
# on it's container, which forces overflow-y to 'auto' (as per
# https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-y). Discussion
# of usage here: https://github.com/selectize/selectize.js/issues/192
if (is.null(options$dropdownParent)) {
options$dropdownParent <- "body"
}
res <- checkAsIs(options)
deps <- list(selectizeDependency())

View File

@@ -229,11 +229,8 @@ ionRangeSliderDependencyCSS <- function(theme) {
}
bslib::bs_dependency(
input = list(
list(accent = "$component-active-bg"),
sass::sass_file(
system.file(package = "shiny", "www/shared/ionrangeslider/scss/shiny.scss")
)
input = sass::sass_file(
system.file(package = "shiny", "www/shared/ionrangeslider/scss/shiny.scss")
),
theme = theme,
name = "ionRangeSlider",

View File

@@ -113,7 +113,7 @@
#' }
#' @export
insertTab <- function(inputId, tab, target = NULL,
position = c("after", "before"), select = FALSE,
position = c("before", "after"), select = FALSE,
session = getDefaultReactiveDomain()) {
bslib::nav_insert(
inputId, tab, target,
@@ -137,14 +137,14 @@ insertTab <- function(inputId, tab, target = NULL,
#' @export
prependTab <- function(inputId, tab, select = FALSE, menuName = NULL,
session = getDefaultReactiveDomain()) {
bslib::nav_prepend(inputId, tab, menu_title = menuName, select = select, session = session)
bslib::tab_prepend(inputId, tab, menu_title = menuName, select = select, session = session)
}
#' @rdname insertTab
#' @export
appendTab <- function(inputId, tab, select = FALSE, menuName = NULL,
session = getDefaultReactiveDomain()) {
bslib::nav_append(inputId, tab, menu_title = menuName, select = select, session = session)
bslib::tab_append(inputId, tab, menu_title = menuName, select = select, session = session)
}
#' @rdname insertTab

View File

@@ -4,7 +4,6 @@
#' themselves in knitr/rmarkdown documents.
#'
#' @name knitr_methods
#' @keywords internal
#' @param x Object to knit_print
#' @param ... Additional knit_print arguments
NULL

View File

@@ -1,3 +1,4 @@
#' @importFrom fastmap fastmap
Map <- R6Class(
'Map',
portable = FALSE,

View File

@@ -178,15 +178,14 @@ modalDialog <- function(..., title = NULL, footer = modalButton("Dismiss"),
if (!is.null(footer)) div(class = "modal-footer", footer)
)
),
# jQuery plugin doesn't work in Bootstrap 5, but vanilla JS doesn't work in Bootstrap 4 :sob:
tags$script(HTML(
"if (window.bootstrap && !window.bootstrap.Modal.VERSION.match(/^4\\./)) {
tags$script(
"if (window.bootstrap) {
var modal = new bootstrap.Modal(document.getElementById('shiny-modal'));
modal.show();
} else {
$('#shiny-modal').modal().focus();
}"
))
)
)
}

View File

@@ -875,7 +875,8 @@ Observable <- R6Class(
invisible(.value)
},
format = function() {
simpleExprToFunction(fn_body(.origFunc), "reactive")
label <- sprintf('reactive(%s)', paste(deparse(body(.origFunc)), collapse='\n'))
strsplit(label, "\n")[[1]]
},
.updateValue = function() {
ctx <- Context$new(.domain, .label, type = 'observable',
@@ -944,15 +945,14 @@ Observable <- R6Class(
#' See the [Shiny tutorial](https://shiny.rstudio.com/tutorial/) for
#' more information about reactive expressions.
#'
#' @param x For `is.reactive()`, an object to test. For `reactive()`, an expression. When passing in a [`quo()`]sure with `reactive()`, remember to use [`rlang::inject()`] to distinguish that you are passing in the content of your quosure, not the expression of the quosure.
#' @template param-env
#' @templateVar x x
#' @templateVar env env
#' @templateVar quoted quoted
#' @template param-quoted
#' @templateVar x x
#' @templateVar quoted quoted
#' @param x For `reactive`, an expression (quoted or unquoted). For
#' `is.reactive`, an object to test.
#' @param env The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression.
#' @param quoted Is the expression quoted? By default, this is `FALSE`.
#' This is useful when you want to use an expression that is stored in a
#' variable; to do so, it must be quoted with `quote()`.
#' @param label A label for the reactive expression, useful for debugging.
#' @param domain See [domains].
#' @param ..stacktraceon Advanced use only. For stack manipulation purposes; see
@@ -961,56 +961,46 @@ Observable <- R6Class(
#' @return a function, wrapped in a S3 class "reactive"
#'
#' @examples
#' library(rlang)
#' values <- reactiveValues(A=1)
#'
#' reactiveB <- reactive({
#' values$A + 1
#' })
#' # View the values from the R console with isolate()
#' isolate(reactiveB())
#' # 2
#'
#' # Can use quoted expressions
#' reactiveC <- reactive(quote({ values$A + 2 }), quoted = TRUE)
#'
#' # To store expressions for later conversion to reactive, use quote()
#' myquo <- rlang::quo(values$A + 2)
#' # Unexpected value! Sending a quosure directly will not work as expected.
#' reactiveC <- reactive(myquo)
#' # We'd hope for `3`, but instead we get the quosure that was supplied.
#' expr_q <- quote({ values$A + 3 })
#' reactiveD <- reactive(expr_q, quoted = TRUE)
#'
#' # View the values from the R console with isolate()
#' isolate(reactiveB())
#' isolate(reactiveC())
#'
#' # Instead, the quosure should be `rlang::inject()`ed
#' reactiveD <- rlang::inject(reactive(!!myquo))
#' isolate(reactiveD())
#' # 3
#'
#' # (Legacy) Can use quoted expressions
#' expr <- quote({ values$A + 3 })
#' reactiveE <- reactive(expr, quoted = TRUE)
#' isolate(reactiveE())
#' # 4
#'
#' @export
reactive <- function(
x,
env = parent.frame(),
quoted = FALSE,
reactive <- function(x, env = parent.frame(), quoted = FALSE,
...,
label = NULL,
domain = getDefaultReactiveDomain(),
..stacktraceon = TRUE
) {
..stacktraceon = TRUE)
{
check_dots_empty()
func <- installExprFunction(x, "func", env, quoted, wrappedWithLabel = FALSE)
# Attach a label and a reference to the original user source for debugging
userExpr <- fn_body(func)
label <- exprToLabel(userExpr, "reactive", label)
x <- get_quosure(x, env, quoted)
fun <- as_function(x)
# as_function returns a function that takes `...`. We need one that takes no
# args.
formals(fun) <- list()
o <- Observable$new(func, label, domain, ..stacktraceon = ..stacktraceon)
# Attach a label and a reference to the original user source for debugging
label <- exprToLabel(get_expr(x), "reactive", label)
o <- Observable$new(fun, label, domain, ..stacktraceon = ..stacktraceon)
structure(
o$getValue,
observable = o,
cacheHint = list(userExpr = zap_srcref(userExpr)),
cacheHint = list(userExpr = zap_srcref(get_expr(x))),
class = c("reactiveExpr", "reactive", "function")
)
}
@@ -1335,7 +1325,12 @@ Observer <- R6Class(
#'
#' @param x An expression (quoted or unquoted). Any return value will be
#' ignored.
#' @inheritParams reactive
#' @param env The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression.
#' @param quoted Is the expression quoted? By default, this is `FALSE`.
#' This is useful when you want to use an expression that is stored in a
#' variable; to do so, it must be quoted with `quote()`.
#' @param label A label for the observer, useful for debugging.
#' @param suspended If `TRUE`, start the observer in a suspended state. If
#' `FALSE` (the default), start in a non-suspended state.
@@ -1394,21 +1389,18 @@ Observer <- R6Class(
#' print(values$A + 1)
#' })
#'
#' # To store expressions for later conversion to observe, use rlang::quo()
#' myquo <- rlang::quo({ print(values$A + 3) })
#' obsC <- rlang::inject(observe(!!myquo))
#' # Can use quoted expressions
#' obsC <- observe(quote({ print(values$A + 2) }), quoted = TRUE)
#'
#' # (Legacy) Can use quoted expressions
#' obsD <- observe(quote({ print(values$A + 2) }), quoted = TRUE)
#' # To store expressions for later conversion to observe, use quote()
#' expr_q <- quote({ print(values$A + 3) })
#' obsD <- observe(expr_q, quoted = TRUE)
#'
#' # In a normal Shiny app, the web client will trigger flush events. If you
#' # are at the console, you can force a flush with flushReact()
#' shiny:::flushReact()
#' @export
observe <- function(
x,
env = parent.frame(),
quoted = FALSE,
observe <- function(x, env = parent.frame(), quoted = FALSE,
...,
label = NULL,
suspended = FALSE,
@@ -1419,11 +1411,18 @@ observe <- function(
{
check_dots_empty()
func <- installExprFunction(x, "func", env, quoted)
label <- funcToLabel(func, "observe", label)
x <- get_quosure(x, env, quoted)
fun <- as_function(x)
# as_function returns a function that takes `...`. We need one that takes no
# args.
formals(fun) <- list()
if (is.null(label)) {
label <- sprintf('observe(%s)', paste(deparse(get_expr(x)), collapse='\n'))
}
o <- Observer$new(
func,
fun,
label = label,
suspended = suspended,
priority = priority,
@@ -2145,30 +2144,23 @@ maskReactiveContext <- function(expr) {
#' @param valueExpr The expression that produces the return value of the
#' `eventReactive`. It will be executed within an [isolate()]
#' scope.
#' @param event.env The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression. If `eventExpr` is a quosure and `event.quoted` is `TRUE`,
#' then `event.env` is ignored.
#' @param event.quoted If it is `TRUE`, then the [`quote()`]ed value of `eventExpr`
#' will be used when `eventExpr` is evaluated. If `eventExpr` is a quosure and you
#' would like to use its expression as a value for `eventExpr`, then you must set
#' `event.quoted` to `TRUE`.
#' @param handler.env The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression. If `handlerExpr` is a quosure and `handler.quoted` is `TRUE`,
#' then `handler.env` is ignored.
#' @param handler.quoted If it is `TRUE`, then the [`quote()`]ed value of `handlerExpr`
#' will be used when `handlerExpr` is evaluated. If `handlerExpr` is a quosure and you
#' would like to use its expression as a value for `handlerExpr`, then you must set
#' `handler.quoted` to `TRUE`.
#' @param value.env The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression. If `valueExpr` is a quosure and `value.quoted` is `TRUE`,
#' then `value.env` is ignored.
#' @param value.quoted If it is `TRUE`, then the [`quote()`]ed value of `valueExpr`
#' will be used when `valueExpr` is evaluated. If `valueExpr` is a quosure and you
#' would like to use its expression as a value for `valueExpr`, then you must set
#' `value.quoted` to `TRUE`.
#' @param event.env The parent environment for `eventExpr`. By default,
#' this is the calling environment.
#' @param event.quoted Is the `eventExpr` expression quoted? By default,
#' this is `FALSE`. This is useful when you want to use an expression
#' that is stored in a variable; to do so, it must be quoted with
#' `quote()`.
#' @param handler.env The parent environment for `handlerExpr`. By default,
#' this is the calling environment.
#' @param handler.quoted Is the `handlerExpr` expression quoted? By
#' default, this is `FALSE`. This is useful when you want to use an
#' expression that is stored in a variable; to do so, it must be quoted with
#' `quote()`.
#' @param value.env The parent environment for `valueExpr`. By default,
#' this is the calling environment.
#' @param value.quoted Is the `valueExpr` expression quoted? By default,
#' this is `FALSE`. This is useful when you want to use an expression
#' that is stored in a variable; to do so, it must be quoted with `quote()`.
#' @param label A label for the observer or reactive, useful for debugging.
#' @param suspended If `TRUE`, start the observer in a suspended state. If
#' `FALSE` (the default), start in a non-suspended state.
@@ -2282,13 +2274,15 @@ observeEvent <- function(eventExpr, handlerExpr,
{
check_dots_empty()
eventQ <- exprToQuo(eventExpr, event.env, event.quoted)
handlerQ <- exprToQuo(handlerExpr, handler.env, handler.quoted)
eventExpr <- get_quosure(eventExpr, event.env, event.quoted)
handlerExpr <- get_quosure(handlerExpr, handler.env, handler.quoted)
label <- quoToLabel(eventQ, "observeEvent", label)
if (is.null(label)) {
label <- sprintf('observeEvent(%s)', paste(deparse(get_expr(eventExpr)), collapse='\n'))
}
handler <- inject(observe(
!!handlerQ,
!!handlerExpr,
label = label,
suspended = suspended,
priority = priority,
@@ -2302,7 +2296,7 @@ observeEvent <- function(eventExpr, handlerExpr,
ignoreInit = ignoreInit,
once = once,
label = label,
!!eventQ,
!!eventExpr,
x = handler
))
@@ -2320,17 +2314,19 @@ eventReactive <- function(eventExpr, valueExpr,
{
check_dots_empty()
eventQ <- exprToQuo(eventExpr, event.env, event.quoted)
valueQ <- exprToQuo(valueExpr, value.env, value.quoted)
eventExpr <- get_quosure(eventExpr, event.env, event.quoted)
valueExpr <- get_quosure(valueExpr, value.env, value.quoted)
label <- quoToLabel(eventQ, "eventReactive", label)
if (is.null(label)) {
label <- sprintf('eventReactive(%s)', paste(deparse(get_expr(eventExpr)), collapse='\n'))
}
invisible(inject(bindEvent(
ignoreNULL = ignoreNULL,
ignoreInit = ignoreInit,
label = label,
!!eventQ,
x = reactive(!!valueQ, domain = domain, label = label)
!!eventExpr,
x = reactive(!!valueExpr, domain = domain, label = label)
)))
}

View File

@@ -46,7 +46,9 @@
#' decorative images.
#' @param ... Arguments to be passed through to [grDevices::png()].
#' These can be used to set the width, height, background color, etc.
#' @inheritParams renderUI
#' @param env The environment in which to evaluate `expr`.
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
#' is useful if you want to save an expression in a variable.
#' @param execOnResize If `FALSE` (the default), then when a plot is
#' resized, Shiny will *replay* the plot drawing commands with
#' [grDevices::replayPlot()] instead of re-executing `expr`.
@@ -63,13 +65,10 @@ renderPlot <- function(expr, width = 'auto', height = 'auto', res = 72, ...,
execOnResize = FALSE, outputArgs = list()
) {
func <- installExprFunction(
expr, "func", env, quoted,
label = "renderPlot",
# This ..stacktraceon is matched by a ..stacktraceoff.. when plotFunc
# is called
..stacktraceon = TRUE
)
expr <- get_quosure(expr, env, quoted)
# This ..stacktraceon is matched by a ..stacktraceoff.. when plotFunc
# is called
func <- quoToFunction(expr, "renderPlot", ..stacktraceon = TRUE)
args <- list(...)
@@ -187,7 +186,7 @@ renderPlot <- function(expr, width = 'auto', height = 'auto', res = 72, ...,
outputFunc,
renderFunc,
outputArgs,
cacheHint = list(userExpr = installedFuncExpr(func), res = res)
cacheHint = list(userExpr = get_expr(expr), res = res)
)
class(markedFunc) <- c("shiny.renderPlot", class(markedFunc))
markedFunc
@@ -345,7 +344,7 @@ custom_print.ggplot <- function(x) {
# Infer alt text description from renderPlot() value
# (currently just ggplot2 is supported)
getAltText <- function(x, default = "Plot object") {
getAltText <- function(x, default = "Plot Object") {
# Since, inside renderPlot(), custom_print.ggplot()
# overrides print.ggplot, this class indicates a ggplot()
if (!inherits(x, "ggplot_build_gtable")) {

View File

@@ -42,7 +42,9 @@
#' (i.e. they either evaluate to `NA` or `NaN`).
#' @param ... Arguments to be passed through to [xtable::xtable()]
#' and [xtable::print.xtable()].
#' @inheritParams renderUI
#' @param env The environment in which to evaluate `expr`.
#' @param quoted Is `expr` a quoted expression (with `quote()`)?
#' This is useful if you want to save an expression in a variable.
#' @param outputArgs A list of arguments to be passed through to the
#' implicit call to [tableOutput()] when `renderTable` is
#' used in an interactive R Markdown document.
@@ -72,7 +74,8 @@ renderTable <- function(expr, striped = FALSE, hover = FALSE,
env = parent.frame(), quoted = FALSE,
outputArgs=list())
{
func <- installExprFunction(expr, "func", env, quoted, label = "renderTable")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderTable")
if (!is.function(spacing)) spacing <- match.arg(spacing)

View File

@@ -22,10 +22,7 @@
#' @param port The TCP port that the application should listen on. If the
#' `port` is not specified, and the `shiny.port` option is set (with
#' `options(shiny.port = XX)`), then that port will be used. Otherwise,
#' use a random port between 3000:8000, excluding ports that are blocked
#' by Google Chrome for being considered unsafe: 3659, 4045, 5060,
#' 5061, 6000, 6566, 6665:6669 and 6697. Up to twenty random
#' ports will be tried.
#' use a random port.
#' @param launch.browser If true, the system's default web browser will be
#' launched automatically after the app is started. Defaults to true in
#' interactive sessions only. This value of this parameter can also be a
@@ -304,8 +301,7 @@ runApp <- function(appDir=getwd(),
# Reject ports in this range that are considered unsafe by Chrome
# http://superuser.com/questions/188058/which-ports-are-considered-unsafe-on-chrome
# https://github.com/rstudio/shiny/issues/1784
# https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/net/base/port_util.cc
if (!port %in% c(3659, 4045, 5060, 5061, 6000, 6566, 6665:6669, 6697)) {
if (!port %in% c(3659, 4045, 6000, 6665:6669, 6697)) {
break
}
}

View File

@@ -5,6 +5,7 @@
#' value. The returned value will be used for the test snapshot.
#' @param session A Shiny session object.
#'
#' @keywords internal
#' @export
setSerializer <- function(inputId, fun, session = getDefaultReactiveDomain()) {
if (is.null(session)) {

View File

@@ -46,7 +46,7 @@ inputHandlers <- Map$new()
#' }
#'
#' }
#' @seealso [removeInputHandler()] [applyInputHandlers()]
#' @seealso [removeInputHandler()]
#' @export
registerInputHandler <- function(type, fun, force=FALSE){
if (inputHandlers$containsKey(type) && !force){

View File

@@ -2,7 +2,7 @@
## usethis namespace: start
## usethis namespace: end
#' @importFrom lifecycle deprecated is_present
#' @importFrom lifecycle deprecated
#' @importFrom grDevices dev.set dev.cur
#' @importFrom fastmap fastmap
#' @importFrom promises %...!%
@@ -11,13 +11,11 @@
#' promise promise_resolve promise_reject is.promising
#' as.promise
#' @importFrom rlang
#' quo enquo enquo0 as_function get_expr get_env new_function enquos
#' quo enquo as_function get_expr get_env new_function enquos
#' eval_tidy expr pairlist2 new_quosure enexpr as_quosure is_quosure inject
#' quo_set_env quo_set_expr quo_get_expr
#' enquos0 zap_srcref %||% is_na
#' is_false list2
#' missing_arg is_missing maybe_missing
#' quo_is_missing fn_fmls<- fn_body fn_body<-
#' @importFrom ellipsis
#' check_dots_empty check_dots_unnamed
#' @import htmltools

View File

@@ -2,23 +2,12 @@ utils::globalVariables('func', add = TRUE)
#' Mark a function as a render function
#'
#' `r lifecycle::badge("superseded")` Please use [`createRenderFunction()`] to
#' support async execution. (Shiny 1.1.0)
#'
#' Should be called by implementers of `renderXXX` functions in order to mark
#' their return values as Shiny render functions, and to provide a hint to Shiny
#' regarding what UI function is most commonly used with this type of render
#' function. This can be used in R Markdown documents to create complete output
#' widgets out of just the render function.
#'
#' Note that it is generally preferable to use [createRenderFunction()] instead
#' of `markRenderFunction()`. It essentially wraps up the user-provided
#' expression in the `transform` function passed to it, then passes the resulting
#' function to `markRenderFunction()`. It also provides a simpler calling
#' interface. There may be cases where `markRenderFunction()` must be used instead of
#' [createRenderFunction()] -- for example, when the `transform` parameter of
#' [createRenderFunction()] is not flexible enough for your needs.
#'
#' @param uiFunc A function that renders Shiny UI. Must take a single argument:
#' an output ID.
#' @param renderFunc A function that is suitable for assigning to a Shiny output
@@ -48,7 +37,7 @@ utils::globalVariables('func', add = TRUE)
#' is able to serve JS and CSS resources.
#' @return The `renderFunc` function, with annotations.
#'
#' @seealso [createRenderFunction()]
#' @seealso [createRenderFunction()], [quoToFunction()]
#' @export
markRenderFunction <- function(
uiFunc,
@@ -58,12 +47,6 @@ markRenderFunction <- function(
cacheWriteHook = NULL,
cacheReadHook = NULL
) {
# (Do not emit warning for superseded code, "since theres no risk if you keep using it")
# # This method is called by the superseding function, createRenderFunction().
# if (in_devmode()) {
# shinyDeprecated("1.1.0", "markRenderFunction()", "createRenderFunction()")
# }
force(renderFunc)
# a mutable object that keeps track of whether `useRenderFunction` has been
@@ -111,7 +94,6 @@ markRenderFunction <- function(
# For everything else, do nothing.
cacheHint <- lapply(cacheHint, function(x) {
if (is.function(x)) formalsAndBody(x)
else if (is_quosure(x)) zap_srcref(quo_get_expr(x))
else if (is.language(x)) zap_srcref(x)
else x
})
@@ -151,27 +133,10 @@ print.shiny.render.function <- function(x, ...) {
cat_line("<shiny.render.function>")
}
#' Implement custom render functions
#' Implement render functions
#'
#' Developer-facing utilities for implementing a custom `renderXXX()` function.
#' Before using these utilities directly, consider using the [`htmlwidgets`
#' package](http://www.htmlwidgets.org/develop_intro.html) to implement custom
#' outputs (i.e., custom `renderXXX()`/`xxxOutput()` functions). That said,
#' these utilities can be used more directly if a full-blown htmlwidget isn't
#' needed and/or the user-supplied reactive expression needs to be wrapped in
#' additional call(s).
#'
#' To implement a custom `renderXXX()` function, essentially 2 things are needed:
#' 1. Capture the user's reactive expression as a function.
#' * New `renderXXX()` functions can use `quoToFunction()` for this, but
#' already existing `renderXXX()` functions that contain `env` and `quoted`
#' parameters may want to continue using `installExprFunction()` for better
#' legacy support (see examples).
#' 2. Flag the resulting function (from 1) as a Shiny rendering function and
#' also provide a UI container for displaying the result of the rendering
#' function.
#' * `createRenderFunction()` is currently recommended (instead of
#' [markRenderFunction()]) for this step (see examples).
#' This function is a wrapper for [markRenderFunction()] which provides support
#' for async computation via promises.
#'
#' @param func A function without parameters, that returns user data. If the
#' returned value is a promise, then the render function will proceed in async
@@ -188,24 +153,16 @@ print.shiny.render.function <- function(x, ...) {
#' @return An annotated render function, ready to be assigned to an
#' `output` slot.
#'
#' @seealso [quoToFunction()], [markRenderFunction()].
#'
#' @examples
#' # A custom render function that repeats the supplied value 3 times
#' renderTriple <- function(expr) {
#' # Wrap user-supplied reactive expression into a function
#' func <- quoToFunction(rlang::enquo0(expr))
#'
#' createRenderFunction(
#' func,
#' transform = function(value, session, name, ...) {
#' paste(rep(value, 3), collapse=", ")
#' },
#' outputFunc = textOutput
#' )
#' }
#'
#' # For better legacy support, consider using installExprFunction() over quoToFunction()
#' renderTripleLegacy <- function(expr, env = parent.frame(), quoted = FALSE) {
#' func <- installExprFunction(expr, "func", env, quoted)
#' # A very simple render function
#' renderTriple <- function(x) {
#' x <- substitute(x)
#' if (!rlang::is_quosure(x)) {
#' x <- rlang::new_quosure(x, env = parent.frame())
#' }
#' func <- quoToFunction(x, "renderTriple")
#'
#' createRenderFunction(
#' func,
@@ -217,38 +174,10 @@ print.shiny.render.function <- function(x, ...) {
#' }
#'
#' # Test render function from the console
#' reactiveConsole(TRUE)
#'
#' v <- reactiveVal("basic")
#' r <- renderTriple({ v() })
#' a <- 1
#' r <- renderTriple({ a + 1 })
#' a <- 2
#' r()
#' #> [1] "basic, basic, basic"
#'
#' # User can supply quoted code via rlang::quo(). Note that evaluation of the
#' # expression happens when r2() is invoked, not when r2 is created.
#' q <- rlang::quo({ v() })
#' r2 <- rlang::inject(renderTriple(!!q))
#' v("rlang")
#' r2()
#' #> [1] "rlang, rlang, rlang"
#'
#' # Supplying quoted code without rlang::quo() requires installExprFunction()
#' expr <- quote({ v() })
#' r3 <- renderTripleLegacy(expr, quoted = TRUE)
#' v("legacy")
#' r3()
#' #> [1] "legacy, legacy, legacy"
#'
#' # The legacy approach also supports with quosures (env is ignored in this case)
#' q <- rlang::quo({ v() })
#' r4 <- renderTripleLegacy(q, quoted = TRUE)
#' v("legacy-rlang")
#' r4()
#' #> [1] "legacy-rlang, legacy-rlang, legacy-rlang"
#'
#' # Turn off reactivity in the console
#' reactiveConsole(FALSE)
#'
#' @export
createRenderFunction <- function(
func,
@@ -387,7 +316,9 @@ markOutputAttrs <- function(renderFunc, snapshotExclude = NULL,
#' the output, see [plotPNG()].
#'
#' @param expr An expression that returns a list.
#' @inheritParams renderUI
#' @param env The environment in which to evaluate `expr`.
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
#' is useful if you want to save an expression in a variable.
#' @param deleteFile Should the file in `func()$src` be deleted after
#' it is sent to the client browser? Generally speaking, if the image is a
#' temp file generated within `func`, then this should be `TRUE`;
@@ -466,10 +397,11 @@ markOutputAttrs <- function(renderFunc, snapshotExclude = NULL,
#'
#' shinyApp(ui, server)
#' }
renderImage <- function(expr, env = parent.frame(), quoted = FALSE,
renderImage <- function(expr, env=parent.frame(), quoted=FALSE,
deleteFile, outputArgs=list())
{
func <- installExprFunction(expr, "func", env, quoted, label = "renderImage")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderImage")
# missing() must be used directly within the function with the given arg
if (missing(deleteFile)) {
@@ -591,7 +523,9 @@ isTemp <- function(path, tempDir = tempdir(), mustExist) {
#' function return [invisible()].
#'
#' @param expr An expression to evaluate.
#' @inheritParams renderUI
#' @param env The environment in which to evaluate `expr`. For expert use only.
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
#' is useful if you want to save an expression in a variable.
#' @param width Width of printed output.
#' @param outputArgs A list of arguments to be passed through to the implicit
#' call to [verbatimTextOutput()] or [textOutput()] when the functions are
@@ -602,7 +536,8 @@ isTemp <- function(path, tempDir = tempdir(), mustExist) {
renderPrint <- function(expr, env = parent.frame(), quoted = FALSE,
width = getOption('width'), outputArgs=list())
{
func <- installExprFunction(expr, "func", env, quoted, label = "renderPrint")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderPrint")
# Set a promise domain that sets the console width
# and captures output
@@ -634,7 +569,7 @@ renderPrint <- function(expr, env = parent.frame(), quoted = FALSE,
outputArgs,
cacheHint = list(
label = "renderPrint",
origUserExpr = installedFuncExpr(func)
origUserExpr = get_expr(expr)
)
)
}
@@ -684,10 +619,11 @@ createRenderPrintPromiseDomain <- function(width) {
#' element.
#' @export
#' @rdname renderPrint
renderText <- function(expr, env = parent.frame(), quoted = FALSE,
renderText <- function(expr, env=parent.frame(), quoted=FALSE,
outputArgs=list(), sep=" ") {
func <- installExprFunction(expr, "func", env, quoted, label = "renderText")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderText")
createRenderFunction(
func,
@@ -708,13 +644,9 @@ renderText <- function(expr, env = parent.frame(), quoted = FALSE,
#'
#' @param expr An expression that returns a Shiny tag object, [HTML()],
#' or a list of such objects.
#' @template param-env
#' @templateVar x expr
#' @templateVar env env
#' @templateVar quoted quoted
#' @template param-quoted
#' @templateVar x expr
#' @templateVar quoted quoted
#' @param env The environment in which to evaluate `expr`.
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
#' is useful if you want to save an expression in a variable.
#' @param outputArgs A list of arguments to be passed through to the implicit
#' call to [uiOutput()] when `renderUI` is used in an
#' interactive R Markdown document.
@@ -743,7 +675,8 @@ renderText <- function(expr, env = parent.frame(), quoted = FALSE,
renderUI <- function(expr, env = parent.frame(), quoted = FALSE,
outputArgs = list())
{
func <- installExprFunction(expr, "func", env, quoted, label = "renderUI")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderUI")
createRenderFunction(
func,
@@ -822,8 +755,6 @@ downloadHandler <- function(filename, content, contentType=NA, outputArgs=list()
#' Table output with the JavaScript DataTables library
#'
#' @description
#' `r lifecycle::badge("superseded")` Please use [`DT::renderDataTable()`]. (Shiny 0.11.1)
#'
#' Makes a reactive version of the given function that returns a data frame (or
#' matrix), which will be rendered with the [DataTables](https://datatables.net)
#' library. Paging, searching, filtering, and sorting can be done on the R side
@@ -898,7 +829,8 @@ renderDataTable <- function(expr, options = NULL, searchDelay = 500,
)
}
func <- installExprFunction(expr, "func", env, quoted, label = "renderDataTable")
expr <- get_quosure(expr, env, quoted)
func <- quoToFunction(expr, "renderDataTable")
renderFunc <- function(shinysession, name, ...) {
if (is.function(options)) options <- options()

View File

@@ -51,199 +51,60 @@ formalsAndBody <- function(x) {
}
#' @describeIn createRenderFunction convert a quosure to a function.
#' @param q Quosure of the expression `x`. When capturing expressions to create
#' your quosure, it is recommended to use [`enquo0()`] to not unquote the
#' object too early. See [`enquo0()`] for more details.
#' @inheritParams installExprFunction
#' @export
quoToFunction <- function(
q,
label = sys.call(-1)[[1]],
..stacktraceon = FALSE
) {
func <- quoToSimpleFunction(as_quosure(q))
wrapFunctionLabel(func, updateFunctionLabel(label), ..stacktraceon = ..stacktraceon, dots = FALSE)
}
updateFunctionLabel <- function(label) {
badFnName <- "anonymous"
if (all(is.language(label))) {
# Prevent immediately invoked functions like as.language(a()())
if (is.language(label) && length(label) > 1) {
return(badFnName)
# This function is to be called from functions like `reactive()`, `observe()`,
# and the various render functions. It handles the following cases:
# - The typical case where x is an unquoted expression, and `env` and `quoted`
# are not used.
# - New-style metaprogramming cases, where rlang::inject() is used to inline a
# quosure into the AST, as in `inject(reactive(!!x))`.
# - Old-style metaprogramming cases, where `env` and/or `quoted` are used.
#
# Much of the complexity is handling old-style metaprogramming cases. The code
# in this function is more complicated because it needs to look at unevaluated
# expressions in the _calling_ function. If this code were put directly in the
# calling function, it would look like this:
#
# if (!missing(env) || !missing(quoted)) {
# deprecatedEnvQuotedMessage()
# if (!quoted) x <- substitute(x)
# x <- new_quosure(x, env)
#
# } else {
# x <- substitute(x)
# if (!is_quosure(x)) {
# x <- new_quosure(x, env = parent.frame())
# }
# }
#
# In the future, the calling functions will not need to have the `env` and
# `quoted` arguments -- `rlang::inject()` and quosures can be used instead.
# Instead of using this function, `get_quosure()`, the caller can instead use
# just the following code:
#
# x <- substitute(x)
# if (!is_quosure(x)) {
# x <- new_quosure(x, env = parent.frame())
# }
#
get_quosure <- function(x, env, quoted) {
if (!eval(substitute(missing(env)), parent.frame()) ||
!eval(substitute(missing(quoted)), parent.frame()))
{
deprecatedEnvQuotedMessage()
if (!quoted) {
x <- eval(substitute(substitute(x)), parent.frame())
}
label <- deparse(label, width.cutoff = 500L)
}
label <- as.character(label)
# Prevent function calls that are over one line; (Assignments are hard to perform)
# Prevent immediately invoked functions like "a()()"
if (length(label) > 1 || grepl("(", label, fixed = TRUE)) {
return(badFnName)
}
if (label == "NULL") {
return(badFnName)
}
label
}
x <- new_quosure(x, env)
quoToSimpleFunction <- function(q) {
# Should not use `new_function(list(), get_expr(q), get_env(q))` as extra logic
# is done by rlang to convert the quosure to a function within `as_function(q)`
fun <- as_function(q)
} else {
x <- eval(substitute(substitute(x)), parent.frame())
# If the quosure is empty, then the returned function can not be called.
# https://github.com/r-lib/rlang/issues/1244
if (quo_is_missing(q)) {
fn_body(fun) <- quote({})
}
# `as_function()` returns a function that takes `...`. We need one that takes no
# args.
fn_fmls(fun) <- list()
fun
}
#' Convert an expression to a function
#'
#' `r lifecycle::badge("superseded")` Please use [`installExprFunction()`] for a better
#' debugging experience (Shiny 0.8.0). If the `expr` and `quoted` parameters are not needed, please see
#' [`quoToFunction()`] (Shiny 1.6.0).
#'
#' Similar to [installExprFunction()] but doesn't register debug hooks.
#'
#' @param expr A quoted or unquoted expression, or a quosure.
#' @param env The desired environment for the function. Defaults to the
#' calling environment two steps back.
#' @param quoted Is the expression quoted?
#' @seealso [`installExprFunction()`] for the modern approach to converting an expression to a function
#' @export
#' @keywords internal
exprToFunction <- function(expr, env = parent.frame(), quoted = FALSE) {
# If `expr` is a raw quosure, must say `quoted = TRUE`; (env is ignored)
# If `inject()` a quosure, env is ignored, and quoted should be FALSE (aka ignored).
# Make article of usage
# * (by joe)
if (!quoted) {
expr <- eval(substitute(substitute(expr)), parent.frame())
}
# MUST call with `quoted = TRUE` as exprToQuo() will not reach high enough
q <- exprToQuo(expr, env, quoted = TRUE)
# MUST call `as_function()`. Can NOT call `new_function()`
# rlang has custom logic for handling converting a quosure to a function
quoToSimpleFunction(q)
}
# For internal use only; External users should be using `exprToFunction()` or `installExprFunction()`
# MUST be the exact same logic as `exprToFunction()`, but without the `quoToSimpleFunction()` call
exprToQuo <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) {
expr <- eval(substitute(substitute(expr)), parent.frame())
}
q <-
if (is_quosure(expr)) {
# inject()ed quosure
# do nothing
expr
} else if (is.language(expr) || rlang::is_atomic(expr) || is.null(expr)) {
# Most common case...
new_quosure(expr, env = env)
} else {
stop("Don't know how to convert '", class(expr)[1], "' to a function; a quosure or quoted expression was expected")
# At this point, x can be a quosure if rlang::inject() is used, but the
# typical case is that x is not a quosure.
if (!is_quosure(x)) {
x <- new_quosure(x, env = parent.frame(2L))
}
q
}
#' @describeIn createRenderFunction converts a user's reactive `expr` into a
#' function that's assigned to a `name` in the `assign.env`.
#'
#' @param name The name the function should be given
#' @param eval.env The desired environment for the function. Defaults to the
#' calling environment two steps back.
#' @param assign.env The environment in which the function should be assigned.
#' @param label A label for the object to be shown in the debugger. Defaults to
#' the name of the calling function.
#' @param wrappedWithLabel,..stacktraceon Advanced use only. For stack manipulation purposes; see
#' [stacktrace()].
#' @inheritParams exprToFunction
#' @export
installExprFunction <- function(expr, name, eval.env = parent.frame(2),
quoted = FALSE,
assign.env = parent.frame(1),
label = sys.call(-1)[[1]],
wrappedWithLabel = TRUE,
..stacktraceon = FALSE) {
if (!quoted) {
quoted <- TRUE
expr <- eval(substitute(substitute(expr)), parent.frame())
}
func <- exprToFunction(expr, eval.env, quoted)
if (length(label) > 1) {
# Just in case the deparsed code is more complicated than we imagine. If we
# have a label with length > 1 it causes warnings in wrapFunctionLabel.
label <- paste0(label, collapse = "\n")
}
wrappedWithLabel <- isTRUE(wrappedWithLabel)
if (wrappedWithLabel) {
func <- wrapFunctionLabel(func, updateFunctionLabel(label), ..stacktraceon = ..stacktraceon, dots = FALSE)
}
assign(name, func, envir = assign.env)
if (!wrappedWithLabel) {
registerDebugHook(name, assign.env, label)
}
invisible(func)
}
# Utility function for creating a debugging label, given an expression.
# `expr` is a quoted expression.
# `function_name` is the name of the calling function.
# `label` is an optional user-provided label. If NULL, it will be inferred.
exprToLabel <- function(expr, function_name, label = NULL) {
srcref <- attr(expr, "srcref", exact = TRUE)
if (is.null(label)) {
label <- rexprSrcrefToLabel(
srcref[[1]],
simpleExprToFunction(expr, function_name)
)
}
if (length(srcref) >= 2) attr(label, "srcref") <- srcref[[2]]
attr(label, "srcfile") <- srcFileOfRef(srcref[[1]])
label
}
simpleExprToFunction <- function(expr, function_name) {
sprintf('%s(%s)', function_name, paste(deparse(expr), collapse='\n'))
}
installedFuncExpr <- function(func) {
fn_body(attr(func, "wrappedFunc", exact = TRUE))
}
funcToLabelBody <- function(func) {
paste(deparse(installedFuncExpr(func)), collapse='\n')
}
funcToLabel <- function(func, functionLabel, label = NULL) {
if (!is.null(label)) return(label)
sprintf(
'%s(%s)',
functionLabel,
funcToLabelBody(func)
)
}
quoToLabelBody <- function(q) {
paste(deparse(quo_get_expr(q)), collapse='\n')
}
quoToLabel <- function(q, functionLabel, label = NULL) {
if (!is.null(label)) return(label)
sprintf(
'%s(%s)',
functionLabel,
quoToLabelBody(q)
)
x
}

185
R/utils.R
View File

@@ -404,6 +404,165 @@ getContentType <- function(file, defaultType = 'application/octet-stream') {
mime::guess_type(file, unknown = defaultType, subtype = subtype)
}
# Create a zero-arg function from a quoted expression and environment
# @examples
# makeFunction(body=quote(print(3)))
makeFunction <- function(args = pairlist(), body, env = parent.frame()) {
eval(call("function", args, body), env)
}
#' Convert an expression to a function
#'
#' This is to be called from another function, because it will attempt to get
#' an unquoted expression from two calls back. Note: as of Shiny 1.6.0, it is
#' recommended to use [quoToFunction()] instead.
#'
#' If expr is a quoted expression, then this just converts it to a function.
#' If expr is a function, then this simply returns expr (and prints a
#' deprecation message).
#' If expr was a non-quoted expression from two calls back, then this will
#' quote the original expression and convert it to a function.
#
#' @param expr A quoted or unquoted expression, or a function.
#' @param env The desired environment for the function. Defaults to the
#' calling environment two steps back.
#' @param quoted Is the expression quoted?
#'
#' @examples
#' # Example of a new renderer, similar to renderText
#' # This is something that toolkit authors will do
#' renderTriple <- function(expr, env=parent.frame(), quoted=FALSE) {
#' # Convert expr to a function
#' func <- shiny::exprToFunction(expr, env, quoted)
#'
#' function() {
#' value <- func()
#' paste(rep(value, 3), collapse=", ")
#' }
#' }
#'
#'
#' # Example of using the renderer.
#' # This is something that app authors will do.
#' values <- reactiveValues(A="text")
#'
#' \dontrun{
#' # Create an output object
#' output$tripleA <- renderTriple({
#' values$A
#' })
#' }
#'
#' # At the R console, you can experiment with the renderer using isolate()
#' tripleA <- renderTriple({
#' values$A
#' })
#'
#' isolate(tripleA())
#' # "text, text, text"
#' @export
exprToFunction <- function(expr, env=parent.frame(), quoted=FALSE) {
if (!quoted) {
expr <- eval(substitute(substitute(expr)), parent.frame())
}
# expr is a quoted expression
makeFunction(body=expr, env=env)
}
#' Install an expression as a function
#'
#' Installs an expression in the given environment as a function, and registers
#' debug hooks so that breakpoints may be set in the function. Note: as of
#' Shiny 1.6.0, it is recommended to use [quoToFunction()] instead.
#'
#' This function can replace `exprToFunction` as follows: we may use
#' `func <- exprToFunction(expr)` if we do not want the debug hooks, or
#' `installExprFunction(expr, "func")` if we do. Both approaches create a
#' function named `func` in the current environment.
#'
#' @seealso Wraps [exprToFunction()]; see that method's documentation
#' for more documentation and examples.
#'
#' @param expr A quoted or unquoted expression
#' @param name The name the function should be given
#' @param eval.env The desired environment for the function. Defaults to the
#' calling environment two steps back.
#' @param quoted Is the expression quoted?
#' @param assign.env The environment in which the function should be assigned.
#' @param label A label for the object to be shown in the debugger. Defaults to
#' the name of the calling function.
#' @param wrappedWithLabel,..stacktraceon Advanced use only. For stack manipulation purposes; see
#' [stacktrace()].
#' @export
installExprFunction <- function(expr, name, eval.env = parent.frame(2),
quoted = FALSE,
assign.env = parent.frame(1),
label = deparse(sys.call(-1)[[1]]),
wrappedWithLabel = TRUE,
..stacktraceon = FALSE) {
if (!quoted) {
quoted <- TRUE
expr <- eval(substitute(substitute(expr)), parent.frame())
}
func <- exprToFunction(expr, eval.env, quoted)
if (length(label) > 1) {
# Just in case the deparsed code is more complicated than we imagine. If we
# have a label with length > 1 it causes warnings in wrapFunctionLabel.
label <- paste0(label, collapse = "\n")
}
if (wrappedWithLabel) {
func <- wrapFunctionLabel(func, label, ..stacktraceon = ..stacktraceon)
} else {
registerDebugHook(name, assign.env, label)
}
assign(name, func, envir = assign.env)
}
#' Convert a quosure to a function for a Shiny render function
#'
#' This takes a quosure and label, and wraps them into a function that should be
#' passed to [createRenderFunction()] or [markRenderFunction()].
#'
#' This function was added in Shiny 1.6.0. Previously, it was recommended to use
#' [installExprFunction()] or [exprToFunction()] in render functions, but now we
#' recommend using [quoToFunction()], because it does not require `env` and
#' `quoted` arguments -- that information is captured by quosures provided by
#' \pkg{rlang}.
#'
#' @param q A quosure.
#' @inheritParams installExprFunction
#' @seealso [createRenderFunction()] for example usage.
#'
#' @export
quoToFunction <- function(q, label, ..stacktraceon = FALSE) {
q <- as_quosure(q)
func <- as_function(q)
# as_function returns a function that takes `...`. We want one that takes no
# args.
formals(func) <- list()
wrapFunctionLabel(func, label, ..stacktraceon = ..stacktraceon)
}
# Utility function for creating a debugging label, given an expression.
# `expr` is a quoted expression.
# `function_name` is the name of the calling function.
# `label` is an optional user-provided label. If NULL, it will be inferred.
exprToLabel <- function(expr, function_name, label = NULL) {
srcref <- attr(expr, "srcref", exact = TRUE)
if (is.null(label)) {
label <- rexprSrcrefToLabel(
srcref[[1]],
sprintf('%s(%s)', function_name, paste(deparse(expr), collapse = '\n'))
)
}
if (length(srcref) >= 2) attr(label, "srcref") <- srcref[[2]]
attr(label, "srcfile") <- srcFileOfRef(srcref[[1]])
label
}
#' Parse a GET query string from a URL
#'
#' Returns a named list of key-value pairs.
@@ -1000,7 +1159,7 @@ reactiveStop <- function(message = "", class = NULL) {
#'
#' ui <- fluidPage(
#' checkboxGroupInput('in1', 'Check some letters', choices = head(LETTERS)),
#' selectizeInput('in2', 'Select a state', choices = c("", state.name)),
#' selectizeInput('in2', 'Select a state', choices = state.name),
#' plotOutput('plot')
#' )
#'
@@ -1438,31 +1597,21 @@ dateYMD <- function(date = NULL, argName = "value") {
# function which calls the original function using the specified name. This can
# be helpful for profiling, because the specified name will show up on the stack
# trace.
wrapFunctionLabel <- function(func, name, ..stacktraceon = FALSE, dots = TRUE) {
wrapFunctionLabel <- function(func, name, ..stacktraceon = FALSE) {
if (name == "name" || name == "func" || name == "relabelWrapper") {
stop("Invalid name for wrapFunctionLabel: ", name)
}
assign(name, func, environment())
registerDebugHook(name, environment(), name)
if (isTRUE(dots)) {
if (..stacktraceon) {
# We need to wrap the `...` in `!!quote(...)` so that R CMD check won't
# complain about "... may be used in an incorrect context"
body <- expr({ ..stacktraceon..((!!name)(!!quote(...))) })
} else {
body <- expr({ (!!name)(!!quote(...)) })
}
relabelWrapper <- new_function(pairlist2(... =), body, environment())
if (..stacktraceon) {
# We need to wrap the `...` in `!!quote(...)` so that R CMD check won't
# complain about "... may be used in an incorrect context"
body <- expr({ ..stacktraceon..((!!name)(!!quote(...))) })
} else {
# Same logic as when `dots = TRUE`, but without the `...`
if (..stacktraceon) {
body <- expr({ ..stacktraceon..((!!name)()) })
} else {
body <- expr({ (!!name)() })
}
relabelWrapper <- new_function(list(), body, environment())
body <- expr({ (!!name)(!!quote(...)) })
}
relabelWrapper <- new_function(pairlist2(... =), body, environment())
# Preserve the original function that was passed in; is used for caching.
attr(relabelWrapper, "wrappedFunc") <- func

View File

@@ -58,7 +58,3 @@ We welcome contributions to the **shiny** package. Please see our [CONTRIBUTING.
## License
The shiny package as a whole is licensed under the GPLv3. See the [LICENSE](LICENSE) file for more details.
## R version support
Shiny is supported on the latest release version of R, as well as the previous four minor release versions of R. For example, if the latest release R version is 4.1, then that version is supported, as well as 4.0, 3.6, 3.5, and 3.4.

View File

@@ -13,13 +13,6 @@
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
/* https://github.com/rstudio/shiny/issues/3443 */
/* https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ */
box-sizing: border-box;
}
.irs *, .irs *:before, .irs *:after {
box-sizing: inherit;
}
.irs-line {
@@ -179,9 +172,9 @@
.irs--shiny .irs-bar {
top: 25px;
height: 8px;
border-top: 1px solid #428bca;
border-bottom: 1px solid #428bca;
background: #428bca;
border-top: 1px solid #337ab7;
border-bottom: 1px solid #337ab7;
background: #337ab7;
}
.irs--shiny .irs-bar--single {
@@ -235,7 +228,7 @@
color: #fff;
text-shadow: none;
padding: 1px 3px;
background-color: #428bca;
background-color: #337ab7;
border-radius: 3px;
font-size: 11px;
line-height: 1.333;

View File

@@ -4,12 +4,6 @@
@include pos-r();
-webkit-touch-callout: none;
@include no-click();
/* https://github.com/rstudio/shiny/issues/3443 */
/* https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ */
box-sizing: border-box;
*, *:before, *:after {
box-sizing: inherit;
}
&-line {
@include pos-r();

View File

@@ -37,7 +37,7 @@ $font-family: $font-family-base !default;
// "High-level" coloring
$bg: $body-bg !default;
$fg: color-contrast($body-bg) !default;
$accent: #428bca !default;
$accent: $component-active-bg !default;
// "Low-level" coloring, borders, and fonts
$line_bg: linear-gradient(to bottom, mix($bg, $fg, 87%) -50%, $bg 150%) !default;

View File

@@ -1,3 +1,17 @@
/*! shiny 1.6.0.9022 | (c) 2012-2021 RStudio, PBC. | License: GPL-3 | file LICENSE */
(function(){var t="ws:";window.location.protocol==="https:"&&(t="wss:");var o=window.location.pathname;/\/$/.test(o)||(o+="/");o+="autoreload/";var n=new WebSocket(t+"//"+window.location.host+o);n.onmessage=function(a){a.data==="autoreload"&&window.location.reload()};})();
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vc3JjdHMvZXh0cmFzL3NoaW55LWF1dG9yZWxvYWQudHMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbIi8qIGVzbGludC1kaXNhYmxlIHVuaWNvcm4vZmlsZW5hbWUtY2FzZSAqL1xudmFyIHByb3RvY29sID0gXCJ3czpcIjtcbmlmICh3aW5kb3cubG9jYXRpb24ucHJvdG9jb2wgPT09IFwiaHR0cHM6XCIpIHByb3RvY29sID0gXCJ3c3M6XCI7XG52YXIgZGVmYXVsdFBhdGggPSB3aW5kb3cubG9jYXRpb24ucGF0aG5hbWU7XG5pZiAoIS9cXC8kLy50ZXN0KGRlZmF1bHRQYXRoKSkgZGVmYXVsdFBhdGggKz0gXCIvXCI7XG5kZWZhdWx0UGF0aCArPSBcImF1dG9yZWxvYWQvXCI7XG52YXIgd3MgPSBuZXcgV2ViU29ja2V0KHByb3RvY29sICsgXCIvL1wiICsgd2luZG93LmxvY2F0aW9uLmhvc3QgKyBkZWZhdWx0UGF0aCk7XG5cbndzLm9ubWVzc2FnZSA9IGZ1bmN0aW9uIChldmVudCkge1xuICBpZiAoZXZlbnQuZGF0YSA9PT0gXCJhdXRvcmVsb2FkXCIpIHtcbiAgICB3aW5kb3cubG9jYXRpb24ucmVsb2FkKCk7XG4gIH1cbn07XG5cbmV4cG9ydCB7fTsiXSwKICAibWFwcGluZ3MiOiAiO1lBQ0EsR0FBSSxHQUFXLE1BQ2YsQUFBSSxPQUFPLFNBQVMsV0FBYSxVQUFVLEdBQVcsUUFDdEQsR0FBSSxHQUFjLE9BQU8sU0FBUyxTQUNsQyxBQUFLLE1BQU0sS0FBSyxJQUFjLElBQWUsS0FDN0MsR0FBZSxjQUNmLEdBQUksR0FBSyxHQUFJLFdBQVUsRUFBVyxLQUFPLE9BQU8sU0FBUyxLQUFPLEdBRWhFLEVBQUcsVUFBWSxTQUFVLEVBQU8sQ0FDOUIsQUFBSSxFQUFNLE9BQVMsY0FDakIsT0FBTyxTQUFTIiwKICAibmFtZXMiOiBbXQp9Cg==
(function() {
var protocol = 'ws:';
if (window.location.protocol === 'https:')
protocol = 'wss:';
var defaultPath = window.location.pathname;
if (!/\/$/.test(defaultPath))
defaultPath += '/';
defaultPath += 'autoreload/';
var ws = new WebSocket(protocol + '//' + window.location.host + defaultPath);
ws.onmessage = function(event) {
if (event.data === "autoreload") {
window.location.reload()
}
}
})();

View File

@@ -1,2 +1,87 @@
/*! shiny 1.6.0.9022 | (c) 2012-2021 RStudio, PBC. | License: GPL-3 | file LICENSE */
#showcase-well{border-radius:0}.shiny-code{background-color:#fff;margin-bottom:0}.shiny-code code{font-family:Menlo,Consolas,"Courier New",monospace}.shiny-code-container{margin-top:20px;clear:both}.shiny-code-container h3{display:inline;margin-right:15px}.showcase-header{font-size:16px;font-weight:normal}.showcase-code-link{text-align:right;padding:15px}#showcase-app-container{vertical-align:top}#showcase-code-tabs{margin-right:15px}#showcase-code-tabs pre{border:none;line-height:1em}#showcase-code-tabs .nav{margin-bottom:0}#showcase-code-tabs ul{margin-bottom:0}#showcase-code-tabs .tab-content{border-style:solid;border-color:#e5e5e5;border-width:0px 1px 1px 1px;overflow:auto;border-bottom-right-radius:4px;border-bottom-left-radius:4px}#showcase-app-code{width:100%}#showcase-code-position-toggle{float:right}#showcase-sxs-code{padding-top:20px;vertical-align:top}.showcase-code-license{display:block;text-align:right}#showcase-code-content pre{background-color:#fff}
#showcase-well {
border-radius: 0;
-webkit-border-radius: 0;
-moz-border-radius: 0;
}
.shiny-code {
background-color: white;
margin-bottom: 0;
}
.shiny-code code {
font-family: Menlo, Consolas, "Courier New", monospace;
}
.shiny-code-container {
margin-top: 20px;
clear: both;
}
.shiny-code-container h3 {
display: inline;
margin-right: 15px;
}
.showcase-header {
font-size: 16px;
font-weight: normal;
}
.showcase-code-link {
text-align: right;
padding: 15px;
}
#showcase-app-container {
vertical-align: top;
}
#showcase-code-tabs pre {
border: none;
line-height: 1em;
}
#showcase-code-tabs .nav,
#showcase-code-tabs ul {
margin-bottom: 0px;
}
#showcase-app-code {
width: 100%;
}
#showcase-code-tabs {
margin-right: 15px;
}
#showcase-code-tabs .tab-content {
border-style: solid;
border-color: #e5e5e5;
border-width: 0px 1px 1px 1px;
overflow:auto;
-webkit-border-bottom-right-radius: 4px;
-webkit-border-bottom-left-radius: 4px;
-moz-border-radius-bottomright: 4px;
-moz-border-radius-bottomleft: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
}
#showcase-code-position-toggle {
float: right;
}
#showcase-sxs-code {
padding-top: 20px;
vertical-align: top;
}
.showcase-code-license {
display: block;
text-align: right;
}
#showcase-code-content pre {
background-color: white;
}

File diff suppressed because one or more lines are too long

View File

@@ -1,3 +1,8 @@
/*! shiny 1.6.0.9022 | (c) 2012-2021 RStudio, PBC. | License: GPL-3 | file LICENSE */
(function(){var a=eval;window.addEventListener("message",function(i){var e=i.data;e.code&&a(e.code)});})();
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vc3JjdHMvc3JjL3V0aWxzL2V2YWwudHMiLCAiLi4vLi4vLi4vc3JjdHMvZXh0cmFzL3NoaW55LXRlc3Rtb2RlLnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyIvL2VzYnVpbGQuZ2l0aHViLmlvL2NvbnRlbnQtdHlwZXMvI2RpcmVjdC1ldmFsXG4vL3RsL2RyO1xuLy8gKiBEaXJlY3QgdXNhZ2Ugb2YgYGV2YWwoXCJ4XCIpYCBpcyBiYWQgd2l0aCBidW5kbGVkIGNvZGUuXG4vLyAqIEluc3RlYWQsIHVzZSBpbmRpcmVjdCBjYWxscyB0byBgZXZhbGAgc3VjaCBhcyBgaW5kaXJlY3RFdmFsKFwieFwiKWBcbi8vICAgKiBFdmVuIGp1c3QgcmVuYW1pbmcgdGhlIGZ1bmN0aW9uIHdvcmtzIHdlbGwgZW5vdWdoLlxuLy8gPiBUaGlzIGlzIGtub3duIGFzIFwiaW5kaXJlY3QgZXZhbFwiIGJlY2F1c2UgZXZhbCBpcyBub3QgYmVpbmcgY2FsbGVkIGRpcmVjdGx5LCBhbmQgc28gZG9lcyBub3QgdHJpZ2dlciB0aGUgZ3JhbW1hdGljYWwgc3BlY2lhbCBjYXNlIGZvciBkaXJlY3QgZXZhbCBpbiB0aGUgSmF2YVNjcmlwdCBWTS4gWW91IGNhbiBjYWxsIGluZGlyZWN0IGV2YWwgdXNpbmcgYW55IHN5bnRheCBhdCBhbGwgZXhjZXB0IGZvciBhbiBleHByZXNzaW9uIG9mIHRoZSBleGFjdCBmb3JtIGV2YWwoJ3gnKS4gRm9yIGV4YW1wbGUsIHZhciBldmFsMiA9IGV2YWw7IGV2YWwyKCd4JykgYW5kIFtldmFsXVswXSgneCcpIGFuZCB3aW5kb3cuZXZhbCgneCcpIGFyZSBhbGwgaW5kaXJlY3QgZXZhbCBjYWxscy5cbi8vID4gV2hlbiB5b3UgdXNlIGluZGlyZWN0IGV2YWwsIHRoZSBjb2RlIGlzIGV2YWx1YXRlZCBpbiB0aGUgZ2xvYmFsIHNjb3BlIGluc3RlYWQgb2YgaW4gdGhlIGlubGluZSBzY29wZSBvZiB0aGUgY2FsbGVyLlxudmFyIGluZGlyZWN0RXZhbCA9IGV2YWw7XG5leHBvcnQgeyBpbmRpcmVjdEV2YWwgfTsiLCAiLyogZXNsaW50LWRpc2FibGUgdW5pY29ybi9maWxlbmFtZS1jYXNlICovXG5pbXBvcnQgeyBpbmRpcmVjdEV2YWwgfSBmcm9tIFwiLi4vc3JjL3V0aWxzL2V2YWxcIjsgLy8gTGlzdGVuIGZvciBtZXNzYWdlcyBmcm9tIHBhcmVudCBmcmFtZS4gVGhpcyBmaWxlIGlzIG9ubHkgYWRkZWQgd2hlbiB0aGVcbi8vIHNoaW55LnRlc3Rtb2RlIG9wdGlvbiBpcyBUUlVFLlxuXG53aW5kb3cuYWRkRXZlbnRMaXN0ZW5lcihcIm1lc3NhZ2VcIiwgZnVuY3Rpb24gKGUpIHtcbiAgdmFyIG1lc3NhZ2UgPSBlLmRhdGE7XG4gIGlmIChtZXNzYWdlLmNvZGUpIGluZGlyZWN0RXZhbChtZXNzYWdlLmNvZGUpO1xufSk7Il0sCiAgIm1hcHBpbmdzIjogIjtZQU9BLEdBQUksR0FBZSxLQ0huQixPQUFPLGlCQUFpQixVQUFXLFNBQVUsRUFBRyxDQUM5QyxHQUFJLEdBQVUsRUFBRSxLQUNoQixBQUFJLEVBQVEsTUFBTSxFQUFhLEVBQVEiLAogICJuYW1lcyI6IFtdCn0K
// Listen for messages from parent frame. This file is only added when the
// shiny.testmode option is TRUE.
window.addEventListener("message", function(e) {
var message = e.data;
if (message.code)
eval(message.code);
});

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +0,0 @@
# Also update observeEvent param descriptions!
# https://github.com/r-lib/roxygen2/issues/1241
#' @param <%= env %> The parent environment for the reactive expression. By default,
#' this is the calling environment, the same as when defining an ordinary
#' non-reactive expression. If `<%= x %>` is a quosure and `<%= quoted %>` is `TRUE`,
#' then `<%= env %>` is ignored.

View File

@@ -1,6 +0,0 @@
# Also update observeEvent param descriptions!
# https://github.com/r-lib/roxygen2/issues/1241
#' @param <%= quoted %> If it is `TRUE`, then the [`quote()`]ed value of `<%= x %>`
#' will be used when `<%= x %>` is evaluated. If `<%= x %>` is a quosure and you
#' would like to use its expression as a value for `<%= x %>`, then you must set
#' `<%= quoted %>` to `TRUE`.

View File

@@ -275,11 +275,11 @@ render function for cache collisions in a real application.
In some cases, however, the automatic cache hint inference is not
sufficient, and it is necessary to provide a cache hint. This is true
for \code{renderPrint()}. Unlike \code{renderText()}, it wraps the user-provided
expression in another function, before passing it to \code{\link[=createRenderFunction]{createRenderFunction()}}
expression in another function, before passing it to \code{\link[=markRenderFunction]{markRenderFunction()}}
(instead of \code{\link[=createRenderFunction]{createRenderFunction()}}). Because the user code is wrapped in
another function, \code{createRenderFunction()} is not able to automatically
another function, \code{markRenderFunction()} is not able to automatically
extract the user-provided code and use it in the cache key. Instead,
\code{renderPrint} calls \code{createRenderFunction()}, it explicitly passes along a
\code{renderPrint} calls \code{markRenderFunction()}, it explicitly passes along a
\code{cacheHint}, which includes a label and the original user expression.
In general, if you need to provide a \code{cacheHint}, it is best practice to
@@ -290,19 +290,19 @@ For \pkg{htmlwidgets}, it will try to automatically infer a cache hint;
again, you can inspect the cache hint with \code{shiny:::extractCacheHint()} and
also test it in an application. If you do need to explicitly provide a
cache hint, pass it to \code{shinyRenderWidget}. For example:\preformatted{renderMyWidget <- function(expr) \{
q <- rlang::enquo0(expr)
expr <- substitute(expr)
htmlwidgets::shinyRenderWidget(
q,
htmlwidgets::shinyRenderWidget(expr,
myWidgetOutput,
quoted = TRUE,
cacheHint = list(label = "myWidget", userQuo = q)
env = parent.frame(),
cacheHint = list(label = "myWidget", userExpr = expr)
)
\}
}
If your \code{render} function sets any internal state, you may find it useful
in your call to \code{\link[=createRenderFunction]{createRenderFunction()}} to use
in your call to \code{\link[=createRenderFunction]{createRenderFunction()}} or \code{\link[=markRenderFunction]{markRenderFunction()}} to use
the \code{cacheWriteHook} and/or \code{cacheReadHook} parameters. These hooks are
functions that run just before the object is stored in the cache, and just
after the object is retrieved from the cache. They can modify the data
@@ -321,8 +321,8 @@ Some render functions cannot be cached, typically because they have side
effects or modify some external state, and they must re-execute each time
in order to work properly.
For developers of such code, they should call \code{\link[=createRenderFunction]{createRenderFunction()}} (or
\code{\link[=markRenderFunction]{markRenderFunction()}}) with \code{cacheHint = FALSE}.
For developers of such code, they should call \code{\link[=createRenderFunction]{createRenderFunction()}} or
\code{\link[=markRenderFunction]{markRenderFunction()}} with \code{cacheHint = FALSE}.
}
\section{Caching with \code{renderPlot()}}{

View File

@@ -49,3 +49,4 @@ These functions give control over the \code{click}, \code{dblClick} and
\seealso{
\code{\link[=brushOpts]{brushOpts()}} for brushing events.
}
\keyword{internal}

View File

@@ -1,10 +1,8 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/shinywrappers.R, R/utils-lang.R
% Please edit documentation in R/shinywrappers.R
\name{createRenderFunction}
\alias{createRenderFunction}
\alias{quoToFunction}
\alias{installExprFunction}
\title{Implement custom render functions}
\title{Implement render functions}
\usage{
createRenderFunction(
func,
@@ -15,19 +13,6 @@ createRenderFunction(
cacheWriteHook = NULL,
cacheReadHook = NULL
)
quoToFunction(q, label = sys.call(-1)[[1]], ..stacktraceon = FALSE)
installExprFunction(
expr,
name,
eval.env = parent.frame(2),
quoted = FALSE,
assign.env = parent.frame(1),
label = sys.call(-1)[[1]],
wrappedWithLabel = TRUE,
..stacktraceon = FALSE
)
}
\arguments{
\item{func}{A function without parameters, that returns user data. If the
@@ -70,85 +55,23 @@ argument, the value retrieved from the cache. This can be useful when some
side effect needs to occur for a render function to behave correctly. For
example, some render functions call \code{\link[=createWebDependency]{createWebDependency()}} so that Shiny
is able to serve JS and CSS resources.}
\item{q}{Quosure of the expression \code{x}. When capturing expressions to create
your quosure, it is recommended to use \code{\link[=enquo0]{enquo0()}} to not unquote the
object too early. See \code{\link[=enquo0]{enquo0()}} for more details.}
\item{label}{A label for the object to be shown in the debugger. Defaults to
the name of the calling function.}
\item{expr}{A quoted or unquoted expression, or a quosure.}
\item{name}{The name the function should be given}
\item{eval.env}{The desired environment for the function. Defaults to the
calling environment two steps back.}
\item{quoted}{Is the expression quoted?}
\item{assign.env}{The environment in which the function should be assigned.}
\item{wrappedWithLabel, ..stacktraceon}{Advanced use only. For stack manipulation purposes; see
\code{\link[=stacktrace]{stacktrace()}}.}
}
\value{
An annotated render function, ready to be assigned to an
\code{output} slot.
}
\description{
Developer-facing utilities for implementing a custom \code{renderXXX()} function.
Before using these utilities directly, consider using the \href{http://www.htmlwidgets.org/develop_intro.html}{\code{htmlwidgets} package} to implement custom
outputs (i.e., custom \code{renderXXX()}/\code{xxxOutput()} functions). That said,
these utilities can be used more directly if a full-blown htmlwidget isn't
needed and/or the user-supplied reactive expression needs to be wrapped in
additional call(s).
This function is a wrapper for \code{\link[=markRenderFunction]{markRenderFunction()}} which provides support
for async computation via promises.
}
\details{
To implement a custom \code{renderXXX()} function, essentially 2 things are needed:
\enumerate{
\item Capture the user's reactive expression as a function.
\itemize{
\item New \code{renderXXX()} functions can use \code{quoToFunction()} for this, but
already existing \code{renderXXX()} functions that contain \code{env} and \code{quoted}
parameters may want to continue using \code{installExprFunction()} for better
legacy support (see examples).
}
\item Flag the resulting function (from 1) as a Shiny rendering function and
also provide a UI container for displaying the result of the rendering
function.
\itemize{
\item \code{createRenderFunction()} is currently recommended (instead of
\code{\link[=markRenderFunction]{markRenderFunction()}}) for this step (see examples).
}
}
}
\section{Functions}{
\itemize{
\item \code{quoToFunction}: convert a quosure to a function.
\item \code{installExprFunction}: converts a user's reactive \code{expr} into a
function that's assigned to a \code{name} in the \code{assign.env}.
}}
\examples{
# A custom render function that repeats the supplied value 3 times
renderTriple <- function(expr) {
# Wrap user-supplied reactive expression into a function
func <- quoToFunction(rlang::enquo0(expr))
createRenderFunction(
func,
transform = function(value, session, name, ...) {
paste(rep(value, 3), collapse=", ")
},
outputFunc = textOutput
)
}
# For better legacy support, consider using installExprFunction() over quoToFunction()
renderTripleLegacy <- function(expr, env = parent.frame(), quoted = FALSE) {
func <- installExprFunction(expr, "func", env, quoted)
# A very simple render function
renderTriple <- function(x) {
x <- substitute(x)
if (!rlang::is_quosure(x)) {
x <- rlang::new_quosure(x, env = parent.frame())
}
func <- quoToFunction(x, "renderTriple")
createRenderFunction(
func,
@@ -160,36 +83,11 @@ renderTripleLegacy <- function(expr, env = parent.frame(), quoted = FALSE) {
}
# Test render function from the console
reactiveConsole(TRUE)
v <- reactiveVal("basic")
r <- renderTriple({ v() })
a <- 1
r <- renderTriple({ a + 1 })
a <- 2
r()
#> [1] "basic, basic, basic"
# User can supply quoted code via rlang::quo(). Note that evaluation of the
# expression happens when r2() is invoked, not when r2 is created.
q <- rlang::quo({ v() })
r2 <- rlang::inject(renderTriple(!!q))
v("rlang")
r2()
#> [1] "rlang, rlang, rlang"
# Supplying quoted code without rlang::quo() requires installExprFunction()
expr <- quote({ v() })
r3 <- renderTripleLegacy(expr, quoted = TRUE)
v("legacy")
r3()
#> [1] "legacy, legacy, legacy"
# The legacy approach also supports with quosures (env is ignored in this case)
q <- rlang::quo({ v() })
r4 <- renderTripleLegacy(q, quoted = TRUE)
v("legacy-rlang")
r4()
#> [1] "legacy-rlang, legacy-rlang, legacy-rlang"
# Turn off reactivity in the console
reactiveConsole(FALSE)
}
\seealso{
\code{\link[=quoToFunction]{quoToFunction()}}, \code{\link[=markRenderFunction]{markRenderFunction()}}.
}

View File

@@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils-lang.R
% Please edit documentation in R/utils.R
\name{exprToFunction}
\alias{exprToFunction}
\title{Convert an expression to a function}
@@ -7,7 +7,7 @@
exprToFunction(expr, env = parent.frame(), quoted = FALSE)
}
\arguments{
\item{expr}{A quoted or unquoted expression, or a quosure.}
\item{expr}{A quoted or unquoted expression, or a function.}
\item{env}{The desired environment for the function. Defaults to the
calling environment two steps back.}
@@ -15,14 +15,47 @@ calling environment two steps back.}
\item{quoted}{Is the expression quoted?}
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} Please use \code{\link[=installExprFunction]{installExprFunction()}} for a better
debugging experience (Shiny 0.8.0). If the \code{expr} and \code{quoted} parameters are not needed, please see
\code{\link[=quoToFunction]{quoToFunction()}} (Shiny 1.6.0).
This is to be called from another function, because it will attempt to get
an unquoted expression from two calls back. Note: as of Shiny 1.6.0, it is
recommended to use \code{\link[=quoToFunction]{quoToFunction()}} instead.
}
\details{
Similar to \code{\link[=installExprFunction]{installExprFunction()}} but doesn't register debug hooks.
If expr is a quoted expression, then this just converts it to a function.
If expr is a function, then this simply returns expr (and prints a
deprecation message).
If expr was a non-quoted expression from two calls back, then this will
quote the original expression and convert it to a function.
}
\seealso{
\code{\link[=installExprFunction]{installExprFunction()}} for the modern approach to converting an expression to a function
\examples{
# Example of a new renderer, similar to renderText
# This is something that toolkit authors will do
renderTriple <- function(expr, env=parent.frame(), quoted=FALSE) {
# Convert expr to a function
func <- shiny::exprToFunction(expr, env, quoted)
function() {
value <- func()
paste(rep(value, 3), collapse=", ")
}
}
# Example of using the renderer.
# This is something that app authors will do.
values <- reactiveValues(A="text")
\dontrun{
# Create an output object
output$tripleA <- renderTriple({
values$A
})
}
# At the R console, you can experiment with the renderer using isolate()
tripleA <- renderTriple({
values$A
})
isolate(tripleA())
# "text, text, text"
}
\keyword{internal}

View File

@@ -11,7 +11,7 @@ insertTab(
inputId,
tab,
target = NULL,
position = c("after", "before"),
position = c("before", "after"),
select = FALSE,
session = getDefaultReactiveDomain()
)

View File

@@ -0,0 +1,50 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{installExprFunction}
\alias{installExprFunction}
\title{Install an expression as a function}
\usage{
installExprFunction(
expr,
name,
eval.env = parent.frame(2),
quoted = FALSE,
assign.env = parent.frame(1),
label = deparse(sys.call(-1)[[1]]),
wrappedWithLabel = TRUE,
..stacktraceon = FALSE
)
}
\arguments{
\item{expr}{A quoted or unquoted expression}
\item{name}{The name the function should be given}
\item{eval.env}{The desired environment for the function. Defaults to the
calling environment two steps back.}
\item{quoted}{Is the expression quoted?}
\item{assign.env}{The environment in which the function should be assigned.}
\item{label}{A label for the object to be shown in the debugger. Defaults to
the name of the calling function.}
\item{wrappedWithLabel, ..stacktraceon}{Advanced use only. For stack manipulation purposes; see
\code{\link[=stacktrace]{stacktrace()}}.}
}
\description{
Installs an expression in the given environment as a function, and registers
debug hooks so that breakpoints may be set in the function. Note: as of
Shiny 1.6.0, it is recommended to use \code{\link[=quoToFunction]{quoToFunction()}} instead.
}
\details{
This function can replace \code{exprToFunction} as follows: we may use
\code{func <- exprToFunction(expr)} if we do not want the debug hooks, or
\code{installExprFunction(expr, "func")} if we do. Both approaches create a
function named \code{func} in the current environment.
}
\seealso{
Wraps \code{\link[=exprToFunction]{exprToFunction()}}; see that method's documentation
for more documentation and examples.
}

View File

@@ -24,4 +24,3 @@ knit_print.reactive(x, ..., inline = FALSE)
These S3 methods are necessary to help Shiny applications and UI chunks embed
themselves in knitr/rmarkdown documents.
}
\keyword{internal}

View File

@@ -51,24 +51,12 @@ is able to serve JS and CSS resources.}
The \code{renderFunc} function, with annotations.
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} Please use \code{\link[=createRenderFunction]{createRenderFunction()}} to
support async execution. (Shiny 1.1.0)
}
\details{
Should be called by implementers of \code{renderXXX} functions in order to mark
their return values as Shiny render functions, and to provide a hint to Shiny
regarding what UI function is most commonly used with this type of render
function. This can be used in R Markdown documents to create complete output
widgets out of just the render function.
Note that it is generally preferable to use \code{\link[=createRenderFunction]{createRenderFunction()}} instead
of \code{markRenderFunction()}. It essentially wraps up the user-provided
expression in the \code{transform} function passed to it, then passes the resulting
function to \code{markRenderFunction()}. It also provides a simpler calling
interface. There may be cases where \code{markRenderFunction()} must be used instead of
\code{\link[=createRenderFunction]{createRenderFunction()}} -- for example, when the \code{transform} parameter of
\code{\link[=createRenderFunction]{createRenderFunction()}} is not flexible enough for your needs.
}
\seealso{
\code{\link[=createRenderFunction]{createRenderFunction()}}
\code{\link[=createRenderFunction]{createRenderFunction()}}, \code{\link[=quoToFunction]{quoToFunction()}}
}

View File

@@ -23,13 +23,11 @@ ignored.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{x} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
non-reactive expression.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{x}
will be used when \code{x} is evaluated. If \code{x} is a quosure and you
would like to use its expression as a value for \code{x}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is the expression quoted? By default, this is \code{FALSE}.
This is useful when you want to use an expression that is stored in a
variable; to do so, it must be quoted with \code{quote()}.}
\item{...}{Not used.}
@@ -118,12 +116,12 @@ obsB <- observe({
print(values$A + 1)
})
# To store expressions for later conversion to observe, use rlang::quo()
myquo <- rlang::quo({ print(values$A + 3) })
obsC <- rlang::inject(observe(!!myquo))
# Can use quoted expressions
obsC <- observe(quote({ print(values$A + 2) }), quoted = TRUE)
# (Legacy) Can use quoted expressions
obsD <- observe(quote({ print(values$A + 2) }), quoted = TRUE)
# To store expressions for later conversion to observe, use quote()
expr_q <- quote({ print(values$A + 3) })
obsD <- observe(expr_q, quoted = TRUE)
# In a normal Shiny app, the web client will trigger flush events. If you
# are at the console, you can force a flush with flushReact()

View File

@@ -48,25 +48,21 @@ invalidated. This should be a side-effect-producing action (the return
value will be ignored). It will be executed within an \code{\link[=isolate]{isolate()}}
scope.}
\item{event.env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{eventExpr} is a quosure and \code{event.quoted} is \code{TRUE},
then \code{event.env} is ignored.}
\item{event.env}{The parent environment for \code{eventExpr}. By default,
this is the calling environment.}
\item{event.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{eventExpr}
will be used when \code{eventExpr} is evaluated. If \code{eventExpr} is a quosure and you
would like to use its expression as a value for \code{eventExpr}, then you must set
\code{event.quoted} to \code{TRUE}.}
\item{event.quoted}{Is the \code{eventExpr} expression quoted? By default,
this is \code{FALSE}. This is useful when you want to use an expression
that is stored in a variable; to do so, it must be quoted with
\code{quote()}.}
\item{handler.env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{handlerExpr} is a quosure and \code{handler.quoted} is \code{TRUE},
then \code{handler.env} is ignored.}
\item{handler.env}{The parent environment for \code{handlerExpr}. By default,
this is the calling environment.}
\item{handler.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{handlerExpr}
will be used when \code{handlerExpr} is evaluated. If \code{handlerExpr} is a quosure and you
would like to use its expression as a value for \code{handlerExpr}, then you must set
\code{handler.quoted} to \code{TRUE}.}
\item{handler.quoted}{Is the \code{handlerExpr} expression quoted? By
default, this is \code{FALSE}. This is useful when you want to use an
expression that is stored in a variable; to do so, it must be quoted with
\code{quote()}.}
\item{...}{Currently not used.}
@@ -103,15 +99,12 @@ happen once.}
\code{eventReactive}. It will be executed within an \code{\link[=isolate]{isolate()}}
scope.}
\item{value.env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{valueExpr} is a quosure and \code{value.quoted} is \code{TRUE},
then \code{value.env} is ignored.}
\item{value.env}{The parent environment for \code{valueExpr}. By default,
this is the calling environment.}
\item{value.quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{valueExpr}
will be used when \code{valueExpr} is evaluated. If \code{valueExpr} is a quosure and you
would like to use its expression as a value for \code{valueExpr}, then you must set
\code{value.quoted} to \code{TRUE}.}
\item{value.quoted}{Is the \code{valueExpr} expression quoted? By default,
this is \code{FALSE}. This is useful when you want to use an expression
that is stored in a variable; to do so, it must be quoted with \code{quote()}.}
}
\value{
\code{observeEvent} returns an observer reference class object (see

31
man/quoToFunction.Rd Normal file
View File

@@ -0,0 +1,31 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{quoToFunction}
\alias{quoToFunction}
\title{Convert a quosure to a function for a Shiny render function}
\usage{
quoToFunction(q, label, ..stacktraceon = FALSE)
}
\arguments{
\item{q}{A quosure.}
\item{label}{A label for the object to be shown in the debugger. Defaults to
the name of the calling function.}
\item{..stacktraceon}{Advanced use only. For stack manipulation purposes; see
\code{\link[=stacktrace]{stacktrace()}}.}
}
\description{
This takes a quosure and label, and wraps them into a function that should be
passed to \code{\link[=createRenderFunction]{createRenderFunction()}} or \code{\link[=markRenderFunction]{markRenderFunction()}}.
}
\details{
This function was added in Shiny 1.6.0. Previously, it was recommended to use
\code{\link[=installExprFunction]{installExprFunction()}} or \code{\link[=exprToFunction]{exprToFunction()}} in render functions, but now we
recommend using \code{\link[=quoToFunction]{quoToFunction()}}, because it does not require \code{env} and
\code{quoted} arguments -- that information is captured by quosures provided by
\pkg{rlang}.
}
\seealso{
\code{\link[=createRenderFunction]{createRenderFunction()}} for example usage.
}

View File

@@ -18,17 +18,16 @@ reactive(
is.reactive(x)
}
\arguments{
\item{x}{For \code{is.reactive()}, an object to test. For \code{reactive()}, an expression. When passing in a \code{\link[=quo]{quo()}}sure with \code{reactive()}, remember to use \code{\link[rlang:inject]{rlang::inject()}} to distinguish that you are passing in the content of your quosure, not the expression of the quosure.}
\item{x}{For \code{reactive}, an expression (quoted or unquoted). For
\code{is.reactive}, an object to test.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{x} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
non-reactive expression.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{x}
will be used when \code{x} is evaluated. If \code{x} is a quosure and you
would like to use its expression as a value for \code{x}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is the expression quoted? By default, this is \code{FALSE}.
This is useful when you want to use an expression that is stored in a
variable; to do so, it must be quoted with \code{quote()}.}
\item{...}{Not used.}
@@ -59,32 +58,21 @@ See the \href{https://shiny.rstudio.com/tutorial/}{Shiny tutorial} for
more information about reactive expressions.
}
\examples{
library(rlang)
values <- reactiveValues(A=1)
reactiveB <- reactive({
values$A + 1
})
# View the values from the R console with isolate()
isolate(reactiveB())
# 2
# Can use quoted expressions
reactiveC <- reactive(quote({ values$A + 2 }), quoted = TRUE)
# To store expressions for later conversion to reactive, use quote()
myquo <- rlang::quo(values$A + 2)
# Unexpected value! Sending a quosure directly will not work as expected.
reactiveC <- reactive(myquo)
# We'd hope for `3`, but instead we get the quosure that was supplied.
expr_q <- quote({ values$A + 3 })
reactiveD <- reactive(expr_q, quoted = TRUE)
# View the values from the R console with isolate()
isolate(reactiveB())
isolate(reactiveC())
# Instead, the quosure should be `rlang::inject()`ed
reactiveD <- rlang::inject(reactive(!!myquo))
isolate(reactiveD())
# 3
# (Legacy) Can use quoted expressions
expr <- quote({ values$A + 3 })
reactiveE <- reactive(expr, quoted = TRUE)
isolate(reactiveE())
# 4
}

View File

@@ -58,5 +58,5 @@ getType: function(el) {
}
}
\seealso{
\code{\link[=removeInputHandler]{removeInputHandler()}} \code{\link[=applyInputHandlers]{applyInputHandlers()}}
\code{\link[=removeInputHandler]{removeInputHandler()}}
}

View File

@@ -48,23 +48,16 @@ indicate which columns to escape, e.g. \code{1:5} (the first 5 columns),
\code{c(1, 3, 4)}, or \code{c(-1, -3)} (all columns except the first and
third), or \code{c('Species', 'Sepal.Length')}.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})?
This is useful if you want to save an expression in a variable.}
\item{outputArgs}{A list of arguments to be passed through to the implicit
call to \code{dataTableOutput()} when \code{renderDataTable()} is used
in an interactive R Markdown document.}
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} Please use \code{\link[DT:renderDataTable]{DT::renderDataTable()}}. (Shiny 0.11.1)
Makes a reactive version of the given function that returns a data frame (or
matrix), which will be rendered with the \href{https://datatables.net}{DataTables}
library. Paging, searching, filtering, and sorting can be done on the R side

View File

@@ -15,15 +15,10 @@ renderImage(
\arguments{
\item{expr}{An expression that returns a list.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})? This
is useful if you want to save an expression in a variable.}
\item{deleteFile}{Should the file in \code{func()$src} be deleted after
it is sent to the client browser? Generally speaking, if the image is a

View File

@@ -50,15 +50,10 @@ ggplot objects; for other plots, \code{NA} results in alt text of "Plot object".
\code{NULL} or \code{""} is not recommended because those should be limited to
decorative images.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})? This
is useful if you want to save an expression in a variable.}
\item{execOnResize}{If \code{FALSE} (the default), then when a plot is
resized, Shiny will \emph{replay} the plot drawing commands with

View File

@@ -24,15 +24,10 @@ renderText(
\arguments{
\item{expr}{An expression to evaluate.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}. For expert use only.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})? This
is useful if you want to save an expression in a variable.}
\item{width}{Width of printed output.}

View File

@@ -71,15 +71,10 @@ columns will be displayed in scientific format with a precision of
\item{...}{Arguments to be passed through to \code{\link[xtable:xtable]{xtable::xtable()}}
and \code{\link[xtable:print.xtable]{xtable::print.xtable()}}.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})?
This is useful if you want to save an expression in a variable.}
\item{outputArgs}{A list of arguments to be passed through to the
implicit call to \code{\link[=tableOutput]{tableOutput()}} when \code{renderTable} is

View File

@@ -10,15 +10,10 @@ renderUI(expr, env = parent.frame(), quoted = FALSE, outputArgs = list())
\item{expr}{An expression that returns a Shiny tag object, \code{\link[=HTML]{HTML()}},
or a list of such objects.}
\item{env}{The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If \code{expr} is a quosure and \code{quoted} is \code{TRUE},
then \code{env} is ignored.}
\item{env}{The environment in which to evaluate \code{expr}.}
\item{quoted}{If it is \code{TRUE}, then the \code{\link[=quote]{quote()}}ed value of \code{expr}
will be used when \code{expr} is evaluated. If \code{expr} is a quosure and you
would like to use its expression as a value for \code{expr}, then you must set
\code{quoted} to \code{TRUE}.}
\item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})? This
is useful if you want to save an expression in a variable.}
\item{outputArgs}{A list of arguments to be passed through to the implicit
call to \code{\link[=uiOutput]{uiOutput()}} when \code{renderUI} is used in an

View File

@@ -30,10 +30,7 @@ expression that produces a Shiny app object.
\item{port}{The TCP port that the application should listen on. If the
\code{port} is not specified, and the \code{shiny.port} option is set (with
\code{options(shiny.port = XX)}), then that port will be used. Otherwise,
use a random port between 3000:8000, excluding ports that are blocked
by Google Chrome for being considered unsafe: 3659, 4045, 5060,
5061, 6000, 6566, 6665:6669 and 6697. Up to twenty random
ports will be tried.}
use a random port.}
\item{launch.browser}{If true, the system's default web browser will be
launched automatically after the app is started. Defaults to true in

View File

@@ -19,10 +19,7 @@ list the available examples.}
\item{port}{The TCP port that the application should listen on. If the
\code{port} is not specified, and the \code{shiny.port} option is set (with
\code{options(shiny.port = XX)}), then that port will be used. Otherwise,
use a random port between 3000:8000, excluding ports that are blocked
by Google Chrome for being considered unsafe: 3659, 4045, 5060,
5061, 6000, 6566, 6665:6669 and 6697. Up to twenty random
ports will be tried.}
use a random port.}
\item{launch.browser}{If true, the system's default web browser will be
launched automatically after the app is started. Defaults to true in

View File

@@ -17,3 +17,4 @@ value. The returned value will be used for the test snapshot.}
\description{
Add a function for serializing an input before bookmarking application state
}
\keyword{internal}

View File

@@ -4,13 +4,7 @@
\alias{shinyDeprecated}
\title{Print message for deprecated functions in Shiny}
\usage{
shinyDeprecated(
version,
what,
with = NULL,
details = NULL,
type = c("deprecated", "superseded")
)
shinyDeprecated(version, what, with = NULL, details = NULL)
}
\arguments{
\item{version}{Shiny version when the function was deprecated}

View File

@@ -68,7 +68,7 @@ options(device.ask.default = FALSE)
ui <- fluidPage(
checkboxGroupInput('in1', 'Check some letters', choices = head(LETTERS)),
selectizeInput('in2', 'Select a state', choices = c("", state.name)),
selectizeInput('in2', 'Select a state', choices = state.name),
plotOutput('plot')
)

View File

@@ -1,98 +1,76 @@
{
"name": "@types/shiny",
"private": true,
"homepage": "https://shiny.rstudio.com",
"repository": "github:rstudio/shiny",
"name": "@types/rstudio-shiny",
"version": "1.6.0-alpha.9022",
"license": "GPL-3.0-only",
"version": "1.6.0",
"main": "",
"browser": "",
"types": "srcts/types/extras/globalShiny.d.ts",
"files": [
"DESCRIPTION",
"LICENSE",
"NEWS.md",
"srcts/types/**/*.d.ts"
],
"types": "srcts/types/shiny/index.d.ts",
"engines": {
"node": ">= 14",
"yarn": ">= 1.22"
},
"dependencies": {
"@types/bootstrap": "3.4.0",
"@types/bootstrap-datepicker": "0.0.14",
"@types/datatables.net": "^1.10.19",
"@types/ion-rangeslider": "2.3.0",
"@types/jquery": "patch:@types/jquery@3.5.5#./srcts/patch/types-jquery.patch",
"@types/selectize": "0.12.34"
},
"devDependencies": {
"@babel/core": "^7.14.3",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/preset-env": "^7.14.2",
"@babel/preset-typescript": "^7.13.0",
"@babel/runtime": "^7.14.0",
"@deanc/esbuild-plugin-postcss": "^1.0.1",
"@testing-library/dom": "^7.31.0",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/user-event": "^13.1.9",
"@types/highlightjs": "^9.12.1",
"@types/bootstrap": "3.4.0",
"@types/bootstrap-datepicker": "0.0.14",
"@types/datatables.net": "^1.10.19",
"@types/ion-rangeslider": "2.3.0",
"@types/jest": "^26.0.23",
"@types/jqueryui": "1.12.15",
"@types/jquery": "^3.5.5",
"@types/lodash": "^4.14.170",
"@types/node": "^15.6.1",
"@types/showdown": "^1.9.3",
"@types/selectize": "0.12.34",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"autoprefixer": "^10.2.6",
"bootstrap-datepicker": "1.9.0",
"browserslist": "^4.16.6",
"core-js": "^3.13.0",
"esbuild": "^0.12.4",
"esbuild-plugin-babel": "https://github.com/schloerke/esbuild-plugin-babel#patch-2",
"esbuild-plugin-babel": "0.2.3",
"esbuild-plugin-globals": "^0.1.1",
"esbuild-plugin-sass": "^0.5.2",
"eslint": "^7.27.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-jest-dom": "^3.9.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-unicorn": "^33.0.1",
"ion-rangeslider": "2.3.1",
"jest": "^26.6.3",
"jquery": "3.6.0",
"lodash": "^4.17.21",
"madge": "^4.0.2",
"node-gyp": "^8.1.0",
"phantomjs-prebuilt": "^2.1.16",
"postcss": "^8.3.5",
"prettier": "2.3.0",
"readcontrol": "^1.0.0",
"replace": "^1.2.1",
"selectize": "0.12.4",
"strftime": "0.9.2",
"ts-jest": "^26",
"ts-node": "^10.0.0",
"type-coverage": "^2.17.5",
"typescript": "~4.1.5",
"util-inspect": "https://github.com/deecewan/browser-util-inspect#c0b4350df4378ffd743e8c36dd3898ce3992823e"
"typescript": "~4.1.5"
},
"scripts": {
"prepare": "node --eval \"1+1; // help yarn users not install the whole repo; https://github.com/yarnpkg/yarn/issues/2822#issuecomment-847360267\"",
"watch": "yarn run build_shiny --watch",
"build": "yarn run build_shiny && yarn run bundle_extras && yarn run bundle_external_libs",
"build": "yarn run build_shiny && yarn run bundle_external_libs",
"build_shiny": "yarn run checks && yarn run bundle_shiny",
"bundle_shiny": "ts-node srcts/build/shiny.ts",
"bundle_external_libs": "ts-node srcts/build/external_libs.ts",
"bundle_extras": "ts-node srcts/build/extras.ts",
"bundle_shiny": "node srcts/build/shiny.mjs",
"circular_dep_image": "madge --circular --extensions ts --image madge.svg srcts/src",
"bundle_external_libs": "node srcts/build/external_libs.mjs",
"test": "jest --coverage",
"test_phantom": "echo '\n\t!! Must manually stop phantomjs test !!\n\n' && yarn bundle_shiny && phantomjs --debug=yes ../inst/www/shared/shiny.js",
"checks": "yarn run lint && yarn run build_types && yarn run coverage && yarn run circular",
"lint": "node --eval \"console.log('linting code...')\" && eslint --fix --ext .ts srcts/src",
"build_types": "tsc -p tsconfig.json",
"coverage_detailed": "yarn type-check --detail",
"coverage": "type-coverage -p tsconfig.json --at-least 90",
"circular": "madge --circular --extensions ts srcts/src",
"circular_image": "madge --circular --extensions ts --image madge.svg srcts/src"
"lint": "eslint --fix --ext .ts srcts/src",
"typescript-check": "tsc -p tsconfig.json",
"type-check": "type-coverage -p tsconfig.json --detail --at-least 85",
"import-check": "madge --circular --extensions ts srcts/src",
"checks": "yarn run lint && yarn run typescript-check && yarn run type-check && yarn run import-check"
},
"dependencies": {
"core-js": "^3.13.0",
"lodash": "^4.17.21",
"util-inspect": "https://github.com/deecewan/browser-util-inspect#c0b4350df4378ffd743e8c36dd3898ce3992823e"
}
}

View File

@@ -1,23 +1,4 @@
# Using Shiny TypeScript Definitions
When developing TypeScript projects that use `window.Shiny`, we recommend installing the Shiny TypeScript definitions to your package. To install the latest stable definitions, call
```bash
yarn add https://github.com/rstudio/shiny\#v1.7.0
```
, matching the GitHub tag to your current the Shiny CRAN release (ex: `v1.7.0`). If you are asked to select a version of `@types/jquery`, please select the closest matching version.
This will provide a global type defintion of `Shiny`, let your IDE know that `window.Shiny` is of type `Shiny`, and declare a globally available variable `Shiny` within your project. You **should not** need to import anything. Similar to `jQuery`, it should _Just Work_<sup>TM</sup>.
When loading your compiled file, it should be loaded after `shiny.js` is loaded. If you are using an `htmlDependency()` to add your code to the page, your script will automatically be loaded after has been loaded.
----------------------------------------------------
# TypeScript build tools (Shiny Developers)
All files will be described as if the working directory is the root folder of `rstudio/shiny`, not relative to this `README.md` file.
# TypeScript build tools
## First-time setup
Shiny's TypeScript build tools use Node.js, along with [yarn](https://yarnpkg.com/) v2 to manage the JavaScript packages.
@@ -33,10 +14,9 @@ node --version
yarn --version
```
Once both are installed, run the following in the root repo directory to install the packages :
Once both are installed, run the following in this directory (`srcts/`) to install the packages :
```bash
# Sitting in `rstudio/shiny` repo
yarn install
```
@@ -67,12 +47,12 @@ If in the future you want to upgrade or add a package, run:
yarn add --dev [packagename]
```
This will automatically add the package to the dependencies in `package.json`, and it will also update the `yarn.lock` to reflect that change. If someone other than yourself does this, simply run `yarn` to update your local packages to match the new `package.json`.
This will automatically add the package to the dependencies in `./package.json`, and it will also update the `./yarn.lock` to reflect that change. If someone other than yourself does this, simply run `yarn` to update your local packages to match the new `./package.json`.
## Upgrading packages
Periodically, it's good to upgrade the packages to a recent version. There's two ways of doing this, depending on your intention:
1. Use `yarn up` to upgrade all dependencies to their latest version based on the version range specified in the package.json file (the `yarn.lock` file will be recreated as well. Yarn packages use [semantic versioning](https://yarnpkg.com/en/docs/dependency-versions), i.e. each version is writen with a maximum of 3 dot-separated numbers such that: `major.minor.patch`. For example in the version `3.1.4`, 3 is the major version number, 1 is the minor version number and 4 is the patch version number. Here are the most used operators (these appear before the version number):
1. Use `yarn up` to upgrade all dependencies to their latest version based on the version range specified in the package.json file (the `./yarn.lock` file will be recreated as well. Yarn packages use [semantic versioning](https://yarnpkg.com/en/docs/dependency-versions), i.e. each version is writen with a maximum of 3 dot-separated numbers such that: `major.minor.patch`. For example in the version `3.1.4`, 3 is the major version number, 1 is the minor version number and 4 is the patch version number. Here are the most used operators (these appear before the version number):
- `~` is for upgrades that keep the minor version the same (assuming that was specified);
@@ -82,70 +62,16 @@ Periodically, it's good to upgrade the packages to a recent version. There's two
3. To see all outdated packages, run `yarn outdated`
# TypeScript
## Learn about TypeScript
The documentation by [TypeScript](https://www.typescriptlang.org/docs/) is a solid resource to know each and every bell and whistle. Most features have examples and convey the thoughts well.
[TypeScript Deep Dive](https://basarat.gitbook.io/typescript/) is an online `bookdown`-like approach to TypeScript by "Microsoft MVP for TypeScript", Basarat Ali Syed. In his book, he goes through many examples of what you "should do", not necessarily "what is possible" like the [TypeScript docs](https://www.typescriptlang.org/docs/).
## TypeScript StyleGuide
Using the style guid from [TypeScript Deep Dive / StyleGuide](https://basarat.gitbook.io/typescript/styleguide), we extend it to have the usage be more familiar to R developers and preexisting Shiny development. The goal is to produce consistent code that can be injested quickly.
### StyleGuide
* `null` vs. `undefined`
* Do not use `x === null` unless you truly mean it.
* Safer to use _truthy_ or _falsey_ checks instead. Ex: `if (x) {}`
* `type` vs `interface`
* > Use `type` when you might need a union or intersection: `type Foo = number | { someProperty: number }`
* > Use `interface` when you want extends or implements: `interface FooBar extends Foo { bar: string;}`
* > Otherwise use whatever makes you happy that day.
* Namespace
* `PascalCase`
* Ex: `Shiny`
### Enforced (by `eslint`) StyleGuide
* Variable
* `camelCase`
* Ex: `const hello = "world`
* Class
* `PascalCase`
* Ex: `class InputBinding {}`
* Type, Interface definitions:
* `PascalCase`
* Ex: `type BindingBase = {name: string}`
* Ex: `interface ShinyEventMessage extends JQuery.Event {}`
* Enum
* `PascalCase`
* (Currently unused)
* Single vs. Double Quotes
* While the JS community has decided on single quotes, R has decided on double quotes.
* > When you can't use double quotes, try using back ticks (`).
* Annotate Arrays as `Type[]`
* Ex: `Foo[]` (vs `Array<Foo>`)
* Annotate Records as `{[key: string]: valueType}`
* Ex: `const x: {[key: string]: number} = {a: 4}`
* Ex: Extend the unknown key definition with static keys: `const x: {known: string, [key: string]: number} = {known: "yes", a: 4}`
* File Names
* `camelCase` - Enforced by `eslint`
## Config files
# Configure TypeScript
The JavaScript community likes to build many small, effective packages that do minimal work. The unfortunate side effect is needing a config file for everything.
All config files are located in the root folder to avoid opening two separate VS Code projects.
## Config files
* `.browserslistrc`
* Used with `browserslist` and `core-js` to determine which polyfills should be incorporated.
* `.eslintrc.yml`
* Used with `eslint` and `prettier` to determine how the TypeScript files should be formatted and which lint failures should cause warnings, errors, or be ignored.
* `.madgerc`
* Package used to determine if circular dependencies are found. `type` only imports are ignored as they are not included in the final bundle.
* `.prettierrc.yml`
* Used by `prettier` to know how to adjust code when a file is saved in VSCode or within `eslint`'s linting process.
* `yarnrc.yml`
@@ -156,13 +82,15 @@ All config files are located in the root folder to avoid opening two separate VS
* `"useBuiltIns": "usage"` - `core-js` polyfills are only added as they are _used_.
* `"corejs": "3.9"` - This number should match the installed `core-js` number.
* `"ignore":["node_modules/core-js"]` - The `core-js` library is directly ignored to [avoid being processed by `babel`](https://github.com/zloirock/core-js/issues/743#issuecomment-571983318).
* `esbuild.config.mjs`
* Script that will build `shiny.js` and `shiny.min.js` with their sourcemaps
* `jest.config.js`
* Used to configure [`jest` testing](https://jestjs.io/)
* `package.json`
* Contains useful scripts that can be run by `yarn` via `yarn run SCRIPTNAME`.
* The scripts described below are inteded for developer use. All other scripts are means to an end.
* `yarn run watch` - Watch `srcts/src` for changes and rebuild the JavaScript files.
* `yarn run build` - Build `shiny.js` and `shiny.min.js` in `inst/www/shared`. Both files will have a corresponding sourcemap
* `yarn run watch` - Watch `./src` for changes and rebuild the JavaScript files.
* `yarn run build` - Build `shiny.js` and `shiny.min.js` in `../inst/www/shared`. Both files will have a corresponding sourcemap
* `yarn run lint` - Fix all TypeScript lints using [`eslint`](https://eslint.org/) and [`prettier`](https://prettier.io/)
* `yarn run test` - Run all TypeScript tests
* `tsconfig.json` -
@@ -172,11 +100,13 @@ All config files are located in the root folder to avoid opening two separate VS
* `preserveConstEnums: false` - Do no preserve enum values into the final code. (If true, produces bloat / unused code)
* `isolatedModules: true` & `esModuleInterop: true` - Requested by `esbuild`. This [allows for `esbuild`](https://esbuild.github.io/content-types/#typescript) to safely compile the files in parallel
## Bundle TypeScript
[esbuild](https://esbuild.github.io/) is a build tool that (for Shiny's purposes) compiles the TypeScript into a single JavaScript file.
To run all build tasks, run:
To run all build tasks, from within the `./srcts` directory, run:
```bash
yarn build
@@ -190,10 +120,6 @@ yarn watch
Both JavaScript files will produce a sourcemap (`**.js.map`) that the browser will understand. This will help you debug Shiny's JavaScript code within the browser and point back to the original TypeScript files.
### Exported types
`./extras/globalShiny.ts` contains global declarations to define `window.Shiny`, a globally available `Shiny` variable, and a globally available `Shiny` type. This file is in a parallel folder to `./src` to avoid `Shiny` from being globally accessable within the source code. However, this file is the default type defintion when the Type definitions are installed by external developers.
### GitHub Actions
On push to the `master` branch or push to a Pull Request to the `master` branch, a GitHub Action will be run to make sure the bundled JavaScript code is up to date. If the source code does not compile to the exact same file, it will be committed an pushed back to the outdated branch. (This makes it so the full build tools are not necessary for small tweaks and comments. 🎉)
@@ -215,6 +141,13 @@ For this to work you must first install `xdotool` using your distribution's pack
```bash
find ../srcts/ | entr bash -c './node_modules/grunt/bin/grunt && xdotool search --onlyvisible --class Chrome windowfocus key ctrl+r'
``` -->
# Development in VSCode
VSCode does not like to develop TypeScript with the configuration files in a subfolder. To leverage full VSCode capabilities, it is recommended to open the `./srcts` folder as the root folder of a VSCode project. This will enable VSCode to readily find all of the configuration files.
# Updating dependencies
### `@types/jquery`
@@ -232,14 +165,3 @@ To update the version of `core-js`:
* Check if there is a newer version available by running `yarn outdated core-js`. (If there's no output, then you have the latest version.)
* Run `yarn add --dev core-js --exact`.
### External libraries
Shiny already has a handful of html dependencies that should NOT be bundled within `shiny.js`. To update the dependencies below, see the directions in in [`tools/README.md`](../tools).
* `jquery` / `@types/jquery`
* `bootstrap` / `@types/bootstrap`
* Bootstrap is not being updated anymore. Only bootstrap 3.4 will be utilized within shiny.js. To use the latest bootstrap, see [`rstudio/bslib`](https://github.com/rstudio/bslib)
* `bootstrap-datepicker` / `@types/bootstrap-datepicker`
* `ion-rangeslider` / `@types/ion-rangeslider`
* `selectize` / `@types/selectize`

View File

@@ -1,80 +0,0 @@
import {
build as esbuildBuild,
BuildIncremental,
BuildOptions,
BuildResult,
} from "esbuild";
import readcontrol from "readcontrol";
import process from "process";
import { basename } from "path";
const outDir = "./inst/www/shared/";
type ShinyDesc = { version: string; package: string; license: string };
const shinyDesc = readcontrol.readSync("./DESCRIPTION") as ShinyDesc;
const bannerTxt = [
`/*! ${shinyDesc.package} ${shinyDesc.version}`,
`(c) 2012-${new Date().getFullYear()} RStudio, PBC.`,
`License: ${shinyDesc.license} */`,
].join(" | ");
const banner = {
js: bannerTxt,
css: bannerTxt,
};
async function build(
opts: BuildOptions
): Promise<BuildIncremental | BuildResult> {
const outFileNames = opts.outfile
? [basename(opts.outfile)]
: (opts.entryPoints as string[]).map((entry) => basename(entry));
const strSizes = outFileNames.map((outFileName) => outFileName.length);
strSizes.push("shiny.min.js".length);
const strSize = Math.max(...strSizes);
const printNames = outFileNames;
for (let i = 0; i < printNames.length; i++) {
while (printNames[i].length < strSize) {
printNames[i] = printNames[i] + " ";
}
}
const onRebuild = function (error?: string) {
if (error) {
console.error(printNames.join(", "), "watch build failed:\n", error);
} else {
printNames.map((printName) => {
console.log("√ -", printName, "-", new Date().toJSON());
});
}
return;
};
let incremental = false;
let watch: false | { onRebuild: (error, result) => void } = false;
if (process.argv.length >= 3 && process.argv[2] == "--watch") {
incremental = true;
watch = {
onRebuild: onRebuild,
};
}
outFileNames.map((outFileName) => {
console.log("Building " + outFileName);
});
return esbuildBuild({
incremental: incremental,
watch: watch,
target: "es5",
...opts,
}).then((x) => {
onRebuild();
return x;
});
}
export { outDir, build, shinyDesc, banner };

View File

@@ -0,0 +1,72 @@
// This build script must be executed from the root repo directory via
// ```
// yarn build
// ```
import { readdirSync, unlinkSync, writeFileSync } from "fs";
import esbuild from "esbuild";
import globalsPlugin from "esbuild-plugin-globals";
// import process from "process";
// let watch = process.argv.length >= 3 && process.argv[2] == "--watch";
let instdir = "./inst/";
let opts = {
bundle: false,
watch: false,
target: "es5",
sourcemap: false,
};
console.log("Building datepicker");
const localeFiles = readdirSync(instdir + "www/shared/datepicker/js/locales/");
let requireFiles = localeFiles
.map(function (filename) {
return `require("./locales/${filename}");`;
})
.join("\n");
let tmpfile = instdir + "www/shared/datepicker/js/temp.js";
writeFileSync(
tmpfile,
`require("./bootstrap-datepicker.js");
${requireFiles}`
);
await esbuild.build({
...opts,
plugins: [
globalsPlugin({
jquery: "window.jQuery",
}),
],
bundle: true,
entryPoints: [tmpfile],
outfile: instdir + "www/shared/datepicker/js/bootstrap-datepicker.min.js",
external: ["jquery"],
minify: true,
});
// Clean up
unlinkSync(tmpfile);
console.log("Building ionrangeslider");
await esbuild.build({
...opts,
entryPoints: [instdir + "www/shared/ionrangeslider/js/ion.rangeSlider.js"],
outfile: instdir + "www/shared/ionrangeslider/js/ion.rangeSlider.min.js",
minify: true,
});
console.log("Building selectize");
await esbuild.build({
...opts,
entryPoints: [
instdir + "www/shared/selectize/accessibility/js/selectize-plugin-a11y.js",
],
outfile:
instdir +
"www/shared/selectize/accessibility/js/selectize-plugin-a11y.min.js",
minify: true,
});

View File

@@ -1,57 +0,0 @@
// This build script must be executed from the root repo directory via
// ```
// yarn build
// ```
import { build, outDir } from "./_build";
import { readdir, unlink, writeFile } from "fs/promises";
import globalsPlugin from "esbuild-plugin-globals";
const opts = {
bundle: false,
sourcemap: false,
};
readdir(outDir + "datepicker/js/locales/").then(async (localeFiles) => {
const requireFiles = localeFiles
.map(function (filename) {
return `require("./locales/${filename}");`;
})
.join("\n");
const tmpFile = outDir + "datepicker/js/temp.js";
await writeFile(
tmpFile,
`require("./bootstrap-datepicker.js");\n${requireFiles}`
);
await build({
...opts,
plugins: [
globalsPlugin({
jquery: "window.jQuery",
}),
],
bundle: true,
entryPoints: [tmpFile],
outfile: outDir + "datepicker/js/bootstrap-datepicker.min.js",
minify: true,
});
// Clean up
unlink(tmpFile);
});
build({
...opts,
entryPoints: [outDir + "ionrangeslider/js/ion.rangeSlider.js"],
outfile: outDir + "ionrangeslider/js/ion.rangeSlider.min.js",
minify: true,
});
build({
...opts,
entryPoints: [outDir + "selectize/accessibility/js/selectize-plugin-a11y.js"],
outfile: outDir + "selectize/accessibility/js/selectize-plugin-a11y.min.js",
minify: true,
});

View File

@@ -1,54 +0,0 @@
// This build script must be executed from the root repo directory via
// ```
// yarn build
// ```
// - TypeScript -----------------------------------------------------------
import { banner, build, outDir } from "./_build";
import babelPlugin from "esbuild-plugin-babel";
build({
bundle: true,
sourcemap: "inline",
minify: true,
plugins: [babelPlugin()],
banner: banner,
entryPoints: [
"srcts/extras/shiny-autoreload.ts",
"srcts/extras/shiny-showcase.ts",
"srcts/extras/shiny-testmode.ts",
],
outdir: outDir,
});
// - Sass -----------------------------------------------------------
import autoprefixer from "autoprefixer";
import postCssPlugin from "@deanc/esbuild-plugin-postcss";
import sassPlugin from "esbuild-plugin-sass";
const sassOpts = {
minify: true,
banner: banner,
plugins: [
sassPlugin(),
postCssPlugin({
plugins: [autoprefixer],
}),
],
};
build({
...sassOpts,
entryPoints: ["srcts/extras/shiny-showcase.scss"],
outfile: outDir + "shiny-showcase.css",
});
build({
...sassOpts,
entryPoints: [
// Must keep shiny.scss within `inst` to be able to use as htmldependency
outDir + "shiny_scss/shiny.scss",
],
outfile: outDir + "shiny.min.css",
});

72
srcts/build/shiny.mjs Normal file
View File

@@ -0,0 +1,72 @@
// This build script must be executed from the root repo directory via
// ```
// yarn build
// ```
import esbuild from "esbuild";
import babel from "esbuild-plugin-babel";
import readcontrol from "readcontrol";
import process from "process";
import globalsPlugin from "esbuild-plugin-globals";
async function buildFile(
fileName,
extraOpts = {},
strSize = "shiny.min.js".length
) {
let watch = process.argv.length >= 3 && process.argv[2] == "--watch";
let incremental = false;
let printName = fileName;
while (printName.length < strSize) {
printName = printName + " ";
}
const onRebuild = function (error, result) {
if (error) {
console.error(printName, "watch build failed:\n", error);
} else {
console.log("√ -", printName, "-", new Date().toJSON());
}
return;
};
if (watch) {
incremental = true;
watch = {
onRebuild: onRebuild,
};
}
const outdir = "./inst/www/shared/";
console.log("Building " + fileName);
await esbuild.build({
outfile: outdir + fileName,
entryPoints: ["srcts/src/index.ts"],
bundle: true,
incremental: incremental,
watch: watch,
plugins: [
globalsPlugin({
jquery: "window.jQuery",
//// Loaded dynamically. MUST use `window.strftime` within code
// strftime: "window.strftime",
}),
babel(),
],
target: "es5",
sourcemap: true,
define: {
"process.env.SHINY_VERSION": `"${
readcontrol.readSync("./DESCRIPTION").version
}"`,
},
...extraOpts,
});
onRebuild();
}
buildFile("shiny.js");
buildFile("shiny.min.js", { minify: true });

View File

@@ -1,38 +0,0 @@
// This build script must be executed from the root repo directory via
// ```
// yarn build
// ```
import { banner, build, outDir, shinyDesc } from "./_build";
import globalsPlugin from "esbuild-plugin-globals";
import babelPlugin from "esbuild-plugin-babel";
import type { BuildOptions } from "esbuild";
const opts: BuildOptions = {
entryPoints: ["srcts/src/index.ts"],
bundle: true,
sourcemap: true,
plugins: [
globalsPlugin({
jquery: "window.jQuery",
//// Loaded dynamically. MUST use `window.strftime` within code
// strftime: "window.strftime",
}),
babelPlugin(),
],
define: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"process.env.SHINY_VERSION": `"${shinyDesc.version}"`,
},
banner: banner,
};
build({
...opts,
outfile: outDir + "shiny.js",
});
build({
...opts,
outfile: outDir + "shiny.min.js",
minify: true,
});

View File

@@ -1,20 +0,0 @@
// Type definitions for @types-rstudio/shiny
// Project: Shiny <https://shiny.rstudio.com/>
// Definitions by: RStudio <https://www.rstudio.com/>
import type { Shiny as RStudioShiny } from "../src/shiny/index";
declare global {
// Tell Shiny variable globally exists
// eslint-disable-next-line @typescript-eslint/naming-convention
const Shiny: RStudioShiny;
// Tell window.Shiny exists
interface Window {
// eslint-disable-next-line @typescript-eslint/naming-convention
Shiny: RStudioShiny;
}
// Make `Shiny` a globally available type definition. (No need to import the type)
type Shiny = RStudioShiny;
}

View File

@@ -1,19 +0,0 @@
/* eslint-disable unicorn/filename-case */
let protocol = "ws:";
if (window.location.protocol === "https:") protocol = "wss:";
let defaultPath = window.location.pathname;
if (!/\/$/.test(defaultPath)) defaultPath += "/";
defaultPath += "autoreload/";
const ws = new WebSocket(protocol + "//" + window.location.host + defaultPath);
ws.onmessage = function (event) {
if (event.data === "autoreload") {
window.location.reload();
}
};
export {};

View File

@@ -1,86 +0,0 @@
#showcase-well {
border-radius: 0;
}
.shiny-code {
background-color: white;
margin-bottom: 0;
code {
font-family: Menlo, Consolas, "Courier New", monospace;
}
}
.shiny-code-container {
margin-top: 20px;
clear: both;
h3 {
display: inline;
margin-right: 15px;
}
}
.showcase-header {
font-size: 16px;
font-weight: normal;
}
.showcase-code-link {
text-align: right;
padding: 15px;
}
#showcase-app-container {
vertical-align: top;
}
#showcase-code-tabs {
pre {
border: none;
line-height: 1em;
}
.nav {
margin-bottom: 0px;
}
ul {
margin-bottom: 0px;
}
margin-right: 15px;
.tab-content {
border-style: solid;
border-color: #e5e5e5;
border-width: 0px 1px 1px 1px;
overflow: auto;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
}
}
#showcase-app-code {
width: 100%;
}
#showcase-code-position-toggle {
float: right;
}
#showcase-sxs-code {
padding-top: 20px;
vertical-align: top;
}
.showcase-code-license {
display: block;
text-align: right;
}
#showcase-code-content {
pre {
background-color: white;
}
}

View File

@@ -1,315 +0,0 @@
/* eslint-disable unicorn/filename-case */
import "./globalShiny";
type ShowcaseSrcMessage = {
srcref: number[];
srcfile: string;
};
const animateMs = 400;
// Given a DOM node and a column (count of characters), walk recursively
// through the node's siblings counting characters until the given number
// of characters have been found.
//
// If the given count is bigger than the number of characters contained by
// the node and its siblings, returns a null node and the number of
// characters found.
function findTextColPoint(node: Node, col: number) {
let cols = 0;
if (node.nodeType === 3) {
const nchar = node.nodeValue.replace(/\n/g, "").length;
if (nchar >= col) {
return { element: node, offset: col };
} else {
cols += nchar;
}
} else if (node.nodeType === 1 && node.firstChild) {
const ret = findTextColPoint(node.firstChild, col);
if (ret.element !== null) {
return ret;
} else {
cols += ret.offset;
}
}
if (node.nextSibling) return findTextColPoint(node.nextSibling, col - cols);
else return { element: null, offset: cols };
}
// Returns an object indicating the element containing the given line and
// column of text, and the offset into that element where the text was found.
//
// If the given line and column are not found, returns a null element and
// the number of lines found.
function findTextPoint(el: Node, line: number, col: number) {
let newlines = 0;
for (let childId = 0; childId < el.childNodes.length; childId++) {
const child = el.childNodes[childId];
// If this is a text node, count the number of newlines it contains.
if (child.nodeType === 3) {
// TEXT_NODE
const newlinere = /\n/g;
let match: ReturnType<RegExp["exec"]>;
while ((match = newlinere.exec(child.nodeValue)) !== null) {
newlines++;
// Found the desired line, now find the column.
if (newlines === line) {
return findTextColPoint(child, match.index + col + 1);
}
}
}
// If this is not a text node, descend recursively to see how many
// lines it contains.
else if (child.nodeType === 1) {
// ELEMENT_NODE
const ret = findTextPoint(child, line - newlines, col);
if (ret.element !== null) return ret;
else newlines += ret.offset;
}
}
return { element: null, offset: newlines };
}
// Draw a highlight effect for the given source ref. srcref is assumed to be
// an integer array of length 6, following the standard R format for source
// refs.
function highlightSrcref(
srcref: ShowcaseSrcMessage["srcref"],
srcfile: ShowcaseSrcMessage["srcfile"]
) {
// Check to see if the browser supports text ranges (IE8 doesn't)
if (!document.createRange) return;
// Check to see if we already have a marker for this source ref
let el = document.getElementById("srcref_" + srcref);
if (!el) {
// We don't have a marker, create one
el = document.createElement("span");
el.id = "srcref_" + srcref;
const ref = srcref;
const code = document.getElementById(srcfile.replace(/\./g, "_") + "_code");
// if there is no code file (might be a shiny file), quit early
if (!code) return;
const start = findTextPoint(code, ref[0], ref[4]);
const end = findTextPoint(code, ref[2], ref[5]);
// If the insertion point can't be found, bail out now
if (start.element === null || end.element === null) return;
const range = document.createRange();
// If the text points are inside different <SPAN>s, we may not be able to
// surround them without breaking apart the elements to keep the DOM tree
// intact. Just move the selection points to encompass the contents of
// the SPANs.
if (
start.element.parentNode.nodeName === "SPAN" &&
start.element !== end.element
) {
range.setStartBefore(start.element.parentNode);
} else {
range.setStart(start.element, start.offset);
}
if (
end.element.parentNode.nodeName === "SPAN" &&
start.element !== end.element
) {
range.setEndAfter(end.element.parentNode);
} else {
range.setEnd(end.element, end.offset);
}
range.surroundContents(el);
}
// End any previous highlight before starting this one
$(el).stop(true, true).effect("highlight", null, 1600);
}
// If this is the main Shiny window, wire up our custom message handler.
// TODO-barret, this should work
if (Shiny) {
Shiny.addCustomMessageHandler(
"showcase-src",
function (message: ShowcaseSrcMessage) {
if (message.srcref && message.srcfile) {
highlightSrcref(message.srcref, message.srcfile);
}
}
);
}
let isCodeAbove = false;
const setCodePosition = function (above: boolean, animate: boolean) {
const animateCodeMs = animate ? animateMs : 1;
// set the source and targets for the tab move
const newHostElement = above
? document.getElementById("showcase-sxs-code")
: document.getElementById("showcase-code-inline");
const currentHostElement = above
? document.getElementById("showcase-code-inline")
: document.getElementById("showcase-sxs-code");
const metadataElement = document.getElementById("showcase-app-metadata");
if (metadataElement === null) {
// if there's no app metadata, show and hide the entire well container
// when the code changes position
const wellElement = $("#showcase-well");
if (above) {
wellElement.fadeOut(animateCodeMs);
} else {
wellElement.fadeIn(animateCodeMs);
}
}
// hide the new element before doing anything to it
$(newHostElement).hide();
$(currentHostElement).fadeOut(animateCodeMs, function () {
const tabs = document.getElementById("showcase-code-tabs");
currentHostElement.removeChild(tabs);
newHostElement.appendChild(tabs);
// remove or set the height of the code
if (above) {
setCodeHeightFromDocHeight();
} else {
document.getElementById("showcase-code-content").removeAttribute("style");
}
$(newHostElement).fadeIn(animateCodeMs);
if (!above) {
// remove the applied width and zoom on the app container, and
// scroll smoothly down to the code's new home
document
.getElementById("showcase-app-container")
.removeAttribute("style");
if (animate)
$(document.body).animate({
scrollTop: $(newHostElement).offset().top,
});
}
// if there's a readme, move it either alongside the code or beneath
// the app
const readme = document.getElementById("readme-md");
if (readme !== null) {
readme.parentElement.removeChild(readme);
if (above) {
currentHostElement.appendChild(readme);
$(currentHostElement).fadeIn(animateCodeMs);
} else
document.getElementById("showcase-app-metadata").appendChild(readme);
}
// change the text on the toggle button to reflect the new state
document.getElementById("showcase-code-position-toggle").innerHTML = above
? '<i class="fa fa-level-down"></i> show below'
: '<i class="fa fa-level-up"></i> show with app';
});
if (above) {
$(document.body).animate({ scrollTop: 0 }, animateCodeMs);
}
isCodeAbove = above;
setAppCodeSxsWidths(above && animate);
$(window).trigger("resize");
};
function setAppCodeSxsWidths(animate: boolean) {
const appTargetWidth = 960;
let appWidth = appTargetWidth;
let zoom = 1.0;
const totalWidth = document.getElementById("showcase-app-code").offsetWidth;
if (totalWidth / 2 > appTargetWidth) {
// If the app can use only half the available space and still meet its
// target, take half the available space.
appWidth = totalWidth / 2;
} else if (totalWidth * 0.66 > appTargetWidth) {
// If the app can meet its target by taking up more space (up to 66%
// of its container), take up more space.
appWidth = 960;
} else {
// The space is too narrow for the app and code to live side-by-side
// in a friendly way. Keep the app at 2/3 of the space but scale it.
appWidth = totalWidth * 0.66;
zoom = appWidth / appTargetWidth;
}
const app = document.getElementById("showcase-app-container");
$(app).animate(
{
width: appWidth + "px",
zoom: zoom * 100 + "%",
},
animate ? animateMs : 0
);
}
const toggleCodePosition = function () {
setCodePosition(!isCodeAbove, true);
};
// if the browser is sized to wider than 1350px, show the code next to the
// app by default
const setInitialCodePosition = function () {
if (document.body.offsetWidth > 1350) {
setCodePosition(true, false);
}
};
// make the code scrollable to about the height of the browser, less space
// for the tabs
function setCodeHeightFromDocHeight() {
document.getElementById("showcase-code-content").style.height =
$(window).height() + "px";
}
// if there's a block of markdown content, render it to HTML
function renderMarkdown() {
const mdContent = document.getElementById("showcase-markdown-content");
if (mdContent !== null) {
// IE8 puts the content of <script> tags into innerHTML but
// not innerText
const content = mdContent.innerText || mdContent.innerHTML;
const showdownConverter = (window as any).Showdown
.converter as showdown.ConverterStatic;
document.getElementById("readme-md").innerHTML =
new showdownConverter().makeHtml(content);
}
}
$(window).resize(function () {
if (isCodeAbove) {
setAppCodeSxsWidths(false);
setCodeHeightFromDocHeight();
}
});
declare global {
interface Window {
toggleCodePosition: () => void;
}
}
window.toggleCodePosition = toggleCodePosition;
$(window).on("load", setInitialCodePosition);
$(window).on("load", renderMarkdown);
if (window.hljs) window.hljs.initHighlightingOnLoad();
export {};

View File

@@ -1,10 +0,0 @@
/* eslint-disable unicorn/filename-case */
import { indirectEval } from "../src/utils/eval";
// Listen for messages from parent frame. This file is only added when the
// shiny.testmode option is TRUE.
window.addEventListener("message", function (e: { data: { code: string } }) {
const message = e.data;
if (message.code) indirectEval(message.code);
});

View File

@@ -1,17 +0,0 @@
(function() {
var protocol = 'ws:';
if (window.location.protocol === 'https:')
protocol = 'wss:';
var defaultPath = window.location.pathname;
if (!/\/$/.test(defaultPath))
defaultPath += '/';
defaultPath += 'autoreload/';
var ws = new WebSocket(protocol + '//' + window.location.host + defaultPath);
ws.onmessage = function(event) {
if (event.data === "autoreload") {
window.location.reload()
}
}
})();

View File

@@ -1,87 +0,0 @@
#showcase-well {
border-radius: 0;
-webkit-border-radius: 0;
-moz-border-radius: 0;
}
.shiny-code {
background-color: white;
margin-bottom: 0;
}
.shiny-code code {
font-family: Menlo, Consolas, "Courier New", monospace;
}
.shiny-code-container {
margin-top: 20px;
clear: both;
}
.shiny-code-container h3 {
display: inline;
margin-right: 15px;
}
.showcase-header {
font-size: 16px;
font-weight: normal;
}
.showcase-code-link {
text-align: right;
padding: 15px;
}
#showcase-app-container {
vertical-align: top;
}
#showcase-code-tabs pre {
border: none;
line-height: 1em;
}
#showcase-code-tabs .nav,
#showcase-code-tabs ul {
margin-bottom: 0px;
}
#showcase-app-code {
width: 100%;
}
#showcase-code-tabs {
margin-right: 15px;
}
#showcase-code-tabs .tab-content {
border-style: solid;
border-color: #e5e5e5;
border-width: 0px 1px 1px 1px;
overflow:auto;
-webkit-border-bottom-right-radius: 4px;
-webkit-border-bottom-left-radius: 4px;
-moz-border-radius-bottomright: 4px;
-moz-border-radius-bottomleft: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
}
#showcase-code-position-toggle {
float: right;
}
#showcase-sxs-code {
padding-top: 20px;
vertical-align: top;
}
.showcase-code-license {
display: block;
text-align: right;
}
#showcase-code-content pre {
background-color: white;
}

View File

@@ -1,272 +0,0 @@
/*jshint browser:true, jquery:true, strict:false, curly:false, indent:2*/
(function() {
var animateMs = 400;
// Given a DOM node and a column (count of characters), walk recursively
// through the node's siblings counting characters until the given number
// of characters have been found.
//
// If the given count is bigger than the number of characters contained by
// the node and its siblings, returns a null node and the number of
// characters found.
function findTextColPoint(node, col) {
var cols = 0;
if (node.nodeType === 3) {
var nchar = node.nodeValue.replace(/\n/g, "").length;
if (nchar >= col) {
return { element: node, offset: col };
} else {
cols += nchar;
}
} else if (node.nodeType === 1 && node.firstChild) {
var ret = findTextColPoint(node.firstChild, col);
if (ret.element !== null) {
return ret;
} else {
cols += ret.offset;
}
}
if (node.nextSibling)
return findTextColPoint(node.nextSibling, col - cols);
else
return { element: null, offset: cols };
}
// Returns an object indicating the element containing the given line and
// column of text, and the offset into that element where the text was found.
//
// If the given line and column are not found, returns a null element and
// the number of lines found.
function findTextPoint(el, line, col) {
var newlines = 0;
for (var childId = 0; childId < el.childNodes.length; childId++) {
var child = el.childNodes[childId];
// If this is a text node, count the number of newlines it contains.
if (child.nodeType === 3) { // TEXT_NODE
var newlinere = /\n/g;
var match;
while ((match = newlinere.exec(child.nodeValue)) !== null) {
newlines++;
// Found the desired line, now find the column.
if (newlines === line) {
return findTextColPoint(child, match.index + col + 1);
}
}
}
// If this is not a text node, descend recursively to see how many
// lines it contains.
else if (child.nodeType === 1) { // ELEMENT_NODE
var ret = findTextPoint(child, line - newlines, col);
if (ret.element !== null)
return ret;
else
newlines += ret.offset;
}
}
return { element: null, offset: newlines };
}
// Draw a highlight effect for the given source ref. srcref is assumed to be
// an integer array of length 6, following the standard R format for source
// refs.
function highlightSrcref (srcref, srcfile) {
// Check to see if the browser supports text ranges (IE8 doesn't)
if (!document.createRange)
return;
// Check to see if we already have a marker for this source ref
var el = document.getElementById("srcref_" + srcref);
if (!el) {
// We don't have a marker, create one
el = document.createElement("span");
el.id = "srcref_" + srcref;
var ref = srcref;
var code = document.getElementById(srcfile.replace(/\./g, "_") + "_code");
// if there is no code file (might be a shiny file), quit early
if (!code) return;
var start = findTextPoint(code, ref[0], ref[4]);
var end = findTextPoint(code, ref[2], ref[5]);
// If the insertion point can't be found, bail out now
if (start.element === null || end.element === null)
return;
var range = document.createRange();
// If the text points are inside different <SPAN>s, we may not be able to
// surround them without breaking apart the elements to keep the DOM tree
// intact. Just move the selection points to encompass the contents of
// the SPANs.
if (start.element.parentNode.nodeName === "SPAN" &&
start.element !== end.element) {
range.setStartBefore(start.element.parentNode);
} else {
range.setStart(start.element, start.offset);
}
if (end.element.parentNode.nodeName === "SPAN" &&
start.element !== end.element) {
range.setEndAfter(end.element.parentNode);
} else {
range.setEnd(end.element, end.offset);
}
range.surroundContents(el);
}
// End any previous highlight before starting this one
jQuery(el)
.stop(true, true)
.effect("highlight", null, 1600);
}
// If this is the main Shiny window, wire up our custom message handler.
if (window.Shiny) {
Shiny.addCustomMessageHandler('showcase-src', function(message) {
if (message.srcref && message.srcfile) {
highlightSrcref(message.srcref, message.srcfile);
}
});
}
var isCodeAbove = false;
var setCodePosition = function(above, animate) {
var animateCodeMs = animate ? animateMs : 1;
// set the source and targets for the tab move
var newHostElement = above ?
document.getElementById("showcase-sxs-code") :
document.getElementById("showcase-code-inline");
var currentHostElement = above ?
document.getElementById("showcase-code-inline") :
document.getElementById("showcase-sxs-code");
var metadataElement = document.getElementById("showcase-app-metadata");
if (metadataElement === null) {
// if there's no app metadata, show and hide the entire well container
// when the code changes position
var wellElement = $("#showcase-well");
if (above) {
wellElement.fadeOut(animateCodeMs);
} else {
wellElement.fadeIn(animateCodeMs);
}
}
// hide the new element before doing anything to it
$(newHostElement).hide();
$(currentHostElement).fadeOut(animateCodeMs, function() {
var tabs = document.getElementById("showcase-code-tabs");
currentHostElement.removeChild(tabs);
newHostElement.appendChild(tabs);
// remove or set the height of the code
if (above) {
setCodeHeightFromDocHeight();
} else {
document.getElementById("showcase-code-content").removeAttribute("style");
}
$(newHostElement).fadeIn(animateCodeMs);
if (!above) {
// remove the applied width and zoom on the app container, and
// scroll smoothly down to the code's new home
document.getElementById("showcase-app-container").removeAttribute("style");
if (animate)
$(document.body).animate({ scrollTop: $(newHostElement).offset().top });
}
// if there's a readme, move it either alongside the code or beneath
// the app
var readme = document.getElementById("readme-md");
if (readme !== null) {
readme.parentElement.removeChild(readme);
if (above) {
currentHostElement.appendChild(readme);
$(currentHostElement).fadeIn(animateCodeMs);
}
else
document.getElementById("showcase-app-metadata").appendChild(readme);
}
// change the text on the toggle button to reflect the new state
document.getElementById("showcase-code-position-toggle").innerHTML = above ?
'<i class="fa fa-level-down"></i> show below' :
'<i class="fa fa-level-up"></i> show with app';
});
if (above) {
$(document.body).animate({ scrollTop: 0 }, animateCodeMs);
}
isCodeAbove = above;
setAppCodeSxsWidths(above && animate);
$(window).trigger("resize");
};
var setAppCodeSxsWidths = function(animate) {
var appTargetWidth = 960;
var appWidth = appTargetWidth;
var zoom = 1.0;
var totalWidth = document.getElementById("showcase-app-code").offsetWidth;
if (totalWidth / 2 > appTargetWidth) {
// If the app can use only half the available space and still meet its
// target, take half the available space.
appWidth = totalWidth / 2;
} else if (totalWidth * 0.66 > appTargetWidth) {
// If the app can meet its target by taking up more space (up to 66%
// of its container), take up more space.
appWidth = 960;
} else {
// The space is too narrow for the app and code to live side-by-side
// in a friendly way. Keep the app at 2/3 of the space but scale it.
appWidth = totalWidth * 0.66;
zoom = appWidth/appTargetWidth;
}
var app = document.getElementById("showcase-app-container");
$(app).animate({
width: appWidth + "px",
zoom: (zoom*100) + "%"
}, animate ? animateMs : 0);
};
var toggleCodePosition = function() {
setCodePosition(!isCodeAbove, true);
};
// if the browser is sized to wider than 1350px, show the code next to the
// app by default
var setInitialCodePosition = function() {
if (document.body.offsetWidth > 1350) {
setCodePosition(true, false);
}
};
// make the code scrollable to about the height of the browser, less space
// for the tabs
var setCodeHeightFromDocHeight = function() {
document.getElementById("showcase-code-content").style.height =
$(window).height() + "px";
};
// if there's a block of markdown content, render it to HTML
var renderMarkdown = function() {
var mdContent = document.getElementById("showcase-markdown-content");
if (mdContent !== null) {
// IE8 puts the content of <script> tags into innerHTML but
// not innerText
var content = mdContent.innerText || mdContent.innerHTML;
document.getElementById("readme-md").innerHTML =
(new Showdown.converter()).makeHtml(content)
}
}
$(window).resize(function() {
if (isCodeAbove) {
setAppCodeSxsWidths(false);
setCodeHeightFromDocHeight();
}
});
window.toggleCodePosition = toggleCodePosition;
$(window).on("load", setInitialCodePosition);
$(window).on("load", renderMarkdown);
if (window.hljs)
hljs.initHighlightingOnLoad();
})();

View File

@@ -1,8 +0,0 @@
// Listen for messages from parent frame. This file is only added when the
// shiny.testmode option is TRUE.
window.addEventListener("message", function(e) {
var message = e.data;
if (message.code)
eval(message.code);
});

View File

@@ -1,11 +0,0 @@
# `yarn` Patch files
* `types-jquery.patch`
* Do not export `$` as a globally available variable. When developing TS code, I like to have full control over the variables. It is good to have a record of where everything comes from. Shiny can not use the latest jQuery loaded onto the page and needs to use a scoped `jQuery` variable. In the end, we can shim the `jquery` import to be `window.jQuery`
* `yarn_pnp.patch`
* This file is currently not used and is outdated.
* This provides a good game plan on how to use PnP with Yarn once esbuild can easily be integrated with Yarn PnP.
* Using PnP removes the `node_modules` folder, but adds a zip of each package. I **do not** like Yarn's suggestion to commit these zip files to support their [Zero Installs](https://next.yarnpkg.com/features/zero-installs) philosophy.
* Reference:
* https://next.yarnpkg.com/features/pnp
* https://yarnpkg.com/api/modules/esbuild_plugin_pnp.html

View File

@@ -1,12 +1,12 @@
import type { RatePolicyModes } from "../../inputPolicies/inputRateDecorator";
import type { BindScope } from "../../shiny/bind";
import { RatePolicyModes } from "../../inputPolicies/inputRateDecorator";
import { bindScope } from "../../shiny/bind";
class InputBinding {
name: string;
// Returns a jQuery object or element array that contains the
// descendants of scope that match this binding
find(scope: BindScope): JQuery<HTMLElement> {
find(scope: bindScope): JQuery<HTMLElement> {
throw "Not implemented";
// add so that typescript isn't mad about an unused var
scope;

View File

@@ -1,5 +1,5 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { hasOwnProperty } from "../../utils";

View File

@@ -1,5 +1,5 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { hasOwnProperty } from "../../utils";
type CheckedHTMLElement = HTMLInputElement;

View File

@@ -1,8 +1,8 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { $escape, hasOwnProperty, updateLabel } from "../../utils";
import type { CheckedHTMLElement } from "./checkbox";
import { CheckedHTMLElement } from "./checkbox";
type CheckboxGroupHTMLElement = CheckedHTMLElement;
type ValueLabelObject = {
@@ -17,38 +17,12 @@ type CheckboxGroupReceiveMessageData = {
type CheckboxGroupValue = CheckboxGroupHTMLElement["value"];
// Get the DOM element that contains the top-level label
function getLabelNode(el: CheckboxGroupHTMLElement): JQuery<HTMLElement> {
return $(el).find('label[for="' + $escape(el.id) + '"]');
}
// Given an input DOM object, get the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
function getLabel(obj: HTMLElement): string | null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
return $(obj.parentNode).find("span").text().trim();
}
return null;
}
// Given an input DOM object, set the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function setLabel(obj: HTMLElement, value: string): null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
$(obj.parentNode).find("span").text(value);
}
return null;
}
class CheckboxGroupInputBinding extends InputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find(".shiny-input-checkboxgroup");
}
getValue(el: CheckboxGroupHTMLElement): CheckboxGroupValue[] {
getValue(el: CheckboxGroupHTMLElement): Array<CheckboxGroupValue> {
// Select the checkbox objects that have name equal to the grouping div's id
const $objs = $('input:checkbox[name="' + $escape(el.id) + '"]:checked');
const values = new Array($objs.length);
@@ -58,7 +32,7 @@ class CheckboxGroupInputBinding extends InputBinding {
}
return values;
}
setValue(el: HTMLElement, value: string[] | string): void {
setValue(el: HTMLElement, value: Array<string> | string): void {
// Clear all checkboxes
$('input:checkbox[name="' + $escape(el.id) + '"]').prop("checked", false);
@@ -87,7 +61,7 @@ class CheckboxGroupInputBinding extends InputBinding {
getState(el: CheckboxGroupHTMLElement): {
label: string;
value: ReturnType<CheckboxGroupInputBinding["getValue"]>;
options: ValueLabelObject[];
options: Array<ValueLabelObject>;
} {
const $objs = $(
'input:checkbox[name="' + $escape(el.id) + '"]'
@@ -97,11 +71,11 @@ class CheckboxGroupInputBinding extends InputBinding {
const options = new Array($objs.length);
for (let i = 0; i < options.length; i++) {
options[i] = { value: $objs[i].value, label: getLabel($objs[i]) };
options[i] = { value: $objs[i].value, label: this._getLabel($objs[i]) };
}
return {
label: getLabelNode(el).text(),
label: this._getLabelNode(el).text(),
value: this.getValue(el),
options: options,
};
@@ -123,7 +97,7 @@ class CheckboxGroupInputBinding extends InputBinding {
if (hasOwnProperty(data, "value")) this.setValue(el, data.value);
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
$(el).trigger("change");
}
@@ -138,6 +112,31 @@ class CheckboxGroupInputBinding extends InputBinding {
unsubscribe(el: CheckboxGroupHTMLElement): void {
$(el).off(".checkboxGroupInputBinding");
}
// Get the DOM element that contains the top-level label
_getLabelNode(el: CheckboxGroupHTMLElement): JQuery<HTMLElement> {
return $(el).find('label[for="' + $escape(el.id) + '"]');
}
// Given an input DOM object, get the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
_getLabel(obj: HTMLElement): string | null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
return $(obj.parentNode).find("span").text().trim();
}
return null;
}
// Given an input DOM object, set the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
_setLabel(obj: HTMLElement, value: string): null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
$(obj.parentNode).find("span").text(value);
}
return null;
}
}
export { CheckboxGroupInputBinding };

View File

@@ -1,5 +1,5 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import {
formatDateUTC,
updateLabel,
@@ -17,7 +17,7 @@ declare global {
bsDatepicker(methodName: "getStartDate"): Date | -1e9999;
bsDatepicker(methodName: "getEndDate"): Date | 1e9999;
bsDatepicker(methodName: string): void;
bsDatepicker(methodName: string, params: Date | null): void;
bsDatepicker(methodName: string, params: null | Date): void;
}
}
@@ -100,13 +100,13 @@ class DateInputBindingBase extends InputBinding {
this._setMax($input[0], $input.data("max-date"));
}
}
protected _getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
_getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el).find('label[for="' + $escape(el.id) + '"]');
}
// Given a format object from a date picker, return a string
protected _formatToString(format: {
parts: string[];
separators: string[];
_formatToString(format: {
parts: Array<string>;
separators: Array<string>;
}): string {
// Format object has structure like:
// { parts: ['mm', 'dd', 'yy'], separators: ['', '/', '/' ,''] }
@@ -122,7 +122,7 @@ class DateInputBindingBase extends InputBinding {
}
// Given an unambiguous date string or a Date object, set the min (start) date.
// null will unset. undefined will result in no change,
protected _setMin(el: HTMLElement, date: Date | null | undefined): void {
_setMin(el: HTMLElement, date: Date | undefined | null): void {
if (date === undefined) return;
if (date === null) {
$(el).bsDatepicker("setStartDate", null);
@@ -145,7 +145,7 @@ class DateInputBindingBase extends InputBinding {
// Note that there's no `setUTCStartDate`, so we need to convert this Date.
// It starts at 00:00 UTC, and we convert it to 00:00 in local time, which
// is what's needed for `setStartDate`.
$(el).bsDatepicker("setStartDate", this._utcDateAsLocal(date));
$(el).bsDatepicker("setStartDate", this._UTCDateAsLocal(date));
// If the new min is greater than the current date, unset the current date.
if (date && curValue && date.getTime() > curValue.getTime()) {
@@ -161,7 +161,7 @@ class DateInputBindingBase extends InputBinding {
}
// Given an unambiguous date string or a Date object, set the max (end) date
// null will unset.
protected _setMax(el: HTMLElement, date: Date): void {
_setMax(el: HTMLElement, date: Date): void {
if (date === undefined) return;
if (date === null) {
$(el).bsDatepicker("setEndDate", null);
@@ -180,7 +180,7 @@ class DateInputBindingBase extends InputBinding {
// Workaround for same issue as in _setMin.
const curValue = $(el).bsDatepicker("getUTCDate");
$(el).bsDatepicker("setEndDate", this._utcDateAsLocal(date));
$(el).bsDatepicker("setEndDate", this._UTCDateAsLocal(date));
// If the new min is greater than the current date, unset the current date.
if (date && curValue && date.getTime() < curValue.getTime()) {
@@ -192,7 +192,7 @@ class DateInputBindingBase extends InputBinding {
// Given a date string of format yyyy-mm-dd, return a Date object with
// that date at 12AM UTC.
// If date is a Date object, return it unchanged.
protected _newDate(date: Date | never | string): Date | null {
_newDate(date: Date | string | never): Date | null {
if (date instanceof Date) return date;
if (!date) return null;
@@ -207,7 +207,7 @@ class DateInputBindingBase extends InputBinding {
}
// A Date can have any time during a day; this will return a new Date object
// set to 00:00 in UTC.
protected _floorDateTime(date: Date): Date {
_floorDateTime(date: Date): Date {
date = new Date(date.getTime());
date.setUTCHours(0, 0, 0, 0);
return date;
@@ -216,13 +216,13 @@ class DateInputBindingBase extends InputBinding {
// in UTC. For example, if input date is 2013-02-01 23:00:00 GMT-0600 (CST),
// output will be 2013-02-01 23:00:00 UTC. Note that the JS console may
// print this in local time, as "Sat Feb 02 2013 05:00:00 GMT-0600 (CST)".
protected _dateAsUTC(date: Date): Date {
_dateAsUTC(date: Date): Date {
return new Date(date.getTime() - date.getTimezoneOffset() * 60000);
}
// The inverse of _dateAsUTC. This is needed to adjust time zones because
// some bootstrap-datepicker methods only take local dates as input, and not
// UTC.
protected _utcDateAsLocal(date: Date): Date {
_UTCDateAsLocal(date: Date): Date {
return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
}
}
@@ -257,7 +257,7 @@ class DateInputBinding extends DateInputBindingBase {
getState(el: HTMLElement): {
label: string;
value: string | null;
valueString: string[] | number | string;
valueString: string | number | string[];
min: string | null;
max: string | null;
language: string | null;

View File

@@ -15,9 +15,6 @@ type DateRangeReceiveMessageData = {
value?: { start?: Date; end?: Date };
};
function getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el).find('label[for="' + $escape(el.id) + '"]');
}
class DateRangeInputBinding extends DateInputBindingBase {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find(".shiny-date-range-input");
@@ -95,7 +92,7 @@ class DateRangeInputBinding extends DateInputBindingBase {
else if (startview === 0) startview = "month";
return {
label: getLabelNode(el).text(),
label: this._getLabelNode(el).text(),
value: this.getValue(el),
valueString: [$startinput.val() as string, $endinput.val() as string],
min: minStr,
@@ -112,7 +109,7 @@ class DateRangeInputBinding extends DateInputBindingBase {
const $startinput = $inputs.eq(0);
const $endinput = $inputs.eq(1);
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
if (hasOwnProperty(data, "min")) {
this._setMin($startinput[0], data.min);
@@ -179,6 +176,9 @@ class DateRangeInputBinding extends DateInputBindingBase {
unsubscribe(el: HTMLElement): void {
$(el).off(".dateRangeInputBinding");
}
_getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el).find('label[for="' + $escape(el.id) + '"]');
}
}
export { DateRangeInputBinding };

View File

@@ -1,122 +1,12 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { FileUploader } from "../../file/fileProcessor";
import { InputBinding } from "./InputBinding";
import { FileUploader } from "../../file/FileProcessor";
import { shinyShinyApp } from "../../shiny/initedMethods";
const zoneActive = "shiny-file-input-active";
const zoneOver = "shiny-file-input-over";
function zoneOf(el: HTMLElement | JQuery<HTMLElement>): JQuery<HTMLElement> {
return $(el).closest("div.input-group");
}
// This function makes it possible to attach listeners to the dragenter,
// dragleave, and drop events of a single element with children. It's not
// intuitive to do directly because outer elements fire "dragleave" events
// both when the drag leaves the element and when the drag enters a child. To
// make it easier, we maintain a count of the elements being dragged across
// and trigger 3 new types of event:
//
// 1. draghover:enter - When a drag enters el and any of its children.
// 2. draghover:leave - When the drag leaves el and all of its children.
// 3. draghover:drop - When an item is dropped on el or any of its children.
function enableDraghover(el: JQuery<HTMLElement>): JQuery<HTMLElement> {
const $el = $(el);
let childCounter = 0;
$el.on({
"dragenter.draghover": (e) => {
if (childCounter++ === 0) {
$el.trigger("draghover:enter", e);
}
},
"dragleave.draghover": (e) => {
if (--childCounter === 0) {
$el.trigger("draghover:leave", e);
}
if (childCounter < 0) {
console.error("draghover childCounter is negative somehow");
}
},
"dragover.draghover": (e) => {
e.preventDefault();
},
"drop.draghover": (e) => {
childCounter = 0;
$el.trigger("draghover:drop", e);
e.preventDefault();
},
});
return $el;
}
function disableDraghover(el: JQuery<HTMLElement>): JQuery<HTMLElement> {
return $(el).off(".draghover");
}
function enableDocumentEvents(): void {
const $doc = $("html");
enableDraghover($doc).on({
"draghover:enter.draghover":
// e: Event
() => {
zoneOf($fileInputs).addClass(zoneActive);
},
"draghover:leave.draghover":
// e: Event
() => {
zoneOf($fileInputs).removeClass(zoneActive);
},
"draghover:drop.draghover":
// e: Event
() => {
zoneOf($fileInputs).removeClass(zoneOver).removeClass(zoneActive);
},
});
}
function disableDocumentEvents(): void {
const $doc = $("html");
$doc.off(".draghover");
disableDraghover($doc);
}
function canSetFiles(fileList: FileList): boolean {
const testEl = document.createElement("input");
testEl.type = "file";
try {
testEl.files = fileList;
} catch (e) {
return false;
}
return true;
}
function handleDrop(e: JQuery.DragEventBase, el: HTMLInputElement): void {
const files = e.originalEvent.dataTransfer.files,
$el = $(el);
if (files === undefined || files === null) {
// 1. The FileList object isn't supported by this browser, and
// there's nothing else we can try. (< IE 10)
console.log(
"Dropping files is not supported on this browser. (no FileList)"
);
} else if (!canSetFiles(files)) {
// 2. The browser doesn't support assigning a type=file input's .files
// property, but we do have a FileList to work with. (IE10+/Edge)
$el.val("");
uploadDroppedFilesIE10Plus(el, files);
} else {
// 3. The browser supports FileList and input.files assignment.
// (Chrome, Safari)
$el.val("");
el.files = e.originalEvent.dataTransfer.files;
// Recent versions of Firefox (57+, or "Quantum" and beyond) don't seem to
// automatically trigger a change event, so we trigger one manually here.
// On browsers that do trigger change, this operation appears to be
// idempotent, as el.files doesn't change between events.
$el.trigger("change");
}
}
const _ZoneClass = {
ACTIVE: "shiny-file-input-active",
OVER: "shiny-file-input-over",
};
// NOTE On Safari, at least version 10.1.2, *if the developer console is open*,
// setting the input's value will behave strangely because of a Safari bug. The
@@ -243,47 +133,158 @@ class FileInputBinding extends InputBinding {
return "shiny.file";
el;
}
_zoneOf(el: HTMLElement | JQuery<HTMLElement>): JQuery<HTMLElement> {
return $(el).closest("div.input-group");
}
// This function makes it possible to attach listeners to the dragenter,
// dragleave, and drop events of a single element with children. It's not
// intuitive to do directly because outer elements fire "dragleave" events
// both when the drag leaves the element and when the drag enters a child. To
// make it easier, we maintain a count of the elements being dragged across
// and trigger 3 new types of event:
//
// 1. draghover:enter - When a drag enters el and any of its children.
// 2. draghover:leave - When the drag leaves el and all of its children.
// 3. draghover:drop - When an item is dropped on el or any of its children.
_enableDraghover(el: JQuery<HTMLElement>): JQuery<HTMLElement> {
const $el = $(el);
let childCounter = 0;
$el.on({
"dragenter.draghover": (e) => {
if (childCounter++ === 0) {
$el.trigger("draghover:enter", e);
}
},
"dragleave.draghover": (e) => {
if (--childCounter === 0) {
$el.trigger("draghover:leave", e);
}
if (childCounter < 0) {
console.error("draghover childCounter is negative somehow");
}
},
"dragover.draghover": (e) => {
e.preventDefault();
},
"drop.draghover": (e) => {
childCounter = 0;
$el.trigger("draghover:drop", e);
e.preventDefault();
},
});
return $el;
}
_disableDraghover(el: JQuery<HTMLElement>): JQuery<HTMLElement> {
return $(el).off(".draghover");
}
_enableDocumentEvents(): void {
const $doc = $("html"),
{ ACTIVE, OVER } = _ZoneClass;
this._enableDraghover($doc).on({
"draghover:enter.draghover":
// e: Event
() => {
this._zoneOf($fileInputs).addClass(ACTIVE);
},
"draghover:leave.draghover":
// e: Event
() => {
this._zoneOf($fileInputs).removeClass(ACTIVE);
},
"draghover:drop.draghover":
// e: Event
() => {
this._zoneOf($fileInputs).removeClass(OVER).removeClass(ACTIVE);
},
});
}
_disableDocumentEvents(): void {
const $doc = $("html");
$doc.off(".draghover");
this._disableDraghover($doc);
}
_canSetFiles(fileList: FileList): boolean {
const testEl = document.createElement("input");
testEl.type = "file";
try {
testEl.files = fileList;
} catch (e) {
return false;
}
return true;
}
_handleDrop(e: JQuery.DragEventBase, el: HTMLInputElement): void {
const files = e.originalEvent.dataTransfer.files,
$el = $(el);
if (files === undefined || files === null) {
// 1. The FileList object isn't supported by this browser, and
// there's nothing else we can try. (< IE 10)
console.log(
"Dropping files is not supported on this browser. (no FileList)"
);
} else if (!this._canSetFiles(files)) {
// 2. The browser doesn't support assigning a type=file input's .files
// property, but we do have a FileList to work with. (IE10+/Edge)
$el.val("");
uploadDroppedFilesIE10Plus(el, files);
} else {
// 3. The browser supports FileList and input.files assignment.
// (Chrome, Safari)
$el.val("");
el.files = e.originalEvent.dataTransfer.files;
// Recent versions of Firefox (57+, or "Quantum" and beyond) don't seem to
// automatically trigger a change event, so we trigger one manually here.
// On browsers that do trigger change, this operation appears to be
// idempotent, as el.files doesn't change between events.
$el.trigger("change");
}
}
subscribe(el: HTMLInputElement, callback: (x: boolean) => void): void {
callback;
$(el).on("change.fileInputBinding", uploadFiles);
// Here we try to set up the necessary events for Drag and Drop ("DnD").
if ($fileInputs.length === 0) enableDocumentEvents();
if ($fileInputs.length === 0) this._enableDocumentEvents();
$fileInputs = $fileInputs.add(el);
const $zone = zoneOf(el);
const $zone = this._zoneOf(el),
{ OVER } = _ZoneClass;
enableDraghover($zone).on({
this._enableDraghover($zone).on({
"draghover:enter.draghover": (e) => {
e;
$zone.addClass(zoneOver);
$zone.addClass(OVER);
},
"draghover:leave.draghover": (e) => {
$zone.removeClass(zoneOver);
$zone.removeClass(OVER);
// Prevent this event from bubbling to the document handler,
// which would deactivate all zones.
e.stopPropagation();
},
"draghover:drop.draghover": (e, dropEvent) => {
e;
handleDrop(dropEvent, el);
this._handleDrop(dropEvent, el);
},
});
}
unsubscribe(el: HTMLElement): void {
const $el = $(el),
$zone = zoneOf(el);
$zone = this._zoneOf(el);
$zone.removeClass(zoneOver).removeClass(zoneActive);
$zone.removeClass(_ZoneClass.OVER).removeClass(_ZoneClass.ACTIVE);
disableDraghover($zone);
this._disableDraghover($zone);
$el.off(".fileInputBinding");
$zone.off(".draghover");
// Remove el from list of inputs and (maybe) clean up global event handlers.
$fileInputs = $fileInputs.not(el);
if ($fileInputs.length === 0) disableDocumentEvents();
if ($fileInputs.length === 0) this._disableDocumentEvents();
}
}

View File

@@ -1,6 +1,6 @@
import { BindingRegistry } from "../registry";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { CheckboxInputBinding } from "./checkbox";
import { CheckboxGroupInputBinding } from "./checkboxgroup";

View File

@@ -12,18 +12,12 @@ type NumberReceiveMessageData = {
step?: string | null;
};
function getLabelNode(el: NumberHTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
class NumberInputBinding extends TextInputBindingBase {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find('input[type="number"]');
}
getValue(el: NumberHTMLElement): string[] | number | string {
getValue(el: NumberHTMLElement): string | number | string[] {
const numberVal = $(el).val();
if (typeof numberVal == "string") {
@@ -54,7 +48,7 @@ class NumberInputBinding extends TextInputBindingBase {
if (hasOwnProperty(data, "max")) el.max = data.max;
if (hasOwnProperty(data, "step")) el.step = data.step;
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
$(el).trigger("change");
}
@@ -67,13 +61,19 @@ class NumberInputBinding extends TextInputBindingBase {
step: number;
} {
return {
label: getLabelNode(el).text(),
label: this._getLabelNode(el).text(),
value: this.getValue(el),
min: Number(el.min),
max: Number(el.max),
step: Number(el.step),
};
}
_getLabelNode(el: NumberHTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
}
export { NumberInputBinding };

View File

@@ -1,5 +1,5 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { $escape, hasOwnProperty, updateLabel } from "../../utils";
type RadioHTMLElement = HTMLInputElement;
@@ -11,43 +11,15 @@ type ValueLabelObject = {
type RadioReceiveMessageData = {
value?: string;
options?: ValueLabelObject[];
options?: Array<ValueLabelObject>;
label: string;
};
// Get the DOM element that contains the top-level label
function getLabelNode(el: RadioHTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
// Given an input DOM object, get the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
function getLabel(obj: HTMLElement): string | null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
return $(obj.parentNode).find("span").text().trim();
}
return null;
}
// Given an input DOM object, set the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function setLabel(obj: HTMLElement, value: string): null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
$(obj.parentNode).find("span").text(value);
}
return null;
}
class RadioInputBinding extends InputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find(".shiny-input-radiogroup");
}
getValue(el: RadioHTMLElement): string[] | number | string | null {
getValue(el: RadioHTMLElement): string | number | string[] | null {
// Select the radio objects that have name equal to the grouping div's id
const checkedItems = $(
'input:radio[name="' + $escape(el.id) + '"]:checked'
@@ -77,8 +49,8 @@ class RadioInputBinding extends InputBinding {
}
getState(el: RadioHTMLElement): {
label: string;
value: string[] | number | string;
options: ValueLabelObject[];
value: string | number | string[];
options: Array<ValueLabelObject>;
} {
const $objs = $(
'input:radio[name="' + $escape(el.id) + '"]'
@@ -88,11 +60,11 @@ class RadioInputBinding extends InputBinding {
const options = new Array($objs.length);
for (let i = 0; i < options.length; i++) {
options[i] = { value: $objs[i].value, label: getLabel($objs[i]) };
options[i] = { value: $objs[i].value, label: this._getLabel($objs[i]) };
}
return {
label: getLabelNode(el).text(),
label: this._getLabelNode(el).text(),
value: this.getValue(el),
options: options,
};
@@ -112,7 +84,7 @@ class RadioInputBinding extends InputBinding {
if (hasOwnProperty(data, "value")) this.setValue(el, data.value);
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
$(el).trigger("change");
}
@@ -124,6 +96,32 @@ class RadioInputBinding extends InputBinding {
unsubscribe(el: RadioHTMLElement): void {
$(el).off(".radioInputBinding");
}
// Get the DOM element that contains the top-level label
_getLabelNode(el: RadioHTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
// Given an input DOM object, get the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
_getLabel(obj: HTMLElement): string | null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
return $(obj.parentNode).find("span").text().trim();
}
return null;
}
// Given an input DOM object, set the associated label. Handles labels
// that wrap the input as well as labels associated with 'for' attribute.
_setLabel(obj: HTMLElement, value: string): null {
// If <label><input /><span>label text</span></label>
if ((obj.parentNode as HTMLElement).tagName === "LABEL") {
$(obj.parentNode).find("span").text(value);
}
return null;
}
}
export { RadioInputBinding };

View File

@@ -1,5 +1,5 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { $escape, hasOwnProperty, updateLabel } from "../../utils";
import { indirectEval } from "../../utils/eval";
@@ -17,27 +17,6 @@ type SelectizeInfo = Selectize.IApi<string, unknown> & {
settings: Selectize.IOptions<string, unknown>;
};
function getLabelNode(el: SelectHTMLElement): JQuery<HTMLElement> {
let escapedId = $escape(el.id);
if (isSelectize(el)) {
escapedId += "-selectized";
}
return $(el)
.parent()
.parent()
.find('label[for="' + escapedId + '"]');
}
// Return true if it's a selectize input, false if it's a regular select input.
// eslint-disable-next-line camelcase
function isSelectize(el: HTMLElement): boolean {
const config = $(el)
.parent()
.find('script[data-for="' + $escape(el.id) + '"]');
return config.length > 0;
}
class SelectInputBinding extends InputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find("select");
@@ -58,11 +37,11 @@ class SelectInputBinding extends InputBinding {
getId(el: SelectHTMLElement): string {
return InputBinding.prototype.getId.call(this, el) || el.name;
}
getValue(el: HTMLElement): string[] | number | string {
getValue(el: HTMLElement): string | number | string[] {
return $(el).val();
}
setValue(el: SelectHTMLElement, value: string): void {
if (!isSelectize(el)) {
if (!this._is_selectize(el)) {
$(el).val(value);
} else {
const selectize = this._selectize(el);
@@ -74,7 +53,7 @@ class SelectInputBinding extends InputBinding {
}
getState(el: SelectHTMLElement): {
label: JQuery<HTMLElement>;
value: string[] | number | string;
value: string | number | string[];
options: Array<{ value: string; label: string }>;
} {
// Store options in an array of objects, each with with value and label
@@ -91,7 +70,7 @@ class SelectInputBinding extends InputBinding {
}
return {
label: getLabelNode(el),
label: this._getLabelNode(el),
value: this.getValue(el),
options: options,
};
@@ -182,7 +161,7 @@ class SelectInputBinding extends InputBinding {
this.setValue(el, data.value);
}
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
$(el).trigger("change");
}
@@ -207,7 +186,27 @@ class SelectInputBinding extends InputBinding {
initialize(el: SelectHTMLElement): void {
this._selectize(el);
}
protected _selectize(el: SelectHTMLElement, update = false): SelectizeInfo {
_getLabelNode(el: SelectHTMLElement): JQuery<HTMLElement> {
let escapedId = $escape(el.id);
if (this._is_selectize(el)) {
escapedId += "-selectized";
}
return $(el)
.parent()
.parent()
.find('label[for="' + escapedId + '"]');
}
// Return true if it's a selectize input, false if it's a regular select input.
// eslint-disable-next-line camelcase
_is_selectize(el: HTMLElement): boolean {
const config = $(el)
.parent()
.find('script[data-for="' + $escape(el.id) + '"]');
return config.length > 0;
}
_selectize(el: SelectHTMLElement, update = false): SelectizeInfo {
if (!$.fn.selectize) return undefined;
const $el = $(el);
const config = $el
@@ -222,7 +221,7 @@ class SelectInputBinding extends InputBinding {
searchField: ["label"];
onItemRemove?: (value: string) => void;
onDropdownClose?: () => void;
} & { [key: string]: unknown } = $.extend(
} & Record<string, unknown> = $.extend(
{
labelField: "label",
valueField: "value",

View File

@@ -7,16 +7,14 @@ import {
hasOwnProperty,
} from "../../utils";
import type { TextHTMLElement } from "./text";
import { TextInputBindingBase } from "./text";
import { TextHTMLElement, TextInputBindingBase } from "./text";
// interface SliderHTMLElement extends NameValueHTMLElement {
// checked?: any;
// }
type TimeFormatter = (fmt: string, dt: Date) => string;
// Backward compatible code for old-style jsliders (Shiny <= 0.10.2.2),
type LegacySlider = {
type legacySliderType = {
canStepNext: () => boolean;
stepNext: () => void;
resetToStart: () => void;
@@ -24,7 +22,7 @@ type LegacySlider = {
type SliderReceiveMessageData = {
label: string;
value?: Array<number | string> | number | string;
value?: Array<string | number> | string | number;
min?: number;
max?: number;
step?: number;
@@ -34,10 +32,14 @@ type SliderReceiveMessageData = {
// and could be needed after shiny has initialized.
declare global {
interface Window {
strftime: TimeFormatter & {
strftime: {
utc: () => TimeFormatter;
timezone: (timezone: string) => TimeFormatter;
};
} & TimeFormatter;
}
interface JQuery {
// Backward compatible code for old-style jsliders (Shiny <= 0.10.2.2),
slider: () => legacySliderType;
}
}
@@ -48,14 +50,14 @@ function forceIonSliderUpdate(slider) {
else console.log("Couldn't force ion slider to update");
}
type Prettify = (num: number) => string;
type prettifyType = (num: number) => string;
function getTypePrettifyer(
dataType: string,
timeFormat: string,
timezone: string
) {
let timeFormatter: TimeFormatter;
let prettify: Prettify;
let prettify: prettifyType;
if (dataType === "date") {
timeFormatter = window.strftime.utc();
@@ -82,17 +84,6 @@ function getTypePrettifyer(
return prettify;
}
function getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
// Number of values; 1 for single slider, 2 for range slider
function numValues(el: HTMLElement): 1 | 2 {
if ($(el).data("ionRangeSlider").options.type === "double") return 2;
else return 1;
}
class SliderInputBinding extends TextInputBindingBase {
find(scope: HTMLElement): JQuery<HTMLElement> {
// Check if ionRangeSlider plugin is loaded
@@ -136,7 +127,7 @@ class SliderInputBinding extends TextInputBindingBase {
};
}
if (numValues(el) === 2) {
if (this._numValues(el) === 2) {
return [convert(result.from), convert(result.to)];
} else {
return convert(result.from);
@@ -151,7 +142,7 @@ class SliderInputBinding extends TextInputBindingBase {
$el.data("immediate", true);
try {
if (numValues(el) === 2 && value instanceof Array) {
if (this._numValues(el) === 2 && value instanceof Array) {
slider.update({ from: value[0], to: value[1] });
} else {
slider.update({ from: value });
@@ -174,20 +165,20 @@ class SliderInputBinding extends TextInputBindingBase {
const $el = $(el);
const slider = $el.data("ionRangeSlider");
const msg: {
from?: number | string;
to?: number | string;
from?: string | number;
to?: string | number;
min?: number;
max?: number;
step?: number;
prettify?: Prettify;
prettify?: prettifyType;
} = {};
if (hasOwnProperty(data, "value")) {
if (numValues(el) === 2 && data.value instanceof Array) {
if (this._numValues(el) === 2 && data.value instanceof Array) {
msg.from = data.value[0];
msg.to = data.value[1];
} else {
msg.from = data.value as number | string;
msg.from = data.value as string | number;
}
}
@@ -201,7 +192,7 @@ class SliderInputBinding extends TextInputBindingBase {
}
}
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
// (maybe) update data elements
const domElements = ["data-type", "time-format", "timezone"];
@@ -254,6 +245,16 @@ class SliderInputBinding extends TextInputBindingBase {
$el.ionRangeSlider(opts);
}
_getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
// Number of values; 1 for single slider, 2 for range slider
_numValues(el: HTMLElement): 1 | 2 {
if ($(el).data("ionRangeSlider").options.type === "double") return 2;
else return 1;
}
}
// Format numbers for nicer output.
@@ -302,7 +303,7 @@ $(document).on("click", ".slider-animate-button", function (evt: Event) {
// Backward compatible code for old-style jsliders (Shiny <= 0.10.2.2),
// and new-style ionsliders.
if (target.hasClass("jslider")) {
const slider = target.slider() as unknown as LegacySlider;
const slider = target.slider();
// If we're currently at the end, restart
if (!slider.canStepNext()) slider.resetToStart();

View File

@@ -1,13 +1,8 @@
import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
import { hasOwnProperty, isBS3 } from "../../utils";
type TabInputReceiveMessageData = { value?: string };
function getTabName(anchor: JQuery<HTMLElement>): string {
return anchor.attr("data-value") || anchor.text();
}
class BootstrapTabInputBinding extends InputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find("ul.nav.shiny-tab-input");
@@ -21,11 +16,14 @@ class BootstrapTabInputBinding extends InputBinding {
".nav-link:not(.dropdown-toggle).active, .dropdown-menu .dropdown-item.active"
);
if (anchor.length === 1) return getTabName(anchor);
if (anchor.length === 1) return this._getTabName(anchor);
return null;
}
setValue(el: HTMLElement, value: string): void {
// this is required as an arrow function will not fix the usage
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
let success = false;
if (value) {
@@ -38,7 +36,7 @@ class BootstrapTabInputBinding extends InputBinding {
);
anchors.each(function () {
if (getTabName($(this)) === value) {
if (self._getTabName($(this)) === value) {
$(this).tab("show");
success = true;
return false; // Break out of each()
@@ -71,6 +69,9 @@ class BootstrapTabInputBinding extends InputBinding {
unsubscribe(el: HTMLElement): void {
$(el).off(".bootstrapTabInputBinding");
}
_getTabName(anchor: JQuery<HTMLElement>): string {
return anchor.attr("data-value") || anchor.text();
}
}
export { BootstrapTabInputBinding };

View File

@@ -1,7 +1,7 @@
import $ from "jquery";
import { $escape, updateLabel, hasOwnProperty } from "../../utils";
import { InputBinding } from "./inputBinding";
import { InputBinding } from "./InputBinding";
// interface TextHTMLElement extends NameValueHTMLElement {
// placeholder: any;
@@ -14,12 +14,6 @@ type TextReceiveMessageData = {
placeholder?: TextHTMLElement["placeholder"];
};
function getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
class TextInputBindingBase extends InputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
const $inputs = $(scope).find(
@@ -86,6 +80,12 @@ class TextInputBindingBase extends InputBinding {
};
el;
}
_getLabelNode(el: HTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
}
class TextInputBinding extends TextInputBindingBase {
@@ -103,7 +103,7 @@ class TextInputBinding extends TextInputBindingBase {
placeholder: string;
} {
return {
label: getLabelNode(el).text(),
label: this._getLabelNode(el).text(),
value: el.value,
placeholder: el.placeholder,
};
@@ -111,7 +111,7 @@ class TextInputBinding extends TextInputBindingBase {
receiveMessage(el: TextHTMLElement, data: TextReceiveMessageData): void {
if (hasOwnProperty(data, "value")) this.setValue(el, data.value);
updateLabel(data.label, getLabelNode(el));
updateLabel(data.label, this._getLabelNode(el));
if (hasOwnProperty(data, "placeholder")) el.placeholder = data.placeholder;

View File

@@ -1,13 +1,13 @@
import $ from "jquery";
import { asArray } from "../../utils";
import type { ErrorsMessageValue } from "../../shiny/shinyapp";
import type { errorsMessageValue } from "../../shiny/shinyapp";
class OutputBinding {
name: string;
// Returns a jQuery object or element array that contains the
// descendants of scope that match this binding
find(scope: HTMLElement | JQuery<HTMLElement>): JQuery<HTMLElement> {
find(scope: JQuery<HTMLElement> | HTMLElement): JQuery<HTMLElement> {
throw "Not implemented";
scope;
}
@@ -25,10 +25,10 @@ class OutputBinding {
this.clearError(el);
this.renderValue(el, data);
}
onValueError(el: HTMLElement, err: ErrorsMessageValue): void {
onValueError(el: HTMLElement, err: errorsMessageValue): void {
this.renderError(el, err);
}
renderError(el: HTMLElement, err: ErrorsMessageValue): void {
renderError(el: HTMLElement, err: errorsMessageValue): void {
this.clearError(el);
if (err.message === "") {
// not really error, but we just need to wait (e.g. action buttons)
@@ -54,10 +54,10 @@ class OutputBinding {
});
}
showProgress(el: HTMLElement, show: boolean): void {
const recalcClass = "recalculating";
const RECALC_CLASS = "recalculating";
if (show) $(el).addClass(recalcClass);
else $(el).removeClass(recalcClass);
if (show) $(el).addClass(RECALC_CLASS);
else $(el).removeClass(RECALC_CLASS);
}
}

View File

@@ -1,34 +1,34 @@
import $ from "jquery";
import { OutputBinding } from "./outputBinding";
import { OutputBinding } from "./OutputBinding";
import { shinyUnbindAll } from "../../shiny/initedMethods";
import { debounce } from "../../time";
import { escapeHTML } from "../../utils";
import { indirectEval } from "../../utils/eval";
import type { ErrorsMessageValue } from "../../shiny/shinyapp";
import type { errorsMessageValue } from "../../shiny/shinyapp";
class DatatableOutputBinding extends OutputBinding {
find(scope: HTMLElement): JQuery<HTMLElement> {
return $(scope).find(".shiny-datatable-output");
}
onValueError(el: HTMLElement, err: ErrorsMessageValue): void {
onValueError(el: HTMLElement, err: errorsMessageValue): void {
shinyUnbindAll(el);
this.renderError(el, err);
}
renderValue(
el: HTMLElement,
data: {
colnames?: string[];
options?: {
data: null | {
colnames?: Array<string>;
options?: null | {
searching?: boolean;
search?: { caseInsensitive?: boolean };
} | null;
};
action?: string;
escape?: string;
evalOptions?: string[];
evalOptions?: Array<string>;
callback?: string;
searchDelay?: number;
} | null
}
): void {
const $el = $(el).empty();

Some files were not shown because too many files have changed in this diff Show More