Skip to content

Instantly share code, notes, and snippets.

@wch
Created March 20, 2014 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wch/9672435 to your computer and use it in GitHub Desktop.
Save wch/9672435 to your computer and use it in GitHub Desktop.
isolate demo for R Shiny

In this example, changing n will not result in an update to the summary text, because it is always used within isolate() on the server side.

However, changing text or clicking on the button will result in the summary text updating, because when input$text and input$goButton are accessed it is not inside isolate().

Type: Shiny
Title: isolate demo
License: MIT
Author: Winston Chang <winston@rstudio.com>
AuthorUrl: http://www.rstudio.com/
Tags: isolate
DisplayMode: Showcase
shinyServer(function(input, output) {
output$summary <- renderText({
# Simply accessing input$goButton here makes this reactive
# object take a dependency on it. That means when
# input$goButton changes, this code will re-execute.
input$goButton
# input$text is accessed here, so this reactive object will
# take a dependency on it. However, input$ is inside of
# isolate(), so this reactive object will NOT take a
# dependency on it; changes to input$n will therefore not
# trigger re-execution.
paste0('input$text is "', input$text,
'", and input$n is ', isolate(input$n))
})
# In the code above, the call to isolate() is used inline in
# a function call. However, isolate can take any expression,
# as shown in the code below.
output$summary2 <- renderText({
input$goButton
str <- paste0('input$text is "', input$text, '"')
# Any sort of expression can go in isolate()
isolate({
str <- paste0(str, ', and input$n is ')
paste0(str, isolate(input$n))
})
})
})
shinyUI(fluidPage(
titlePanel("isolate example"),
fluidRow(
column(4, wellPanel(
sliderInput("n", "n (isolated):",
min = 10, max = 1000, value = 200, step = 10),
textInput("text", "text (not isolated):", "input text"),
br(),
actionButton("goButton", "Go!")
)),
column(8,
h4("summary"),
textOutput("summary"),
h4("summary2"),
textOutput("summary2")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment