R Notebook/Markdown不会使用"fig.path ="保存块图.块选项 [英] R Notebook/Markdown does not save chunk plots using "fig.path = " chunk option

查看:299
本文介绍了R Notebook/Markdown不会使用"fig.path ="保存块图.块选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R笔记本中运行分析,我希望将R块中创建的所有图另存为单独的PDF文件, .nb.html笔记本输出.

I'm running an analysis in an R Notebook and I would like all plots created in R chunks to be saved as individual PDF files in addition to appearing in the .nb.html notebook output.

我遇到的问题是,在运行笔记本时,无论在单独的块头中指定,都不会将图保存到块选项fig.path = "figures/"中指定的目录中:

The problem I'm having is that, when the notebook is run, it does not save plots to the dir specified in the chunk option fig.path = "figures/" either when specified in the individual chunk header:

#```{r fig.path = "figures/"}
plot(x, y)
#```

,或者在使用全局块选项指定时:

or when specified with the global chunk options:

#```{r setup}
library(knitr)
opts_chunk$set(fig.path = "figures/")
#```

#```{r}
plot(x, y)
#```

实际上,任何地方都没有名为*figures/ 的目录. .不在项目根目录中,也不在.Rmd脚本所在的目录中.即使当我手动创建文件夹proj_root/figures/时,图也不会输出到此处.

In fact, there is no dir named *figures/ at all, anywhere. not in the project root directory nor in the dir where the .Rmd script is located. Even when I manually create the folder proj_root/figures/, plots are not output to here.

  • 我尝试为单个块设置块选项,该块将图输出到fig.path = "figures/",但不会产生任何结果
  • 在设置块中,我运行了以下几行:
  • I tried setting the chunk options for a single chunk that outputs a plot to fig.path = "figures/", which does not produce anything
  • In a setup chunk, I've run these lines:
# 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/"

,我假设除了将它们保存在.nb.html报告中之外,还应该制作project-root/figures/文件夹并在其中输出所有块图.

, which I assume should make the project-root/figures/ folder and output all chunk plots there, in addition to keeping them in the .nb.html report.

我也尝试过在opts_chunk$set(dev = "pdf", fig.path = "figures/")的global chunks选项中也指定dev = "pdf",但这并没有改变.

I've also tried also specifying dev = "pdf" in the global chunks option in opts_chunk$set(dev = "pdf", fig.path = "figures/"), but this doesn't change anything.

我不知道自己在做什么错,在我搜索过的所有地方,所有选项都不起作用,包括:

I have no idea what I'm doing wrong, and everywhere I've searched, none of the options work, including:

  • Knitr with R Markdown
  • knitr Documentation: Options: Plots
  • knitr: include figures in report and output figures to separate files
  • change where rmarkdown saves images generated by r code

knitr文档无济于事,因为它仅描述了fig.path =块选项,但如果无法解决,则无需进行故障排除.我是否需要添加其他fig.*** =块选项以使其起作用?仅设置fig.path =选项不可以吗?

The knitr documentation is unhelpful as it only describes the fig.path = chunk option, but no troubleshooting if it doesn't work. Do I need to add other fig.*** = chunk options in order to make it work? Shouldn't it work with only setting the fig.path = option?

与我的YAML输出为html_notebook而不是输出为markdown或html_document有什么关系吗?

Does it have anything to do with my YAML output being html_notebook instead of outputting to markdown or html_document?

与我遇到的问题最接近的帖子是:

The closest post to my problem I have come across is this one: R Notebook: Include figures in report and save plots, but it has not been resolved since 2018 and I'm not sure how to 'promote' that post other than making a new one and referencing it.

推荐答案

我进行了测试,并且fig.path选项按预期工作.请测试以下rmd:

I tested and the fig.path option works as expected. please test the following rmd:

编辑:确保在Rmd YAML标头中将输出类型设置为output: html_document,然后单击Knit to HTML激活图形的保存.这与交互式运行Rmd文档或使用Run All运行Rmd文档是分开的,也可以在以后运行. 查看下面的评论

Edit: Make sure the output type is set to output: html_document in the Rmd YAML header, and also click Knit to HTML to activate the saving of the figures. This is separate from, and can be done after, running the Rmd document interactively or with Run All. See comments below

---
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)
```

rmd来源

这是在testit下生成的数字

这篇关于R Notebook/Markdown不会使用"fig.path ="保存块图.块选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆