【问题标题】:Is it possible to clear the displayed output in ShinyApp using actionButton是否可以使用 actionButton 清除 ShinyApp 中显示的输出
【发布时间】:2019-06-13 19:41:12
【问题描述】:

我正在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】:

    insertUI()removeUI() 是您可能正在寻找的。​​p>

    使用removeUI() 可以更轻松地删除元素:

      observeEvent(input$reset, {
        removeUI("#mytable")
      })
    

    为避免您不永久删除它,您可以使用insertUI()

      observeEvent(input$go, {
        insertUI("#placeholder", "afterEnd", ui = DT::dataTableOutput('mytable'))
      })
    

    为了正确放置元素,您可以在mainPanel() 中使用占位符:

    mainPanel(
      tags$div(id = "placeholder")
    )
    

    然后您可以从输入按钮中删除thedata() 的依赖关系,因为您现在使用insertUI()。 (你应该切换到insertUI(),否则你不能在没有它的情况下重新插入表格,......)

      thedata <- reactive({
         ...
      })
    

    完整示例如下:

    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(
          tags$div(id = "placeholder")
        )
      )
    )
    
    
    
    #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 <- reactive({
        input$go
        isolate({
    
          data_table<-data_table[data_table$cyl %in% input$cyl,]
    
    
          if(input$disp != 'All'){
            data_table<-data_table[data_table$disp %in% input$disp,]
          }
    
          return(data_table)
        })
      })
    
      observeEvent(input$reset, {
        removeUI("#mytable")
      })
    
      observeEvent(input$go, {
        insertUI("#placeholder", "afterEnd", ui = DT::dataTableOutput('mytable'))
      })
    
    
      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)
    )
    

    【讨论】:

    • 非常感谢伙计,这是一个完美的解决方案。真的很感激:)
    • 小提示,对于最小可重复的示例,七个包很多。 :)
    • 嗨,伙计,开始按钮中还有一个小问题。一旦按下 go 并添加 cyl 过滤器的值,表格就会自动更改。我希望表格只会在我按下 Go 按钮时改变。你能看看吗
    • 您可以为此使用isolate(),请参阅我的编辑。如果有其他要求,最好将其转移到新问题中。希望有帮助,..
    • 非常感谢你,伙计 :) 你真是个天才
    【解决方案2】:

    为什么不注入一些javascript?这样,您的代码几乎保持不变。

    使用以下代码在您的闪亮文件夹中创建一个js 文件(在此示例中为rmDt.js):

    $("#reset").click(function() {
      $(".display.dataTable.no-footer").DataTable().destroy();
      $(".display.dataTable.no-footer").DataTable().clear().draw();    
      $(".display.no-footer").DataTable().destroy();
      $(".display.no-footer").DataTable().clear().draw();    
    });
    

    保存此文件,然后将其注入闪亮的 R 脚本中:

    library(shiny)   
    library(DT)     
    library(dplyr) 
    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"),
          includeScript(path ="rmDt.js") # inject javascript
          ),
    
        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
      })
    
      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, options = list(launch.browser = T))
    

    【讨论】:

    • 非常感谢伙计,它适用于这个特定的数据集,但对于我得到的其他数据集和复杂的编码,由于某种原因它不起作用。也许我在这些代码中使用了 shinyjs。我仍然非常感谢您的努力:)
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多