【问题标题】:Unable to clear the displayed output in ShinyApp using actionButton无法使用 actionButton 清除 ShinyApp 中显示的输出
【发布时间】:2019-06-13 14:36:44
【问题描述】:

我正在mtcars 数据 上构建一个shinyApp。我有 2 个 actionButtons (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)

【问题讨论】:

    标签: r shiny shinydashboard dt action-button


    【解决方案1】:

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

    【讨论】:

    • 嗨,伙计,对不起,我忘了放那部分,我已经用标签添加了。请再看看。谢谢:)
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2017-08-05
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多