0

我正在mtcars 数据上构建一个 shinyApp 。我有 2 个动作按钮(Go & Clear)。Go 按钮用于 在mainPanel 上显示输出,而Clear 按钮用于清除该输出。由于某些不可预见的原因,我的清除按钮无法正常工作。有人可以看看我的代码。我将不胜感激。

library(shiny)   
library(DT)     
library(dplyr) 
library(shinythemes) 
library(htmlwidgets) 
library(shinyWidgets) 
library(shinydashboard)

data_table<-mtcars

#ui
ui = fluidPage( 
  sidebarLayout(
    sidebarPanel (

      uiOutput("cyl_selector"),
      uiOutput("disp_selector"),

      actionButton(inputId = "go", label = "Go"),
      actionButton(inputId = "reset", label = "Clear")),


    mainPanel(
           DT::dataTableOutput('mytable') )))



#server
server = function(input, output, session) {

  output$cyl_selector <- renderUI({

    selectInput(inputId = "cyl",
                label = "cyl:", multiple = TRUE,
                choices = c( unique(as.character(data_table$cyl))),
                selected = c('4')) })


  output$disp_selector <- renderUI({

    available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]  

    selectInput(
      inputId = "disp", 
      label = "disp:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = 'All') })


  thedata <- eventReactive(input$go,{

    data_table<-data_table[data_table$cyl %in% input$cyl,]


    if(input$disp != 'All'){
      data_table<-data_table[data_table$disp %in% input$disp,]
    }

    data_table
 })


 # thedata <- eventReactive(input$reset,{
 #   data_table<-NULL
 # })


  output$mytable = DT::renderDataTable({

    DT::datatable( filter = "top",  rownames = FALSE, escape = FALSE,
                   options = list(pageLength = 50, autowidth=FALSE,
                                  dom = 'Brtip'  ),
                   {     
                     thedata()   # Call reactive thedata()
                   })
 })}  
shinyApp(ui = ui, server = server)
4

1 回答 1

0

我没有完全分析您的脚本,但我可以看到它根本没有调用第二个按钮(Clear)。您使用input$go为第一个按钮制作了一个eventReactive()来制作绘图,但如果您想让它工作,您也需要调用input$reset 。

于 2019-01-19T16:53:47.110 回答