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?
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)