我正在R笔记本中进行分析,我希望除以.nb.html笔记本输出显示之外,所有以R块创建的图都另存为单独的PDF文件。
我遇到的问题是,在运行笔记本时,fig.path = "figures/"
无论是在各个块头中指定,都不会将绘图保存到块选项中指定的目录中:
#```{r fig.path = "figures/"}
plot(x, y)
#```
或在使用全局块选项指定时:
#```{r setup}
library(knitr)
opts_chunk$set(fig.path = "figures/")
#```
#```{r}
plot(x, y)
#```
实际上*figures/
,任何地方都没有命名目录。不在项目根目录中,也不在.Rmd脚本所在的目录中。即使当我手动创建文件夹时proj_root/figures/
,图也不会输出到此处。
fig.path = "figures/"
,但不会产生任何结果# load knitr package
library(knitr)
# set all subsequent chunks' working dir as the project root dir
opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
# set this first setup chunk working dir to project root dir (since the previous line does not affect the chunk it's run in)
setwd(rprojroot::find_rstudio_root_file())
# Check current working dir
getwd() # CORRECTLY OUTPUTS R.PROJECT ROOT DIR
# Set all chunks to output plots to the dir "figures/"
opts_chunk$set(fig.path = "figures/") # This should output all chunk plots to "project-root/figures/"
,project-root/figures/
除了将它们保留在.nb.html报告中之外,我认为还应该制作该文件夹并在其中输出所有块图。
我也尝试过dev = "pdf"
在中的global chunks选项中指定opts_chunk$set(dev = "pdf", fig.path = "figures/")
,但这不会改变任何内容。
我不知道自己在做什么错,在搜索过的所有地方,所有选项都不起作用,包括:
该knitr文档是无益的,因为它只是描述了fig.path =
块选项,但没有排除故障,如果它不能正常工作。我是否需要添加其他fig.*** =
块选项才能使其正常工作?仅设置fig.path =
选项不可以吗?
它与我的YAML输出有关,html_notebook
而不是输出到markdown或html_document
吗?
与我遇到的问题最接近的帖子是:R Notebook:在报表中包含数字并保存图表,但自2018年以来一直未解决,我不确定如何“推广”该帖子,除了制作一个新的并引用它。
我测试了,fig.path选项按预期工作。请测试以下rmd:
编辑:确保output: html_document
在Rmd YAML标头中将输出类型设置为,然后单击Knit to HTML
以激活图形的保存。这与交互式运行Rmd文档或使用单独运行,可以在之后运行Run All
。见下面的评论
---
title: "Untitled"
author: "TC"
date: "10/30/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.path="testit/")
```
## R Markdown
https://stackoverflow.com/questions/58600399/how-do-you-run-an-r-file-on-ubuntu?noredirect=1#comment103544013_58600399
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r pressure2, echo=FALSE}
plot(pressure)
```
```{r echo=FALSE}
plot(pressure)
```
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句