diff --git a/R/shiny.R b/R/shiny.R index 3a275d8479..e2bcbf58bc 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -2142,15 +2142,70 @@ outputOptions <- function(x, name, ...) { } -#' Get information about the output that is currently being executed. +#' Get output information #' -#' @return A list with information about the current output, including the -#' `name` of the output. If no output is currently being executed, this will -#' return `NULL`. +#' Returns information about the currently executing output, including its `name` (i.e., `outputId`); +#' and in some cases, relevant sizing and styling information. #' #' @param session The current Shiny session. #' +#' @return `NULL` if called outside of an output context; otherwise, +#' a list which includes: +#' * The `name` of the output (reported for any output). +#' * If the output is a `plotOutput()` or `imageOutput()`, then: +#' * `height`: a reactive expression which returns the height in pixels. +#' * `width`: a reactive expression which returns the width in pixels. +#' * If the output is a `plotOutput()`, `imageOutput()`, or contains a `shiny-report-theme` class, then: +#' * `bg`: a reactive expression which returns the background color. +#' * `fg`: a reactive expression which returns the foreground color. +#' * `accent`: a reactive expression which returns the hyperlink color. +#' * `font`: a reactive expression which returns a list of font information, including: +#' * `families`: a character vector containing the CSS `font-family` property. +#' * `size`: a character string containing the CSS `font-size` property +#' * `renderedFamily`: if running in a browser that supports +#' [FontFaceSet.check](https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/check), +#' a character string containing the first of `families` available to the browser. +#' #' @export +#' @examples +#' +#' if (interactive()) { +#' shinyApp( +#' fluidPage( +#' tags$style(HTML("body {background-color: black; color: white; }")), +#' tags$style(HTML("body a {color: purple}")), +#' tags$style(HTML("#info {background-color: teal; color: orange; }")), +#' plotOutput("p"), +#' "Computed CSS styles for the output named info:", +#' tagAppendAttributes( +#' textOutput("info"), +#' class = "shiny-report-theme" +#' ) +#' ), +#' function(input, output) { +#' output$p <- renderPlot({ +#' info <- getCurrentOutputInfo() +#' par(bg = info$bg(), fg = info$fg(), col.axis = info$fg(), col.main = info$fg()) +#' plot(1:10, col = info$accent(), pch = 19) +#' title("A simple R plot that uses its CSS styling") +#' }) +#' output$info <- renderText({ +#' info <- getCurrentOutputInfo() +#' jsonlite::toJSON( +#' list( +#' bg = info$bg(), +#' fg = info$fg(), +#' accent = info$accent(), +#' font = info$font() +#' ), +#' auto_unbox = TRUE +#' ) +#' }) +#' } +#' ) +#' } +#' +#' getCurrentOutputInfo <- function(session = getDefaultReactiveDomain()) { if (is.null(session)) return(NULL) session$getCurrentOutputInfo() diff --git a/man/getCurrentOutputInfo.Rd b/man/getCurrentOutputInfo.Rd index 1aa59b5cc3..11ec5ed9c7 100644 --- a/man/getCurrentOutputInfo.Rd +++ b/man/getCurrentOutputInfo.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/shiny.R \name{getCurrentOutputInfo} \alias{getCurrentOutputInfo} -\title{Get information about the output that is currently being executed.} +\title{Get output information} \usage{ getCurrentOutputInfo(session = getDefaultReactiveDomain()) } @@ -10,10 +10,72 @@ getCurrentOutputInfo(session = getDefaultReactiveDomain()) \item{session}{The current Shiny session.} } \value{ -A list with information about the current output, including the -\code{name} of the output. If no output is currently being executed, this will -return \code{NULL}. +\code{NULL} if called outside of an output context; otherwise, +a list which includes: +\itemize{ +\item The \code{name} of the output (reported for any output). +\item If the output is a \code{plotOutput()} or \code{imageOutput()}, then: +\itemize{ +\item \code{height}: a reactive expression which returns the height in pixels. +\item \code{width}: a reactive expression which returns the width in pixels. +} +\item If the output is a \code{plotOutput()}, \code{imageOutput()}, or contains a \code{shiny-report-theme} class, then: +\itemize{ +\item \code{bg}: a reactive expression which returns the background color. +\item \code{fg}: a reactive expression which returns the foreground color. +\item \code{accent}: a reactive expression which returns the hyperlink color. +\item \code{font}: a reactive expression which returns a list of font information, including: +\itemize{ +\item \code{families}: a character vector containing the CSS \code{font-family} property. +\item \code{size}: a character string containing the CSS \code{font-size} property +\item \code{renderedFamily}: if running in a browser that supports +\href{https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/check}{FontFaceSet.check}, +a character string containing the first of \code{families} available to the browser. +} +} +} } \description{ -Get information about the output that is currently being executed. +Returns information about the currently executing output, including its \code{name} (i.e., \code{outputId}); +and in some cases, relevant sizing and styling information. +} +\examples{ + +if (interactive()) { + shinyApp( + fluidPage( + tags$style(HTML("body {background-color: black; color: white; }")), + tags$style(HTML("body a {color: purple}")), + tags$style(HTML("#info {background-color: teal; color: orange; }")), + plotOutput("p"), + "Computed CSS styles for the output named info:", + tagAppendAttributes( + textOutput("info"), + class = "shiny-report-theme" + ) + ), + function(input, output) { + output$p <- renderPlot({ + info <- getCurrentOutputInfo() + par(bg = info$bg(), fg = info$fg(), col.axis = info$fg(), col.main = info$fg()) + plot(1:10, col = info$accent(), pch = 19) + title("A simple R plot that uses its CSS styling") + }) + output$info <- renderText({ + info <- getCurrentOutputInfo() + jsonlite::toJSON( + list( + bg = info$bg(), + fg = info$fg(), + accent = info$accent(), + font = info$font() + ), + auto_unbox = TRUE + ) + }) + } + ) +} + + }