簡體   English   中英

閃亮-renderDataTable-bSearchable vs checkboxInput

[英]Shiny - renderDataTable - bSearchable vs checkboxInput

建立數據表時,我在合並兩個功能時遇到問題:

  1. 我使用“ bSearchable”選擇要使用搜索工具進行過濾的1列
  2. 我使用“ checkboxInput”來選擇用戶想要查看的列。

兩者都是分開工作的,但不能一起工作。 如果取消選中菜單輸入中的一列,數據將消失-就像應用過濾器一樣,未找到任何數據。 我怎樣才能解決這個問題?

          library(shiny)    
runApp(list(ui=(fluidPage(
      pageWithSidebar(
        headerPanel('Title'),
        sidebarPanel(
          helpText('Text about the table'),

          checkboxInput('columns','I want to select the columns' , value = FALSE),

          conditionalPanel(
            condition= "input.columns == true",
            checkboxGroupInput('show_vars', 'Select the columns that you want to see:', names(iris[1:4]),
                               selected =  names(iris[1:4]))
          ),

          downloadButton('downloadData', 'Download'),width = 3

        ),
        mainPanel(
          tags$head(tags$style("tfoot {display: table-header-group;}")),
          dataTableOutput("mytable1"),width = 9

        )
      ))
    )

    ,

    server=(function(input, output) {

      library(ggplot2) 
      library(XLConnect)  

      #DATA 
      tabel<- reactive({
        iris[,c(input$show_vars,"Species"), drop = FALSE]

      })

      #   OUTPUT   
      output$mytable1 = renderDataTable({
        tabel()}, 
        options = list(    
          aoColumns = list(list(bSearchable = FALSE), list(bSearchable = FALSE),list(bSearchable = FALSE),
                           list(bSearchable = FALSE),list(bSearchable = TRUE)),
          bFilter=1, bSortClasses = 1,aLengthMenu = list(c(10,25,50, -1), list('10','25', '50', 'Todas')),iDisplayLength = 10
        )

      )

      output$downloadData <- downloadHandler(
        filename = function() { paste('tabela_PSU','.xlsx', sep='') },
        content = function(file){
          fname <- paste(file,"xlsx",sep=".")
          wb <- loadWorkbook(fname, create = TRUE)
          createSheet(wb, name = "Sheet1")
          writeWorksheet(wb, tabel(), sheet = "Sheet1") 
          saveWorkbook(wb)
          file.rename(fname,file)
        },
      )


    })
    ))

問題是通過更改基於input$show_vars的數據iris ,您正在更改DataTable的列數。 但是,您已經定義了一個固定的 aoColumns選項,這意味着您的DataTable具有五列(四列不可搜索,一列可搜索)。

因此,當取消選擇任何復選框輸入時,過濾的數據與指定的選項不匹配。 結果,什么也不顯示。

也就是說,盡管您在DataTable中的數據是反應性的,但是選項不是反應性的

如果您仔細閱讀renderDataTable的文檔 ,將會看到可以將兩種類型的變量傳遞給options參數:

options傳遞給DataTables的初始化選項的列表,或者返回該列表的函數。

不同之處在於:

  • 如果您將options指定為列表,Shiny會假定options是固定的; 但是,由於要基於input$show_vars動態過濾數據,因此也應該動態更改aoColumns的選項。
  • 如果將函數作為options的參數傳遞,Shiny將知道這些options也是反應性的。 因此閃亮還將更新options時的數據(在你的情況下, data.frame中的反應變量命名封裝tabel更新)。

您可能已經知道, 反應變量本身就是函數 在反應性環境中對它們進行評估,並在評估時返回數據的當前狀態/值。 這就是為什么您將tabel()而不是tabelrenderDataTable

然后,解決方案是將整個options列表包裝到一個反應變量中(因此也有一個函數)。 具體來說,我們要動態設置aoColumns選項,以便bSearchable切換次數與DataTable中顯示的列數匹配。

下面,我僅顯示更新的server部分,因為在UI部分中無需更改任何內容。

server.R

shinyServer(function(input, output) {

  library(ggplot2) 
  library(XLConnect)  

  #DATA 
  tabel<- reactive({
    iris[,c(input$show_vars,"Species"), drop = FALSE]

  })

  # wrap the `options` into a reactive variable (hence a function) so that it will
  # be evaluated dynamically when the data changes as well. 
  # `dt_options` is reactive in the sense that it will reflect the number of rows
  # visible based on the checkboxInput selections.

  dt_options <- reactive({
    # dynamically create options for `aoColumns` depending on how many columns are selected.
    toggles <- lapply(1:length(input$show_vars), function(x) list(bSearchable = F))
    # for `species` columns
    toggles[[length(toggles) + 1]] <- list(bSearchable = T)

    list(
      aoColumns = toggles,
      bFilter = 1, bSortClasses = 1, 
      aLengthMenu = list(c(10,25,50, -1), list('10','25', '50', 'Todas')),
      iDisplayLength = 10
      )
  })

  #   OUTPUT
  output$mytable1 = renderDataTable({
    tabel()}, 
    options = dt_options
  )

  output$downloadData <- downloadHandler(
    filename = function() { paste('tabela_PSU','.xlsx', sep='') },
    content = function(file){
      fname <- paste(file,"xlsx",sep=".")
      wb <- loadWorkbook(fname, create = TRUE)
      createSheet(wb, name = "Sheet1")
      writeWorksheet(wb, tabel(), sheet = "Sheet1") 
      saveWorkbook(wb)
      file.rename(fname,file)
    },
  )

})

(請注意,我將UI部分和服務器部分分為ui.Rserver.R 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM