首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >insertImage函数失败(R包"openxlsx")

insertImage函数失败(R包"openxlsx")
EN

Stack Overflow用户
提问于 2019-12-02 22:28:47
回答 2查看 332关注 0票数 0

我在使用Ropenxlsx中的insertImage函数时遇到了问题。每次我插入新图像时,文档中的所有其他图像都会折叠并且不显示。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2019-12-03 00:49:23

如果没有看到更多的代码,我不确定我能帮上多少忙。我能够使用您的代码片段插入两张图像(一张在另一张下面)。以下是insertImage()文档的link

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2020-11-23 17:42:11

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

这不是一个很好的解决方案,但您唯一能做的就是将图像插入到单独的工作表中,然后根据需要手动合并工作表。

如果你定义了一个数字,你可以这样做:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59140987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档