insertImage函数失败(R包“ openxlsx”)

时间:2019-12-02 14:28:46

标签: r openxlsx

我在insertImage软件包R中使用了openxlsx函数。每次插入新图像时,文档中的所有其他图像都会折叠而不会显示。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

没有看到更多代码,我不确定我能帮上什么忙。我能够使用您的代码段插入两个图像而没有问题(一个在另一个下方)。这是insertImage()文档的link

library(openxlsx)

wb <- openxlsx::loadWorkbook("M:\\imageTest.xlsx")
wb %>% 
  insertImage("01", "C:\\Users\\jcarty\\Desktop\\imageTest.jpg", width = 13, height = 8.5
              , startRow = 11, startCol = 2, units = "cm", dpi = 96) 
wb %>% 
  saveWorkbook("M:\\imageTest.xlsx", overwrite = TRUE)


wb <- openxlsx::loadWorkbook("M:\\imageTest.xlsx")
wb %>% 
  insertImage("01", "C:\\Users\\jcarty\\Desktop\\imageTest2.jpg", width = 13, height = 8.5
              , startRow = 27, startCol = 2, units = "cm", dpi = 96) 
wb %>% 
  saveWorkbook("M:\\imageTest.xlsx", overwrite = TRUE)

答案 1 :(得分:0)

每当我尝试在同一张纸上插入2张以上图像时,我都会遇到相同的问题。显然,这是软件包https://github.com/awalker89/openxlsx/issues/373中的一个已知问题。

解决方案不多,但是唯一可以做的就是将图像插入单独的图纸中,然后根据需要手动组合图纸。

如果定义数字,则可以执行以下操作:

fignum <- 1

# Set current sheet name
# For uneven figure numbers
if ((fignum %% 2) != 0) {
  
  # Set plot name to current number
  current_sheetname <- paste0("plot_", fignum)
  
# For even figure numbers
} else {
  
  # Set plot name to previous number
  current_sheetname <- paste0("plot_", fignum - 1)
}

# Check if the current worksheet name doesn't exist yet
if (!(current_sheetname %in% names(wb))) {
  # Create new worksheet
  openxlsx::addWorksheet(wb = wb,
                         sheetName = current_sheetname)
}

# Write figure to new worksheet
openxlsx::insertImage(wb = wb,
                      sheet = current_sheetname,
                      file = img1)