我在使用软件包openxlsx
中的insertImage
函数时遇到问题R
。每次插入新图像时,文档中的所有其他图像都会折叠并且不显示。有什么想法吗?
如果没有看到更多的代码,我不确定我能有多大帮助。我能够使用您的代码片段毫无问题地插入两张图像(一张在另一张下方(。这是指向insertImage()
文档的链接。
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)
每当我尝试在同一张纸中插入 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)