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?
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).
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.
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() .
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)
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() } }) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With