Initial state of eventReactive()

我与影子孤独终老i 提交于 2020-01-07 03:00:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35792052/initial-state-of-eventreactive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!