如何使用来自DT的Colvis在Shiny中仅下载数据框中的选定列?

发布于 2025-01-30 18:39:38 字数 1818 浏览 2 评论 0原文

我正在使用dt软件包的按钮colvis来选择我要在表中显示的列。在这里,您有更多 info 关于按钮colvis。 它可以很好地工作,它隐藏了我不想选择的列,结果将显示给用户。

但是,下载文件时似乎没有更新此信息。

如果我只选择“ petal.width”和“物种”:

​我仍然有所有的列,而不是选定的列。

我一直在试图找到解决方案,但是我什么都没找到。

有人知道如何解决吗?

提前致谢。

这是我的代码:

library(shiny)
library(DT)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderDataTable({
    datatable(
      iris,
      filter = list(position = 'top', clear = FALSE),
      selection = "none", #this is to avoid select rows if you click on the rows
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        scrollX = TRUE,
        dom = 'Blrtip',
        buttons =
          list(I('colvis'),'copy', 'print', list(
            extend = 'collection',
            buttons = list(
              list(extend = 'csv', filename = paste0("iris"), title = NULL),
              list(extend = 'excel', filename = paste0("iris"), title = NULL)),
            text = 'Download'
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )
  })
}

shinyApp(ui, server)

I am using the button colvis from the DT package to select which columns I would like to show in the table. Here you have more info about the button colvis.
It works perfectly fine, it hides the columns that I don't want to select and the result is shown to the user.

image1

However, it seems that this info is not updated when I download the file.

If I only select "Petal.Width" and "Species":

image 2

Then, I download the file... and I open it. I still have all the columns and not the selected ones.

image 3

I have been trying to find a solution, but I haven't found anything.

Does anyone know how to fix it?

Thanks in advance.

Here is my code:

library(shiny)
library(DT)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderDataTable({
    datatable(
      iris,
      filter = list(position = 'top', clear = FALSE),
      selection = "none", #this is to avoid select rows if you click on the rows
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        scrollX = TRUE,
        dom = 'Blrtip',
        buttons =
          list(I('colvis'),'copy', 'print', list(
            extend = 'collection',
            buttons = list(
              list(extend = 'csv', filename = paste0("iris"), title = NULL),
              list(extend = 'excel', filename = paste0("iris"), title = NULL)),
            text = 'Download'
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )
  })
}

shinyApp(ui, server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2025-02-06 18:39:38
library(DT)

datatable(
  iris,
  extensions = "Buttons",
  options = list(
    dom = "Bfrtip",
    buttons = list(
      I("colvis"),
      list(
        extend = "collection",
        text = "Download",
        buttons = list(
          list(
            extend = "csv",
            exportOptions = list(
              columns = ":visible"
            )
          )
        )
      )
    )
  )
)
library(DT)

datatable(
  iris,
  extensions = "Buttons",
  options = list(
    dom = "Bfrtip",
    buttons = list(
      I("colvis"),
      list(
        extend = "collection",
        text = "Download",
        buttons = list(
          list(
            extend = "csv",
            exportOptions = list(
              columns = ":visible"
            )
          )
        )
      )
    )
  )
)
三五鸿雁 2025-02-06 18:39:38

感谢StéphaneLaurent的答案,我设法找到了答案。

我遇到了一些问题,可以使用两个按钮(CSV和Excel),以及如何使用建议的解决方案来组织列表,但是我找到了做到这一点的方法。

我将使用原始代码添加答案,以防有人像我这样的问题。

library(shiny)
library(DT)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderDataTable({
    datatable(
      iris,
      filter = list(position = 'top', clear = FALSE),
      selection = "none", #this is to avoid select rows if you click on the rows
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        scrollX = TRUE,
        dom = 'Blrtip',
        buttons =
          list(I('colvis'),'copy', 'print', list(
            extend = 'collection',
            text = 'Download',
            buttons = list(
              list(
                extend = "csv", filename = paste0("iris"), title=NULL,
                exportOptions = list(
                  columns = ":visible")
              ),
              
              list(
                extend = "excel", filename = paste0("iris"), title=NULL,
                exportOptions = list(
                  columns = ":visible")
              )
              
            )
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )
  })
}

shinyApp(ui, server)

Thanks to Stéphane Laurent's answer, I managed to find an answer.

I had some problems to have both buttons (csv and excel) and how to organise the lists with the proposed solution, but I found the way to do it.

I will add the answer with the original code just in case someone has problems like me.

library(shiny)
library(DT)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderDataTable({
    datatable(
      iris,
      filter = list(position = 'top', clear = FALSE),
      selection = "none", #this is to avoid select rows if you click on the rows
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        scrollX = TRUE,
        dom = 'Blrtip',
        buttons =
          list(I('colvis'),'copy', 'print', list(
            extend = 'collection',
            text = 'Download',
            buttons = list(
              list(
                extend = "csv", filename = paste0("iris"), title=NULL,
                exportOptions = list(
                  columns = ":visible")
              ),
              
              list(
                extend = "excel", filename = paste0("iris"), title=NULL,
                exportOptions = list(
                  columns = ":visible")
              )
              
            )
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )
  })
}

shinyApp(ui, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文