Dallin
Dallin

Reputation: 1074

Trigger event or call function on initialization

I have an action button with an observer, which loads a chart and some text fields based on user-provided input. When a button is clicked, my function is called, which is all well and good. However, what I would like is for the function to run when the page first loads.

I'm not sure if I should be trying to manually trigger the event (or how to go about doing that), or is there a way to call a function on initialization?

library(shiny)

ui <- fluidPage(
  actionButton("run", "Run"),
  textInput("input_field")
  plotOutput("plot")
)

server <- function(input, output, session) {

  output$text1 <- renderText({
    return('text 1')
  })

  output$text2 <- renderText({
    //use input field to get correct data
    return(getTextOutput(input$input_field))
  })

  output$plot <- renderPlot({
     //use input field to get correct data
     plot(getChartData(input$input_field))
  })

  observeEvent(input$run, {
    populateScreen(input, output)
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 931

Answers (1)

Dallin
Dallin

Reputation: 1074

To make the event to run when it's first created, add ignoreInit = FALSE, ignoreNULL = FALSE as parameters to observeEvent

observeEvent(input$submit_streaks, {
    populateStreaks(input, output)
}, ignoreInit = FALSE, ignoreNULL = FALSE)

From the documentation,

ignoreNULL = FALSE and ignoreInit = FALSE

This combination means that handlerExpr will run every time no matter what.

Upvotes: 1

Related Questions