Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger R actionbutton event when session opens

Tags:

r

shiny

In a Shiny app, I am trying to have an eventReactive triggered either with an actionbutton OR when the session opens. I've tryed the below code using session$clientData but it doesn't work. Also tried to play with toggleModal from shinyBS but still no chance. Any help much appreciated. Thanks!

library(shiny)

ui <- fluidPage(
  actionButton("go", "Go"),
  numericInput("n", "n", 50),
  plotOutput("plot")
)

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

  randomVals <- eventReactive({input$go
  session$clientData}, {
    runif(input$n)
  })

  output$plot <- renderPlot({
    hist(randomVals())
  })
}

shinyApp(ui, server)
like image 203
Guillaume de Bénazé Avatar asked Sep 15 '25 10:09

Guillaume de Bénazé


1 Answers

Actually figured this out. On my question above, I tried to simplified my code, and doing so actually fixed the issue...

randomVals <- eventReactive({input$go
  session$clientData}

works as expected (i.e. you get a chart when opening the session even without clicking on go), while

randomVals <- eventReactive({session$clientData
  input$go}

doesn't work (i.e you need to click on go to get your first chart So I guess the order in the event {} matters, which I didn't know

like image 120
Guillaume de Bénazé Avatar answered Sep 18 '25 01:09

Guillaume de Bénazé