简体   繁体   English

我在使用包 openxlsx 从 R 将图像粘贴到 Excel 中时遇到错误

[英]I am facing error in pasting an image in Excel from R using package openxlsx

I am trying to paste an image saved in my folder to Excel using R package openxlsx.我正在尝试使用 R 包 openxlsx 将保存在我的文件夹中的图像粘贴到 Excel。 I have completely studied the package documentation and followed the steps given in the documentation.我已经完全研究了包文档并按照文档中给出的步骤进行操作。 But even the example given in package is not working for me但即使是包中给出的例子也不适合我

## Create a new workbook
wb <- createWorkbook("Ayanami")
## Add some worksheets
addWorksheet(wb, "Sheet 1")
addWorksheet(wb, "Sheet 2")
addWorksheet(wb, "Sheet 3")
## Insert images
img <- system.file("einstein.jpg", package = "openxlsx")
insertImage(wb, "Sheet 1", img, startRow = 5, startCol = 3, width = 6, height = 5)
insertImage(wb, 2, img, startRow = 2, startCol = 2)
insertImage(wb, 3 , img, width = 15, height = 12, startRow = 3, startCol = "G", units = "cm")
## Save workbook
saveWorkbook(wb, "insertImageExample.xlsx", overwrite = TRUE)

This is the example given in package documentation.这是包文档中给出的示例。 Instead of "einstein.jpg", I am using my ".jpg" file.我使用的是我的“.jpg”文件,而不是“einstein.jpg”。 I am trying to paste that image in my workbook 'wb'.我正在尝试将该图像粘贴到我的工作簿“wb”中。 The function "system.file" does not fetch the image I am passing.函数“system.file”不获取我传递的图像。 I have made sure that there is no issues related to path whether image has been stored.我已经确定图像是否已存储,没有与路径相关的问题。

Could anyone of help me with this function or has any verified alternative?任何人都可以帮助我使用此功能或有任何经过验证的替代方案吗?

You shouldn't use system.file function as the image that you are trying to paste in the workbook isn't a system file.您不应使用system.file函数,因为您尝试粘贴到工作簿中的图像不是系统文件。

instead you will need something like:相反,您将需要类似的东西:

img <- "C:/your_dir/your_filename.jpg"

insertImage(wb, "Sheet 1", img, startRow = 5, startCol = 3, width = 6, height = 5)
saveWorkbook(wb, "insertImageExample2.xlsx", overwrite = TRUE)

For context, you are likely stepping through the documentation for openxlsx here: ycphs.github.io/openxlsx/articles/Introduction.html对于上下文,您可能会在此处逐步浏览 openxlsx 的文档:ycphs.github.io/openxlsx/articles/Introduction.html

The link to the image is incorrect on the link.链接上的图像链接不正确。 You should use:你应该使用:

img <- readJPEG(file.path(path.package("openxlsx"), "extdata/einstein.jpg"))

Try something like this:尝试这样的事情:

    library(jpeg)
    img<-readJPEG("einstein.jpg")
    plotFn(img)
    insertPlot(wb, 1)
    saveWorkbook(wb, "insertImageExample.xlsx", overwrite = TRUE)
    file.show("insertImageExample.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM