【问题标题】:export a huxtable data and a ggplot into one excel file将一个 huxtable 数据和一个 ggplot 导出到一个 excel 文件中
【发布时间】:2021-12-02 20:17:29
【问题描述】:

我在问我是否可以从 R 中导出一个 huxtable 数据集在一张表中,从 ggplot2 中导出另一张表中的图,并在同一个 excel 文件中?

wb <- createWorkbook()
addWorksheet(wb, sheetName = "Frequencies")
addWorksheet(wb, sheetName = "Plot")

writeDataTable(wb, sheet = "Frequencies", x = huxtable, row.names=F)
plot(p)
insertPlot(wb,"Plot")

saveWorkbook(wb=wb, file="path_file/name_file.xlsx", overwrite=TRUE) 

我尝试使用上面的代码,huxtable 是格式化的数据集(数据集的行是彩色的),p 是我使用函数ggplot() 生成的图,但我没有得到所需的输出,因为我丢失了 huxtable 的格式。

我尝试使用此代码,但它只导出带有格式的 huxtable 而不是绘图:

file<- as_Workbook(huxtable,sheet="Frequencies")

showGridLines(file, sheet="Frequencies", showGridLines = FALSE)

openxlsx::saveWorkbook(file,"file_path/file_name.xlsx", overwrite = TRUE)

这里是情节和 huxtable 的一个例子:


p <- 
  ggplot(mtcars)+
  geom_histogram(aes(x = mpg))

p


huxtable<-as_hxtable(mtcars[1:10,])
for (i in 1:length(huxtable) ) {
  
  if  (i  == 1){
    huxtable<-set_background_color(huxtable,row=i  , everywhere, "yellow")  
  }
  
  
  else{
    huxtable<-set_background_color(huxtable,row=i  , everywhere, "red")
  }
  
}

huxtable

我想在不丢失数据集格式的情况下将彩色数据集 + 绘图导出到同一个 excel 文件中

【问题讨论】:

    标签: r ggplot2 huxtable


    【解决方案1】:

    这是一个可以调整的潜在工作流程。查看包文档中的选项,因为下面的答案只使用最少的参数,并且所有使用的包都提供了很多选项。

    在 OP 包含格式化的 huxtable 后更新。

    library(openxlsx)
    library(huxtable)
    library(ggplot2)
    
    # create workbook
    wb <- createWorkbook()
    
    #create sheet for plot
    addWorksheet(wb, sheetName = "plot")
    
    
    # create plot
    p <- 
      ggplot(mtcars)+
      geom_histogram(aes(x = mpg))
    
    p  
    
    

    # insert plot inserts the current plot into the worksheet
    insertPlot(wb, sheet = "plot")
    
    # create huxtable with formatting
    hx <- as_huxtable(mtcars[1:10,])
    
    for (i in 1:length(hx) ) {
      
      if  (i  == 1){
        hx<-set_background_color(hx, row = i, everywhere, "yellow")  
      }
      
      
      else{
        hx<-set_background_color(hx, row = i, everywhere, "red")
      }
      
    }
    
    hx
    
    

    # use huxtable::as_Workbook function to convert table for export into excel workbook
    as_Workbook(hx, Workbook = wb, sheet = "table")
    
    
    ## Save workbook
    saveWorkbook(wb, "eg_table_plot.xlsx", overwrite = TRUE)
    

    reprex package (v2.0.1) 于 2021-12-02 创建

    【讨论】:

    • 谢谢,但是当您在以下数据集上应用代码时,数据集 hx 将不会以颜色导出:hx&lt;-as_hxtable(mtcars[1:10,]) for (i in 1:length(hx) ) { if (i == 1){ hx&lt;-set_background_color(hx,row=i , everywhere, "yellow") } else{ hx&lt;-set_background_color(hx,row=i , everywhere, "red") } }
    • 能否请您将 huxtable 的代码包含在问题中,这将有助于使问题可重现;我必须检查是否使用openxlsx 将huxtable 格式转换为excel; openxlsx 有自己的函数来格式化工作表中的数据。
    • 好的,我将代码包含在问题中
    • 查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2020-11-23
    • 1970-01-01
    • 2018-01-26
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多