我在使用R包openxlsx中的insertImage函数时遇到了问题。每次我插入新图像时,文档中的所有其他图像都会折叠并且不显示。有什么想法吗?
发布于 2019-12-03 00:49:23
如果没有看到更多的代码,我不确定我能帮上多少忙。我能够使用您的代码片段插入两张图像(一张在另一张下面)。以下是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)发布于 2020-11-23 17:42:11
每当我尝试在同一张工作表中插入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)https://stackoverflow.com/questions/59140987
复制相似问题