Change reactiveXX to renderXX

This commit is contained in:
Winston Chang
2013-02-08 15:37:11 -06:00
parent d4ab84745d
commit e26f175a8f
16 changed files with 115 additions and 82 deletions

View File

@@ -708,7 +708,7 @@ tabsetPanel <- function(..., id = NULL) {
#' @param outputId output variable to read the value from
#' @return A text output element that can be included in a panel
#' @details Text is HTML-escaped prior to rendering. This element is often used
#' to dispaly \link{reactiveText} output variables.
#' to dispaly \link{renderText} output variables.
#' @examples
#' h3(textOutput("caption"))
#' @export
@@ -723,7 +723,7 @@ textOutput <- function(outputId) {
#' @param outputId output variable to read the value from
#' @return A verbatim text output element that can be included in a panel
#' @details Text is HTML-escaped prior to rendering. This element is often used
#' with the \link{reactivePrint} function to preserve fixed-width formatting
#' with the \link{renderPrint} function to preserve fixed-width formatting
#' of printed objects.
#' @examples
#' mainPanel(
@@ -740,7 +740,7 @@ verbatimTextOutput <- function(outputId) {
#' Create a plot output element
#'
#' Render a \link{reactivePlot} within an application page.
#' Render a \link{renderPlot} within an application page.
#' @param outputId output variable to read the plot from
#' @param width Plot width. Must be a valid CSS unit (like \code{"100\%"},
#' \code{"400px"}, \code{"auto"}) or a number, which will be coerced to a
@@ -761,7 +761,7 @@ plotOutput <- function(outputId, width = "100%", height="400px") {
#' Create a table output element
#'
#' Render a \link{reactiveTable} within an application page.
#' Render a \link{renderTable} within an application page.
#' @param outputId output variable to read the table from
#' @return A table output element that can be included in a panel
#' @examples
@@ -779,7 +779,7 @@ tableOutput <- function(outputId) {
#' text will be included within an HTML \code{div} tag, and is presumed to
#' contain HTML content which should not be escaped.
#'
#' \code{uiOutput} is intended to be used with \code{reactiveUI} on the
#' \code{uiOutput} is intended to be used with \code{renderUI} on the
#' server side. It is currently just an alias for \code{htmlOutput}.
#'
#' @param outputId output variable to read the value from

View File

@@ -703,7 +703,7 @@ resourcePathHandler <- function(ws, header) {
#' # A very simple Shiny app that takes a message from the user
#' # and outputs an uppercase version of it.
#' shinyServer(function(input, output) {
#' output$uppercase <- reactiveText(function() {
#' output$uppercase <- renderText({
#' toupper(input$message)
#' })
#' })

View File

@@ -32,7 +32,15 @@ suppressPackageStartupMessages({
#' These can be used to set the width, height, background color, etc.
#'
#' @export
renderPlot <- function(func, width='auto', height='auto', ...) {
renderPlot <- function(expr, width='auto', height='auto', ...,
env=parent.frame(), quoted=FALSE, func=NULL) {
if (!is.null(func)) {
shinyDeprecated(msg="renderPlot: argument 'func' is deprecated. Please use 'expr' instead.")
} else {
func <- exprToFunction(expr, env, quoted)
}
args <- list(...)
if (is.function(width))
@@ -108,7 +116,13 @@ renderPlot <- function(func, width='auto', height='auto', ...) {
#' \code{\link[xtable]{print.xtable}}.
#'
#' @export
renderTable <- function(func, ...) {
renderTable <- function(expr, ..., env=parent.frame(), quoted=FALSE, func=NULL) {
if (!is.null(func)) {
shinyDeprecated(msg="renderTable: argument 'func' is deprecated. Please use 'expr' instead.")
} else {
func <- exprToFunction(expr, env, quoted)
}
function() {
classNames <- getOption('shiny.table.class', 'data table table-bordered table-condensed')
data <- func()
@@ -155,7 +169,13 @@ renderTable <- function(func, ...) {
#' @example res/text-example.R
#'
#' @export
renderPrint <- function(func) {
renderPrint <- function(expr, env=parent.frame(), quoted=FALSE, func=NULL) {
if (!is.null(func)) {
shinyDeprecated(msg="renderPrint: argument 'func' is deprecated. Please use 'expr' instead.")
} else {
func <- exprToFunction(expr, env, quoted)
}
function() {
return(paste(capture.output({
result <- withVisible(func())
@@ -181,13 +201,19 @@ renderPrint <- function(func) {
#' @param func A function that returns an R object that can be used as an
#' argument to \code{cat}.
#'
#' @seealso \code{\link{reactivePrint}} for capturing the print output of a
#' @seealso \code{\link{renderPrint}} for capturing the print output of a
#' function, rather than the returned text value.
#'
#' @example res/text-example.R
#'
#' @export
renderText <- function(func) {
renderText <- function(expr, env=parent.frame(), quoted=FALSE, func=NULL) {
if (!is.null(func)) {
shinyDeprecated(msg="renderText: argument 'func' is deprecated. Please use 'expr' instead.")
} else {
func <- exprToFunction(expr, env, quoted)
}
function() {
value <- func()
return(paste(capture.output(cat(value)), collapse="\n"))
@@ -216,7 +242,13 @@ renderText <- function(func) {
#' )
#' })
#' }
renderUI <- function(func) {
renderUI <- function(expr, env=parent.frame(), quoted=FALSE, func=NULL) {
if (!is.null(func)) {
shinyDeprecated(msg="renderUI: argument 'func' is deprecated. Please use 'expr' instead.")
} else {
func <- exprToFunction(expr, env, quoted)
}
function() {
result <- func()
if (is.null(result) || length(result) == 0)
@@ -277,21 +309,21 @@ downloadHandler <- function(filename, content, contentType=NA) {
# Deprecated functions ------------------------------------------------------
reactivePlot <- function(func, width='auto', height='auto', ...) {
shinyDeprecated(new="renderPlot")
renderPlot(func, width='auto', height='auto', ...)
renderPlot({ func() }, width='auto', height='auto', ...)
}
reactiveTable <- function(func, ...) {
shinyDeprecated(new="renderTable")
renderTable(func, ...)
renderTable({ func() })
}
reactivePrint <- function(func) {
shinyDeprecated(new="renderPrint")
renderPrint(func)
renderPrint({ func() })
}
reactiveUI <- function(func) {
shinyDeprecated(new="renderUI")
renderUI(func)
renderUI({ func() })
}
reactiveText <- function(func) {
shinyDeprecated(new="renderText")
renderText(func)
renderText({ func() })
}

View File

@@ -136,7 +136,8 @@ exprToFunction <- function(expr, env=parent.frame(2), quoted=FALSE) {
# tokens, then [[ works. In the former case it will be a name object; in the
# latter, it will be a language object.
if (!is.name(expr_sub) && expr_sub[[1]] == as.name('function')) {
called_fun <- sys.call(1)[[1]]
# Get name of function that called this function
called_fun <- sys.call(-1)[[1]]
shinyDeprecated(msg = paste("Passing functions to '", called_fun,
"' is deprecated. Please use expressions instead. See ?", called_fun,

View File

@@ -3,14 +3,14 @@ library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
# Function that generates a plot of the distribution. The function
# is wrapped in a call to reactivePlot to indicate that:
# Expression that generates a plot of the distribution. The expression
# is wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
#
output$distPlot <- reactivePlot(function() {
output$distPlot <- renderPlot({
# generate an rnorm distribution and plot it
dist <- rnorm(input$obs)

View File

@@ -13,13 +13,13 @@ shinyServer(function(input, output) {
})
# Generate a summary of the dataset
output$summary <- reactivePrint(function() {
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations
output$view <- reactiveTable(function() {
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
})

View File

@@ -4,12 +4,12 @@ library(datasets)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
# By declaring databaseInput as a reactive function we ensure that:
# By declaring databaseInput as a reactive expression we ensure that:
#
# 1) It is only called when the inputs it depends on changes
# 2) The computation and result are shared by all the callers (it
# only executes a single time)
# 3) When the inputs change and the function is re-executed, the
# 3) When the inputs change and the expression is re-executed, the
# new result is compared to the previous result; if the two are
# identical, then the callers are not notified
#
@@ -20,31 +20,31 @@ shinyServer(function(input, output) {
"cars" = cars)
})
# The output$caption is computed based on a reactive function that
# The output$caption is computed based on a reactive expression that
# returns input$caption. When the user changes the "caption" field:
#
# 1) This function is automatically called to recompute the output
# 2) The new caption is pushed back to the browser for re-display
#
# Note that because the data-oriented reactive functions below don't
# depend on input$caption, those functions are NOT called when
# Note that because the data-oriented reactive expressions below don't
# depend on input$caption, those expressions are NOT called when
# input$caption changes.
output$caption <- reactiveText(function() {
output$caption <- renderText({
input$caption
})
# The output$summary depends on the datasetInput reactive function,
# The output$summary depends on the datasetInput reactive expression,
# so will be re-executed whenever datasetInput is re-executed
# (i.e. whenever the input$dataset changes)
output$summary <- reactivePrint(function() {
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# The output$view depends on both the databaseInput reactive function
# The output$view depends on both the databaseInput reactive expression
# and input$obs, so will be re-executed whenever input$dataset or
# input$obs is changed.
output$view <- reactiveTable(function() {
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
})

View File

@@ -11,20 +11,20 @@ mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual"))
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# Compute the forumla text in a reactive expression since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive({
paste("mpg ~", input$variable)
})
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
output$caption <- renderText({
formulaText()
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$mpgPlot <- reactivePlot(function() {
output$mpgPlot <- renderPlot({
boxplot(as.formula(formulaText()),
data = mpgData,
outline = input$outliers)

View File

@@ -3,7 +3,7 @@ library(shiny)
# Define server logic for slider examples
shinyServer(function(input, output) {
# Reactive function to compose a data frame containing all of the values
# Reactive expression to compose a data frame containing all of the values
sliderValues <- reactive({
# Compose data frame
@@ -22,7 +22,7 @@ shinyServer(function(input, output) {
})
# Show the values using an HTML table
output$values <- reactiveTable(function() {
output$values <- renderTable({
sliderValues()
})
})

View File

@@ -3,9 +3,9 @@ library(shiny)
# Define server logic for random distribution application
shinyServer(function(input, output) {
# Reactive function to generate the requested distribution. This is
# Reactive expression to generate the requested distribution. This is
# called whenever the inputs change. The output functions defined
# below then all use the value computed from this function
# below then all use the value computed from this expression
data <- reactive({
dist <- switch(input$dist,
norm = rnorm,
@@ -19,9 +19,9 @@ shinyServer(function(input, output) {
# Generate a plot of the data. Also uses the inputs to build the
# plot label. Note that the dependencies on both the inputs and
# the data reactive function are both tracked, and all functions
# the data reactive expression are both tracked, and all expressions
# are called in the sequence implied by the dependency graph
output$plot <- reactivePlot(function() {
output$plot <- renderPlot({
dist <- input$dist
n <- input$n
@@ -30,12 +30,12 @@ shinyServer(function(input, output) {
})
# Generate a summary of the data
output$summary <- reactivePrint(function() {
output$summary <- renderPrint({
summary(data())
})
# Generate an HTML table view of the data
output$table <- reactiveTable(function() {
output$table <- renderTable({
data.frame(x=data())
})

View File

@@ -13,13 +13,13 @@ shinyServer(function(input, output) {
})
# Generate a summary of the dataset
output$summary <- reactivePrint(function() {
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations
output$view <- reactiveTable(function() {
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
})

View File

@@ -3,9 +3,9 @@ library(shiny)
# Define server logic for random distribution application
shinyServer(function(input, output) {
# Reactive function to generate the requested distribution. This is
# called whenever the inputs change. The output functions defined
# below then all used the value computed from this function
# Reactive expression to generate the requested distribution. This is
# called whenever the inputs change. The output expressions defined
# below then all used the value computed from this expression
data <- reactive({
dist <- switch(input$dist,
norm = rnorm,
@@ -19,9 +19,9 @@ shinyServer(function(input, output) {
# Generate a plot of the data. Also uses the inputs to build the
# plot label. Note that the dependencies on both the inputs and
# the data reactive function are both tracked, and all functions
# the data reactive expression are both tracked, and all expressions
# are called in the sequence implied by the dependency graph
output$plot <- reactivePlot(function() {
output$plot <- renderPlot({
dist <- input$dist
n <- input$n
@@ -30,12 +30,12 @@ shinyServer(function(input, output) {
})
# Generate a summary of the data
output$summary <- reactivePrint(function() {
output$summary <- renderPrint({
summary(data())
})
# Generate an HTML table view of the data
output$table <- reactiveTable(function() {
output$table <- renderTable({
data.frame(x=data())
})

View File

@@ -1,7 +1,7 @@
library(shiny)
shinyServer(function(input, output) {
output$contents <- reactiveTable(function() {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects and uploads a
# file, it will be a data frame with 'name', 'size', 'type', and 'data'

View File

@@ -6,7 +6,7 @@ shinyServer(function(input, output) {
"cars" = cars)
})
output$table <- reactiveTable(function() {
output$table <- renderTable({
datasetInput()
})

View File

@@ -1,33 +1,33 @@
context("text")
test_that("reactivePrint and reactiveText behavior is correct", {
expect_equal(isolate(reactivePrint(function() "foo")()),
test_that("renderPrint and renderText behavior is correct", {
expect_equal(isolate(renderPrint(function() "foo")()),
'[1] "foo"')
expect_equal(isolate(reactivePrint(function() invisible("foo"))()),
expect_equal(isolate(renderPrint(function() invisible("foo"))()),
'')
expect_equal(isolate(reactivePrint(function() { print("foo"); "bar"})()),
expect_equal(isolate(renderPrint(function() { print("foo"); "bar"})()),
'[1] "foo"\n[1] "bar"')
expect_equal(isolate(reactivePrint(function() NULL)()),
expect_equal(isolate(renderPrint(function() NULL)()),
'NULL')
expect_equal(isolate(reactivePrint(function() invisible())()),
expect_equal(isolate(renderPrint(function() invisible())()),
'')
expect_equal(isolate(reactivePrint(function() 1:5)()),
expect_equal(isolate(renderPrint(function() 1:5)()),
'[1] 1 2 3 4 5')
expect_equal(isolate(reactiveText(function() "foo")()),
expect_equal(isolate(renderText(function() "foo")()),
'foo')
expect_equal(isolate(reactiveText(function() invisible("foo"))()),
expect_equal(isolate(renderText(function() invisible("foo"))()),
'foo')
# Capture the print output so it's not shown on console during test, and
# also check that it is correct
print_out <- capture.output(ret <- isolate(reactiveText(function() { print("foo"); "bar"})()))
print_out <- capture.output(ret <- isolate(renderText(function() { print("foo"); "bar"})()))
expect_equal(ret, 'bar')
expect_equal(print_out, '[1] "foo"')
expect_equal(isolate(reactiveText(function() NULL)()),
expect_equal(isolate(renderText(function() NULL)()),
'')
expect_equal(isolate(reactiveText(function() invisible())()),
expect_equal(isolate(renderText(function() invisible())()),
'')
expect_equal(isolate(reactiveText(function() 1:5)()),
expect_equal(isolate(renderText(function() 1:5)()),
'1 2 3 4 5')
})

View File

@@ -1,61 +1,61 @@
isolate({
# reactivePrint captures any print output, converts it to a string, and
# renderPrint captures any print output, converts it to a string, and
# returns it
visFun <- reactivePrint(function() "foo")
visFun <- renderPrint(function() "foo")
visFun()
# '[1] "foo"'
invisFun <- reactivePrint(function() invisible("foo"))
invisFun <- renderPrint(function() invisible("foo"))
invisFun()
# ''
multiprintFun <- reactivePrint(function() {
multiprintFun <- renderPrint(function() {
print("foo");
"bar"
})
multiprintFun()
# '[1] "foo"\n[1] "bar"'
nullFun <- reactivePrint(function() NULL)
nullFun <- renderPrint(function() NULL)
nullFun()
# 'NULL'
invisNullFun <- reactivePrint(function() invisible(NULL))
invisNullFun <- renderPrint(function() invisible(NULL))
invisNullFun()
# ''
vecFun <- reactivePrint(function() 1:5)
vecFun <- renderPrint(function() 1:5)
vecFun()
# '[1] 1 2 3 4 5'
# Contrast with reactiveText, which takes the value returned from the function
# Contrast with renderText, which takes the value returned from the function
# and uses cat() to convert it to a string
visFun <- reactiveText(function() "foo")
visFun <- renderText(function() "foo")
visFun()
# 'foo'
invisFun <- reactiveText(function() invisible("foo"))
invisFun <- renderText(function() invisible("foo"))
invisFun()
# 'foo'
multiprintFun <- reactiveText(function() {
multiprintFun <- renderText(function() {
print("foo");
"bar"
})
multiprintFun()
# 'bar'
nullFun <- reactiveText(function() NULL)
nullFun <- renderText(function() NULL)
nullFun()
# ''
invisNullFun <- reactiveText(function() invisible(NULL))
invisNullFun <- renderText(function() invisible(NULL))
invisNullFun()
# ''
vecFun <- reactiveText(function() 1:5)
vecFun <- renderText(function() 1:5)
vecFun()
# '1 2 3 4 5'