Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: How to make reactive value initialize with default value

Tags:

r

shiny

Consider the following actionButton demo: http://shiny.rstudio.com/gallery/actionbutton-demo.html

server.R:

shinyServer(function(input, output) {    # builds a reactive expression that only invalidates    # when the value of input$goButton becomes out of date    # (i.e., when the button is pressed)   ntext <- eventReactive(input$goButton, {     input$n   })    output$nText <- renderText({     ntext()   }) }) 

ui.R:

shinyUI(pageWithSidebar(   headerPanel("actionButton test"),   sidebarPanel(     numericInput("n", "N:", min = 0, max = 100, value = 50),     br(),     actionButton("goButton", "Go!"),     p("Click the button to update the value displayed in the main panel.")   ),   mainPanel(     verbatimTextOutput("nText")   ) )) 

In this example, prior to the action button being pressed, the right-hand side panel is empty. I would instead like the text with default value "50" to be rendered by default.

How to I get the output to display with default inputs if the action button has not yet been pressed?

like image 345
Eric Avatar asked Nov 11 '15 23:11

Eric


People also ask

How do you make a reactive function Shiny?

A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).

What are reactive values in R Shiny?

Reactive values contain values (not surprisingly), which can be read by other reactive objects. The input object is a ReactiveValues object, which looks something like a list, and it contains many individual reactive values. The values in input are set by input from the web browser.

What is isolate Shiny in R?

The isolate function lets you read a reactive value or expression without establishing this relationship. The expression given to isolate() is evaluated in the calling environment. This means that if you assign a variable inside the isolate() , its value will be visible outside of the isolate() .


2 Answers

eventReactive also takes ignoreNULL as documented here, which lets you initialise the object without an if statement.

By adding the ,ignoreNULL = FALSE to the original post (give or take some formatting), verbatimTextOutput shows 50 on startup.

This makes for a bit of economy on the server side I guess.

ui <- fluidPage(titlePanel("actionButton test"),                 sidebarLayout(                   sidebarPanel(                     numericInput(                       "n",                       "N:",                       min = 0,                       max = 100,                       value = 50                     ),                     br(),                     actionButton("goButton", "Go!"),                     p("Click the button to update the value displayed in the main panel.")                   ),                   mainPanel(verbatimTextOutput("nText"))                 ))  server <- function(input, output) {    ntext <- eventReactive(input$goButton, {     input$n   }   # Adding this parameter to the original example makes it work as intended   # with 50 in the output field to begin with   , ignoreNULL = FALSE   )    output$nText <- renderText({     ntext()   }) }  shinyApp(ui = ui, server = server) 
like image 149
Agronymous Cowherd Avatar answered Sep 16 '22 14:09

Agronymous Cowherd


    shinyServer(function(input, output) {       values <- reactiveValues(default = 0)        observeEvent(input$goButton,{            values$default <- input$goButton       })       # builds a reactive expression that only invalidates        # when the value of input$goButton becomes out of date        # (i.e., when the button is pressed)       ntext <- eventReactive(input$goButton, {            input$n       })        output$nText <- renderText({          if(values$default == 0){               50          }          else{             ntext()          }       })     }) 
like image 39
Krishna Avatar answered Sep 18 '22 14:09

Krishna