gpt4 book ai didi

r - 如何通过单击可执行的 r 文件从 rmd 脚本编织 pdf?

转载 作者:行者123 更新时间:2023-12-04 12:46:52 25 4
gpt4 key购买 nike

概要

我想通过单击文件/图标从 rmd 脚本生成一个 pdf 文件,这样我的同事就不会因为先打开 RStudio 而筋疲力尽。

问题

当我看到this在 R 博客上,并让它工作,我以为我正在接近完美的工作流程,从脚本到共享我的工作,让我的同事执行一个文件,结果得到一个带有更新数字的 pdf。但是,我无法让它与 knitr 库中的某些功能一起使用。

最好的情况是这个问题只有少数人感兴趣,但这里是:

您可以在下面看到名为 的文件中的脚本。 RexecKnit.Rmd .
它存在的唯一原因是,如果您愿意,您可以自己测试整个过程。顺便说一下,我在 64 位 Windows 7 上运行 RStudio 版本 0.99.467。

---
title: "Executable R, rmd and pdf"
header-includes: \usepackage{caption} \usepackage{fancyhdr}
output: pdf_document
fig_caption: no
---

\addtolength{\headheight}{0.5cm}
\pagestyle{fancyplain}
\renewcommand{\headrulewidth}{0pt}

```{r Settings, echo = FALSE, eval = TRUE, results = "hide", warning = FALSE, message = FALSE}
rm(list=ls())

pck_loaded <- (.packages())

# Packages to load
pck_toload <- c('ggplot2', 'xts', 'quantmod', 'zoo', 'PerformanceAnalytics',
'tseries', 'mvtnorm', 'data.table', 'XLConnect', 'sqldf', 'stargazer', 'xtable', 'gridExtra', 'grid', 'TTR')

# Load packages
for(i in 1:length(pck_toload)) {
if (!pck_toload[i] %in% pck_loaded)
print(pck_toload[i])
library(pck_toload[i], character.only = TRUE)
}

```

\captionsetup[table]{labelformat=empty}

```{r repex1, echo = FALSE, eval = TRUE, results = "asis", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}

# Data table with formatted numbers and text
v1 <- c("\\colorbox{white}{0.05}" , "\\colorbox{yellow}{0.57}", "\\colorbox{red}{-0.99}")
v2 <- c("An unexpected comment", "A qurious question", "And an insightful answer")
dt1 <- data.table(v1,v2)

# Data table using xtable
print(xtable(dt1,
caption = 'Fancy table'),
caption.placement = 'top',
comment = FALSE,
sanitize.text.function = function(x) x)
```

```{r repex2, echo = FALSE, eval = TRUE, results = "asis", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}

# Data table wiht random numbers
dt2 <- data.table(replicate(2,sample(0:100,10,rep=TRUE)))

# ggplot of random numbers
plx <- ggplot(data=dt2 ,aes(x=V1, y = V2))
plx <- plx + geom_line(color = 'blue', fill = 'grey')
plx <- plx + theme_classic()
plx <- plx + labs(title="Random numbers", x="x-axis",y="y-axis")
plot(plx)
```

我知道这是一个用于测试目的的相当冗长的脚本,但这只是为了确保当我双击这个小美女执行脚本时一切正常 .Rexe这是一个名为 的文件来电者 knitr.Rexe (就像在 R-Bloggers 帖子中一样)包含这段代码:
library(knitr)
library(rmarkdown)
setwd('C:/repos/r_tutorials/executable R files')
knit('RexecKnit.Rmd')
Sys.sleep(3)

这按预期工作。双击文件后,脚本将在不打开 R 或 Rstudio 的情况下运行,并在所需文件夹中生成一个 .md 文件。当它存储为 .Rexe 文件和 .R 文件时,相同的脚本也能工作。
但问题来了:

我想制作一个pdf,并根据提示 here , 替换
knit('RexecKnit.Rmd')


rmarkdown::render("RexecKnit.Rmd")

只要 YAML header 包含以下内容,就可以解决问题:
output: pdf_document

它确实有效(意味着 pdf 是使用 lenghty 脚本中指定的每个细节创建的),至少当它作为 .R 文件运行时。
令我失望的是,它在从 .Rexec 文件运行时不起作用,如下所示:
library(knitr)
library(rmarkdown)
setwd('C:/repos/r_tutorials/executable R files')
rmarkdown::render("RexecKnit.Rmd")
Sys.sleep(3)

谢谢你看这个!

最佳答案

假设已经独立安装了pandocWindows机器(在这种情况下,这被证明是问题的根源),她可以通过以下方式做到这一点:

第一 : 做一个 .R文件内容如下

library(knitr)
library(rmarkdown)
setwd('C:/repos/r_tutorials/executable R files')
knit("RexecKnit.Rmd")

# render the generated markdown file.
render("RexecKnit.md")

Sys.sleep(3)

第二 : 做一个 .bat文件

创建一个 .bat 文件,说“my_bat_file.bat”,并包含下面的文本。但是,必须调整路径:
"C:\R\R-3.2.2\bin\x64\R.exe" CMD BATCH --vanilla --slave "C:\path_to_your_file\your_file.R" "C:\path_to_your_file\your_file.Rout"

第三 : 指导 Windows Task Scheduler
如果数据更新频繁,可以通过Windows中的Task Scheduler来重复执行 .bat每周在晚上的某个时间提交一次,所以报告在早上就在那里。

关于r - 如何通过单击可执行的 r 文件从 rmd 脚本编织 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com