如何在 Shiny 中 select 行和列并使用 DT 进行过滤?

How to select rows and columns and filter with DT in Shiny?

我正在尝试创建一个闪亮的应用程序 能够 select 行和列 取决于用户的选择。对于 selecting 列,我使用 pickerInput,对于 selecting 行,扩展名“Select”以及您可以在代码中看到的更多选项(我看到了这个 它工作得很好。)

问题是 我收到此警告 并且我认为它与 select 离子的类型不兼容。恐怕我没有看到的东西会产生更多错误。

警告:Select 扩展无法正常使用服务器端处理模式。建议仅在客户端处理模式下使用 Select 扩展(通过在 DT::renderDT() 中设置 server = FALSE )或使用 DT 自己的 selection 实现(参见 selection参数在?DT::datatable).

另一方面,当我select“行”时,我不知道如何隐藏“pickerInput”中的“Selection Input”和操作按钮。

这是代码:

    library(shiny)
    library(shinyWidgets)
    library(dplyr)

ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      radioButtons("type_select", "What do you want to select?",
                   c("Rows" = "Rows",
                     "Columns" = "Columns")),
      uiOutput("picker"),
      actionButton("view", "View Selection")
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      h2('Mydata'),
      DT::dataTableOutput("table"),
    )
  )
)

library(shiny)
library(DT)

server <- function(session, input, output) {
  
  data <- reactive({
    iris
  })
  
  output$picker <- renderUI({
    pickerInput(inputId = 'pick', 
                label = 'Choose', 
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),multiple = T)
  })
  
  datasetInput <- eventReactive(input$view,{
    
    datasetInput <- data() %>% 
      select(input$pick)
    
    return(datasetInput)
    
  })
  
  output$table <- renderDT({
    if(input$type_select == "Rows"){
        datatable(
          data(),
          selection = "none",
          filter="top", 
          rownames = FALSE,
          extensions = c("Buttons", "Select"),
          
          options = list(
            select = TRUE,
            dom = 'Blfrtip',
            buttons =
              list('copy', 'print', list(
                extend = 'collection',
                buttons = list(
                  list(extend = 'csv', filename = "File", title = NULL,
                       exportOptions = list(modifier = list(selected = TRUE))),
                  list(extend = 'excel', filename = "File", title = NULL,
                       exportOptions = list(modifier = list(selected = TRUE)))),
                text = 'Download'
              ))
          ),
          class = "display"
        )
    }else{
        datatable(
          datasetInput(),
          filter="top", 
          rownames = FALSE,
          extensions = 'Buttons',
          
          options = list(
            dom = 'Blfrtip',
            buttons =
              list('copy', 'print', list(
                extend = 'collection',
                buttons = list(
                  list(extend = 'csv', filename = "File", title = NULL),
                  list(extend = 'excel', filename = "File", title = NULL)),
                text = 'Download'
              ))
          ),
          class = "display"
        )
      }
  })
}


# Run the application 
shinyApp(ui = ui, server = server)

接下来,我将向您展示该应用程序的一些屏幕截图,以解释其工作原理。

如果你select“行”:

如果您select“列”:

事实上,如果您想检查 selection 是否正常,您可以下载一个文件(例如 excel),您会看到您的 select离子没问题。

但是,我不知道如何解决我上面解释的问题。

请问有人能帮帮我吗?我很绝望。

非常感谢

要消除警告,请将 renderDT 更改为 DT::renderDataTable 并在其中设置 server = FALSE

这里是完整的代码-

ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      radioButtons("type_select", "What do you want to select?",
                   c("Rows" = "Rows",
                     "Columns" = "Columns")),
      uiOutput("picker"),
      actionButton("view", "View Selection")
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      h2('Mydata'),
      DT::dataTableOutput("table"),
    )
  )
)

library(shiny)
library(DT)

server <- function(session, input, output) {
  
  data <- reactive({
    iris
  })
  
  output$picker <- renderUI({
    pickerInput(inputId = 'pick', 
                label = 'Choose', 
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),multiple = T)
  })
  
  datasetInput <- eventReactive(input$view,{
    
    datasetInput <- data() %>% 
      select(input$pick)
    
    return(datasetInput)
    
  })
  
  output$table <- DT::renderDataTable(server = FALSE,{
    if(input$type_select == "Rows"){
      datatable(
        data(),
        selection = "none",
        filter="top", 
        rownames = FALSE,
        extensions = c("Buttons", "Select"),
        
        options = list(
          select = TRUE,
          dom = 'Blfrtip',
          buttons =
            list('copy', 'print', list(
              extend = 'collection',
              buttons = list(
                list(extend = 'csv', filename = "File", title = NULL,
                     exportOptions = list(modifier = list(selected = TRUE))),
                list(extend = 'excel', filename = "File", title = NULL,
                     exportOptions = list(modifier = list(selected = TRUE)))),
              text = 'Download'
            ))
        ),
        class = "display"
      )
    }else{
      datatable(
        datasetInput(),
        filter="top", 
        rownames = FALSE,
        extensions = 'Buttons',
        
        options = list(
          dom = 'Blfrtip',
          buttons =
            list('copy', 'print', list(
              extend = 'collection',
              buttons = list(
                list(extend = 'csv', filename = "File", title = NULL),
                list(extend = 'excel', filename = "File", title = NULL)),
              text = 'Download'
            ))
        ),
        class = "display"
      )
    }
  })
}


# Run the application 
shinyApp(ui = ui, server = server)

我找到了所有问题的完整解决方案:

  • 警告(感谢 Ronak Shah 的回答)

  • 当我select“行”时,如何从“pickerInput”和操作按钮中隐藏“选择输入”。感谢这个

    library(shiny)
    library(shinyWidgets)
    library(dplyr)
    
    ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
      sidebarPanel(
        radioButtons("type_select", "What do you want to select?",
                     c("Rows" = "Rows",
                       "Columns" = "Columns")),
    
        conditionalPanel(
          condition = "input.type_select == 'Columns'",
          uiOutput("picker"),
          actionButton("view", "View Selection")        
        )
    
      ),
    
      # Show a plot of the generated distribution
      mainPanel(
        h2('Mydata'),
        DT::dataTableOutput("table"),
      )
    )
    )
    
    
    library(shiny)
    library(DT)
    
    server <- function(session, input, output) {
    
    data <- reactive({
      iris
    })
    
    output$picker <- renderUI({
      pickerInput(inputId = 'pick', 
                  label = 'Choose', 
                  choices = colnames(data()),
                  options = list(`actions-box` = TRUE),multiple = T)
    })
    
    datasetInput <- eventReactive(input$view,{
    
      datasetInput <- data() %>% 
        select(input$pick)
    
      return(datasetInput)
    
    })
    
    output$table <- DT::renderDataTable(server = FALSE,{
      if(input$type_select == "Rows"){
        datatable(
          data(),
          selection = "none",
          filter="top", 
          rownames = FALSE,
          extensions = c("Buttons", "Select"),
    
          options = list(
            select = TRUE,
            dom = 'Blfrtip',
            buttons =
              list('copy', 'print', list(
                extend = 'collection',
                buttons = list(
                  list(extend = 'csv', filename = "File", title = NULL,
                       exportOptions = list(modifier = list(selected = TRUE))),
                  list(extend = 'excel', filename = "File", title = NULL,
                       exportOptions = list(modifier = list(selected = TRUE)))),
                text = 'Download'
              ))
          ),
          class = "display"
        )
      }else{
        datatable(
          datasetInput(),
          filter="top", 
          rownames = FALSE,
          extensions = 'Buttons',
    
          options = list(
            dom = 'Blfrtip',
            buttons =
              list('copy', 'print', list(
                extend = 'collection',
                buttons = list(
                  list(extend = 'csv', filename = "File", title = NULL),
                  list(extend = 'excel', filename = "File", title = NULL)),
                text = 'Download'
              ))
          ),
          class = "display"
        )
      }
    })
    }
    
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

添加条件面板后的一些截图:

如果你 select 行...

如果您 select 列...

警告。如果您只想 select X 列来下载它们,则此解决方案不起作用(例如,您只想下载 25 个条目,但如果您下载文件,则不会有 25 个条目,你将拥有一切。