Sebastian
Sebastian

Reputation: 2570

Initial state of eventReactive()

I'm using an reactive event in my shiny app, the reactive output is part of a renderDataTable and I want to change the initial outcome of that function.

From the documentary I read: "eventReactive() returns NULL until the action button is clicked. As a result, the graph does not appear until the user asks for it by clicking “Go”." But this does not work with my code.

Reproductive Example:

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    actionButton("go","Go"),
    DT::dataTableOutput('tbl')),
  server = function(input, output) {

    dat <- eventReactive(input$go,
                   head(iris,10)
                         )

    output$tbl <- renderDataTable({

      if(is.null(dat())){
        data.frame("a"=NA,"b"=NA,"c"=NA,"d"=NA,"e"=NA)
      }else(data.frame(dat()))

    })  

  }
)

This should return an empty data.frame with 5 variables before the button is clicked, which it is not.

As the function reacts after the button is clicked, I think the state of the function was NULL before, as it is part of the if statement.

Upvotes: 0

Views: 2956

Answers (1)

Sebastian
Sebastian

Reputation: 2570

I tried a workaround by checking the state of the button instead of the reactiveEvent which works.

  if(input$go==0){
    data.frame("a"=NA,"b"=NA,"c"=NA,"d"=NA,"e"=NA)
  }else(data.frame(dat()))

Initial state is 0 and incremented by 1 each time the button is clicked.

Nonetheless, if anybody knows how the reactiveEvent initial state works and could elaborate on it I would appreciate it.

Upvotes: 1

Related Questions