如何使用一个按钮中定义的数据框观察另一个按钮的观察事件?

时间:2016-08-19 04:21:33

标签: r shiny

这是代码:

按钮1是actionbuttonobserveevent定义如下

 observeEvent(
    input$button1,{


   mid<-c("1","2")

   name<-c("a","b")

  datatable1<-data.frame(mid,name)

output$deatilscv <- renderUI({

 div(id="div1",

          fluidPage(shinyjs::useShinyjs(),

   actionButton("button2", "CLICK")  )) )}


observeEvent(

    input$button2,{

a<-datatable1(1,1)  #this shows an error datatable1 not found

print(a)
})

如何访问datatable1

observeevent内的actionbutton button2个详细信息

2 个答案:

答案 0 :(得分:0)

这是一个有效的例子:

ui.R

library(shiny)

    shinyUI(fluidPage(
      titlePanel("Example"),
      sidebarLayout(
        sidebarPanel(
            actionButton("button1", "button1")
        ),
        mainPanel(
           uiOutput("deatilscv")
        )
      )
    ))

server.R

library(shiny)
    library(shinyjs)

    shinyServer(function(input, output) {
      datatable1 <- eventReactive(input$button1, {
              mid<-c("1","2")
              name<-c("a","b")
              tmp <- data.frame(mid,name)
      })
      output$deatilscv <- renderUI({
              if(!input$button1) return()
              div(id="div1",

                  fluidPage(shinyjs::useShinyjs(),

                            actionButton("button2", "CLICK")  ))
      })
      observeEvent(

              input$button2,{

                      a <- datatable1()[1,1]  #this shows an error datatable1 not found

                      print(a)
              })  
    })

请注意,observe事件中的print(a)将打印到控制台。

答案 1 :(得分:0)

单选按钮版本

ui.R

    library(shiny)

    shinyUI(fluidPage(
      titlePanel("Example"),
      sidebarLayout(
        sidebarPanel(
                radioButtons("rb1", "Select options", choices = c("Choice1", "Choice2"))
        ),
        mainPanel(
           uiOutput("deatilscv1"),
           uiOutput("deatilscv2")
        )
      )
    ))

server.R

    library(shiny)
    library(shinyjs)

    shinyServer(function(input, output) {
      datatable1 <- eventReactive(input$rb1, {

              if (input$rb1 == "Choice1") {
                      mid<-c("1","2")
                      name<-c("a","b")
                      tmp <- data.frame(mid,name)                         
              } else {
              mid<-c("3","4")
              name<-c("c","d")
              tmp <- data.frame(mid,name)
              }
              tmp
      })
      output$deatilscv1 <- renderUI({
              if(input$rb1 != "Choice1") return()
              div(id="div1",

                  fluidPage(shinyjs::useShinyjs(),

                            actionButton("button2", "CLICK1"),
                            renderDataTable(DT::datatable(datatable1()))))
      })
      output$deatilscv2 <- renderUI({
              if(input$rb1 !="Choice2") return()
              div(id="div1",

                  fluidPage(shinyjs::useShinyjs(),

                            actionButton("button3", "CLICK2"),
                            renderDataTable(DT::datatable(datatable1()))))
      })
      observeEvent(

              input$button2,{

                      a <- datatable1()[1,1]  #this shows an error datatable1 not found

                      print(a)
              })  
      observeEvent(

              input$button3,{

                      a <- datatable1()[1,1]  #this shows an error datatable1 not found

                      print(a)
              }) 
    })