【问题标题】:Write pdf to figure folder, but delete pngs将pdf写入图形文件夹,但删除png
【发布时间】:2021-12-08 02:16:05
【问题描述】:

这是关于this answer 的后续问题。我已经设置了 knitr 块选项以在文件夹中输出 png 和 pdf 版本的绘图,以及在 knitted 报告中使用 png。

但是,我只想保留该图的 pdf 版本并丢弃 png 文件。编织后是否有与 on.exit() 等效的 knitr 来清理 png?还是我忽略的选项?

使用下面的rmarkdown文档,如何在编织后自动清理png版本的plot? (或者首先不将其作为独立文件生成)

---
title: "Untitled"
author: "Me"
date: "21/10/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  dev = c("png", "pdf"),
  fig.path = here::here(
    "figures",
    gsub("\\.Rmd$", "\\\\", basename(knitr::current_input()))
  )
)
```

```{r my_plot}
library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point()
```

【问题讨论】:

  • 我可能误解了这个问题,但是如果您不想创建png文件,为什么要设置dev = c("png","pdf")
  • 我希望输出文档包含 png 图像,但不包含 pdf 图像。我希望 pdf 文件作为为喜欢它们可用的合作者编织文档的副作用。我希望这是有道理的。

标签: r ggplot2 knitr


【解决方案1】:

这不是您要查找的内容,但手动从以下块中删除 eval=FALSE 会删除想要的文件:

```{r eval=FALSE, include=FALSE}
fList <-  dir("figures")
fList <- fList[stringr::str_detect(fList,"\\.png$")]
file.remove(paste0("figures/",fList))
```

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多