observeEvent()和reactiveValues() - 如何创建好的占位符

时间:2015-09-15 12:21:10

标签: r shiny

我创建了一个应用,其中有两个actionButtons

ui.R
        h4("First | Second "),
        actionButton(inputId = "first", 
                     label = "First"),
        actionButton(inputId = "second", 
                     label = "Second"),

这就是我的server.R

#create the datasetbased on the choice of "First" or "Second"
myDataSet <- reactiveValues(data = NULL)


#observer to see what is clicked. Decision is send to "reactiveValues"
observeEvent(input$first, {
        myDataSet$data <- dataframe1
})


#observer to see what is clicked. Decision is send to "reactiveValues"
observeEvent(input$second, {
        myDataSet$data <- dataframe2
})

取决于来自actionButton的{​​{1}}来自dataframe1的{​​{1}}或dataframe2的输入。 如上所述:这是myDataSet创建将保留reactiveValues()的变量的方式吗?请注意,dataframesdataframes发送的扩展(列/行)会有所不同。

1 个答案:

答案 0 :(得分:1)

这是整个工作示例

ui <- fluidPage( 
    h4("First | Second "),
    actionButton(inputId = "first", label = "First"),
    actionButton(inputId = "second", label = "Second"),
    htmlOutput("nrows")
)
server <- function(input, output, session) {
    dataframe1 <- data.frame(c(1,2,3))
    dataframe2 <- data.frame(c(1,2,3,4,5))
    #create the datasetbased on the choice of "First" or "Second"
    myDataSet <- reactiveValues(data = NULL)

    #observer to see what is clicked. Decision is send to "reactiveValues"
    observeEvent(input$first, {
            myDataSet$data <- dataframe1
    })

    #observer to see what is clicked. Decision is send to "reactiveValues"
    observeEvent(input$second, {
            myDataSet$data <- dataframe2
    })
    output$nrows <- renderUI({  
            HTML(paste('dataframe rows:', nrow(myDataSet$data)))    
    })
}
shinyApp(ui, server)