如何使用第二个操作按钮

时间:2016-09-05 14:41:07

标签: r shiny

我有一个闪亮的应用程序,在按下某个操作按钮时会将数据帧写入输出。这是下面的简单例子中的“Go”按钮。我有一个重置按钮,可以重置输入的值。我想知道如何重置输出(因此当按下“reset”时它变为NULL&消失)。

我试图将input$goButtonReset传递给eventReactive函数(打算在里面使用if语句指示哪个按钮正在进行调用)但这似乎不可能

任何帮助非常感谢!

ui <- fluidPage(title = "Working Title",

sidebarLayout(

  sidebarPanel(width = 6,
  # *Input() functions
  selectInput("Input1", label = h3("Select Input1"),
              choices = list("A" = "A", NULL = "NULL"),
              selected = 1),
  actionButton("goButton", "Go!"),
  p("Click the button to display the table"),
  actionButton("goButtonReset", "Reset"),
  p("Click the button to reset your inputs.")
  ),

  mainPanel(
    # *Output() functions
    tableOutput("pf"))
  )

)

# build the outputs here
server <- function(input, output, session) {

  observeEvent(input$goButtonReset, {
    updateSelectInput(session, "Input1", selected = "NULL")
  })

  writePF <- eventReactive(input$goButton, {
    data.frame("test output")
  })

  output$pf <- renderTable({
    writePF()
  })

}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

您可以尝试使用reactiveValues来存储数据框。这对我有用:

    ui <- fluidPage(title = "Working Title",

                sidebarLayout(

                  sidebarPanel(width = 6,
                               # *Input() functions
                               selectInput("Input1", label = h3("Select Input1"),
                                           choices = list("A" = "A", NULL = "NULL"),
                                           selected = 1),
                               actionButton("goButton", "Go!"),
                               p("Click the button to display the table"),
                               actionButton("goButtonReset", "Reset"),
                               p("Click the button to reset your inputs.")
                  ),

                  mainPanel(
                    # *Output() functions
                    tableOutput("pf"))
                )

)

# build the outputs here
server <- function(input, output, session) {

  df <- reactiveValues()  

  observeEvent(input$goButton,{    
    df$writePF <- data.frame("test output")    
  })

  observeEvent(input$goButtonReset,{    
    df$writePF <- NULL    
  })


  output$pf <- renderTable({
    df$writePF
  })  

}

shinyApp(ui = ui, server = server)