我的软件包中的功能可以创建R Markdown文档在某些工作目录中失败 - 我可以解决这个问题吗?
我已经在包装中写了一个函数,该功能将获取用户提供的一些数据,并用这些数据图制作一个很小的Word文档。但是,虽然当工作目录是本地驱动器时,该功能效果很好,但是当驱动器是其他任何东西时,它会失败。错误是:
从第6-9行(skeleton.rmd)中退出png中的错误(...,res = dpi, 单位=“ in”):另外无法启动png()设备:警告 消息:1:在dir.create(dirname(name),递归= true)中:不能 创建dir'\ myemployer.com \ data \ stites',原因“拒绝权限” 2:在png中(...,res = dpi,单位=“ in”):无法打开文件 '\ myEmployer.com \ data \ stites \ myworkingDirectory/figue-docx/myplot-1.png' 对于写作3:在png中(...,res = dpi,单位=“ in”):打开设备 失败
我确实有权写信给我当前的工作目录。实际上,当我在相同的包装中呼叫类似的函数,该功能可以制作表,而不是图表时,它可以正常工作。
我 think 发生了什么是rmarkDown或knitr需要临时保存图表的png文件,以便将其放置在Word文件中,并且它将其保存在没有许可的某个地方所以。 我可以通过用rmarkdown :: render指定某些内容来解决此行为?
我不知道当我引用多个文件和包装的文件结构时,如何确切地制作一个可重复的示例,但这是我最佳的尝试,需要哪些信息。这是函数的一个示例:
myFun <- function(mydata, filename){
G <- ggplot2::ggplot(mydata, ggplot2::aes(x = A, y = B)) +
ggplot2::geom_point()
OutPath <- dirname(filename)
FileName <- basename(filename)
rmarkdown::render(system.file("rmarkdown/templates/myplot/skeleton/skeleton.Rmd",
package="mypackage"), output_dir = OutPath,
output_file = FileName, quiet = TRUE)
}
从我的软件包的顶级到达skeleton.rmd的文件结构: mypackage/inst/rmarkDown/spemplates/myplot/skeleton/skeleton.rmd
实际r标记文档是这样的:
---
title: "My Plot"
output: word_document
---
```{r}
G
```
错误消息“从第6-9行退出”是指输出g的块。
要调用此功能,您将使用:
myFun(mydata = data.frame(A = 1:10, B = 1:10),
filename = "my file.docx")
如果它正在工作,则将在当前工作目录中获取一个称为“我的file.docx”的Word文件,其中包含图形。
是否有一些方法可以解决RmarkDown临时保存此PNG文件的需求?我可以将此临时文件的目录设置为 的其他内容吗?
I've written a function inside a package that will take some data the user provides and make a pretty little Word document with a graph of those data. However, while the function works great when the working directory is a local drive, it fails when the drive is anything else. The errors are:
Quitting from lines 6-9 (skeleton.Rmd) Error in png(..., res = dpi,
units = "in") : unable to start png() device In addition: Warning
messages: 1: In dir.create(dirname(name), recursive = TRUE) : cannot
create dir '\myemployer.com\data\sites', reason 'Permission denied'
2: In png(..., res = dpi, units = "in") : unable to open file
'\myemployer.com\data\sites\myworkingdirectory/figure-docx/myplot-1.png'
for writing 3: In png(..., res = dpi, units = "in") : opening device
failed
I do have permission to write to my current working directory in general. In fact, when I call on a similar function in the same package that makes a table instead of a graph, it works fine.
I think what's going on is that Rmarkdown or knitr needs to temporarily save a png file of the graph in order to place it in the Word file, and it's saving it somewhere it doesn't have permission to do so. Can I get around this behavior by specifying something with rmarkdown::render?
I don't know how exactly to make a reproducible example when I'm referring to multiple files and the file structure of my package, but here's my best attempt at what info is needed. Here's the an example of the function:
myFun <- function(mydata, filename){
G <- ggplot2::ggplot(mydata, ggplot2::aes(x = A, y = B)) +
ggplot2::geom_point()
OutPath <- dirname(filename)
FileName <- basename(filename)
rmarkdown::render(system.file("rmarkdown/templates/myplot/skeleton/skeleton.Rmd",
package="mypackage"), output_dir = OutPath,
output_file = FileName, quiet = TRUE)
}
The file structure to get to skeleton.Rmd from the top level of my package:
mypackage/inst/rmarkdown/templates/myplot/skeleton/skeleton.Rmd
The actual R markdown document is something like this:
---
title: "My Plot"
output: word_document
---
```{r}
G
```
and the error message "Quitting from lines 6-9" refers to the chunk where G is outputted.
To call on this, you would use:
myFun(mydata = data.frame(A = 1:10, B = 1:10),
filename = "my file.docx")
and, if it's working, you'd get a Word file in the current working directory called "my file.docx" that contains a graph.
Is there some way to work around rmarkdown's need to temporarily save this png file? Can I set the directory for this temporary file to something else where it would have write permissions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能的解决方法是将临时目录中的降价渲染,并将生成的文件复制到其目标,请参阅:::
A possible workaround would be to render the Markdown in a temporary directory, and to copy the generated file to its destination, see:
从评论开始,这可能会有所帮助。您可以在
rmd
文件中制作图:并将您的功能更改为以下内容:
调用函数:
它在当前形式中非常有限,因为您需要
df1
和<代码> DF2 具有相同的变量名称等,但可能会帮助您。Following on from the comments, this may help. You could make the plot within the
rmd
file:And alter you function to something like:
Calling the function:
Its pretty limited in its current form as you would need
df1
anddf2
to have the same variable names etc. but may help you along.