如何使用 xlsx 包动态合并来自 R Shiny 应用程序的 XLSX 下载列?

How to dynamically merge columns of XLSX downloads from an R Shiny app using the xlsx package?

我正在尝试使用 xlsx 包动态合并来自 R Shiny 应用程序的 XLSX 下载列。我目前可以合并从静态数据框创建的 XLSX 文档的列,如以下最小示例所示:

library(xlsx)

# Creating dataframe.
df <- data.frame(c("Title", 1, "one"),
                 c("", 2, "two"),
                 c("", 3, "three"))

# Creating a workbook using the XLSX package.
wb <- xlsx::createWorkbook(type = "xlsx")

# Creating a sheet inside the workbook.
sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

# Adding the full dataset into the sheet.
xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

# Merging first three columns.
addMergedRegion(sheet, 1, 1, 1, 3)

# Saving the workbook.
xlsx::saveWorkbook(wb, "df.xlsx")

我已经在以下 R Shiny 应用程序中实现了这个示例的逻辑:

# ui.R

fluidPage(
  h1("Download XLSX"),
  downloadButton("download.button", label = "Download")
)

# server.R

library(xlsx)

function(input, output) {

  # Creating dataframe.
  df <- data.frame(c("Title", 1, "one"),
                   c("", 2, "two"),
                   c("", 3, "three"))

  # Handling download.
  # Creating XLSX file for download.
  output$download.button <- downloadHandler(
    filename <- paste0("df.xlsx"),
    content <- function(file) {
      # Creating a workbook using the XLSX package.
      wb <- xlsx::createWorkbook(type = "xlsx")

      # Creating a sheet inside the workbook.
      sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

      # Adding the full dataset into the sheet.
      xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

      # Merging first three columns.
      addMergedRegion(sheet, 1, 1, 1, 3)

      # Saving the workbook.
      xlsx::saveWorkbook(wb, file)
    }
  )

}

在上面的示例中,addMergedRegion(sheet, 1, 1, 1, 3) 将第一行的前三列合并为一个。我希望能够动态地执行此操作而不是通过硬编码,因为我将使用各种大小的数据帧。作为输入,我想使用行索引、列索引和列跨度的列表。这些将是我需要填充 addMergedRegion() 函数的所有值,但是,我不确定如何动态生成合并列所需的函数。作为这一挑战的一个例子,我制作了第二个应用程序:

# ui.R

fluidPage(
  h1("Download XLSX"),
  numericInput("numeric.input", "Select Number of Tables:", 1, min = 1),
  DT::dataTableOutput("table"),
  downloadButton("download.button", label = "Download")
)

# server.R

library(DT)
library(xlsx)

function(input, output) {

  # Creating dataframe.
  df <- data.frame(c("Title", 1, "one"),
                   c("", 2, "two"),
                   c("", 3, "three"))

  # Creating reactive dataframe.
  values <- reactiveValues(
    df = df
  )

  # Expanding dataframe based on user input.
  observeEvent(input$numeric.input, {
    values$df <- df[rep(seq_len(nrow(df)), input$numeric.input), ]
  })

  # Rendering data table of dataframe.
  output$table <- DT::renderDataTable({
    DT::datatable(values$df,
                  selection = "none",
                  rownames = FALSE,
                  options = list(dom = "t",
                                 ordering = FALSE,
                                 pageLength = nrow(values$df)))
  })

  # Handling download.
  # Creating XLSX file for download.
  output$download.button <- downloadHandler(
    filename <- paste0("df.xlsx"),
    content <- function(file) {
      # Creating a workbook using the XLSX package.
      wb <- xlsx::createWorkbook(type = "xlsx")

      # Creating a sheet inside the workbook.
      sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

      # Adding the full dataset into the sheet.
      xlsx::addDataFrame(values$df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

      # Saving the workbook.
      xlsx::saveWorkbook(wb, file)
    }
  )

}

此应用根据用户输入扩展数据框行。在这样的动态情况下,如何为包含字符串 "Title" 的每个单元格提供 3 的列跨度?

由于 java 问题,我无法使用 xlsx,但我认为您只需要:

indices <- which(df=="Title", arr.ind=TRUE)

这将为您提供一个 n x 2 的行和列索引矩阵,然后您可以在 addMergedRegion:

中使用它
apply(indices, 1, function (x) {
  xlsx::addMergedRegion(sheet, x[1], x[1], x[2], x[2] + 2)
})

如果你想要 包含 "Title" 的细胞而不是 等于 [=27 的细胞,生活会稍微复杂一些=]:

x <- matrix(NA, nrow(df), ncol(df))
x[] <- grepl("Title", unlist(df))
indices <- which(x, arr.ind = TRUE)

...和以前一样进行。