是否可以使用actionButton

时间:2019-01-20 00:46:27

标签: r shiny shinydashboard dt action-button

我正在基于 mtcars数据构建ShinyApp。我有2个actionButtons (开始并清除)。 转到按钮用于在主面板上显示输出,而清除按钮用于清除该输出。 由于某些不可预见的原因,“我的清除”按钮不起作用。有人可以看看我的代码吗?我将非常感谢。

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)

2 个答案:

答案 0 :(得分:1)

insertUI()removeUI()是您所需要的。

使用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)
)

答案 1 :(得分:1)

为什么不注入一些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))