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

时间:2018-04-25 14:47:52

标签: r shiny xlsx

我正在尝试使用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个列的跨度?

1 个答案:

答案 0 :(得分:2)

由于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)
})

如果你想要包含&#34;标题&#34;的细胞,生活会稍微复杂一些。而不是相等&#34;标题&#34;:

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

......并像以前一样继续。