如何通过单击可执行文件r从rmd脚本编织pdf? [英] How to knit pdf from rmd script by clicking an executable r file?

查看:77
本文介绍了如何通过单击可执行文件r从rmd脚本编织pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

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

I would like to produce a pdf file from an rmd script just by clicking a file / an icon so that my coworkers don't exhaust themselves by opening RStudio first.

问题

当我在R上看到时-bloggers,并使其正常工作,我以为我正在通过让同事执行文件并获取带有更新数字的pdf来实现从脚本编写到共享我的工作的完美工作流程.但是,我无法使其与knitr库中的某些功能一起使用.

When I saw this on R-bloggers, and got it working, I thought I was approaching the perfect work flow from scripting to sharing my work by letting my coworkers execute a file and get a pdf with updated numbers as a result. However, I can't get it to work with some of the functions in the knitr library.

最好的情况是,这个问题仅对你们中的少数几个人很有趣,但这里有:

Best case scenario is that this question is interesting to only a few of you out there, but here goes:

下面您可以在名为 RexecKnit.Rmd 的文件中看到脚本. 它存在的唯一原因是,您可以根据需要自己测试整个过程.顺便说一句,我正在Windows 7(64位)上运行RStudio 0.99.467版.

Below you can see a script in a file called RexecKnit.Rmd. The only reason it's there is so you can test the whole procedure for yourselves if you want to. By the way, I'm running RStudio Version 0.99.467 on Windows 7, 64 bit.

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

我知道这是一个相当冗长的脚本,仅用于测试目的,但这只是为了确保当我双击该小命令执行脚本时,一切都能正常工作,这是一个名为<包含以下这段代码的strong>呼叫者knitr.Rexe (类似于R-Bloggers帖子):

I know that's a pretty lengthy script for testing purposes, but it's just to make sure that everything works when I execute the script upon double clicking this little beauty which is a file called caller knitr.Rexe (like in the R-Bloggers post) containing this little piece of code:

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

这按预期工作.双击文件后,无需打开R或Rstudio即可运行脚本,并在所需文件夹中生成一个.md文件.当将其存储为.Rexe文件和.R文件时,该脚本也可以工作. 但这是问题所在:

And this works as expected. Upon double klicking the file, the script is run without opening R or Rstudio, and a .md file is produced in the desired folder. And the same script works when it's stored as a .Rexe file and as a .R file. But here's the problem:

我想生成一个pdf文件,并根据提示此处,替换

I want to produce a pdf, and according to a tip here, replacing

knit('RexecKnit.Rmd')

使用

rmarkdown::render("RexecKnit.Rmd")

只要YAML标头包含以下内容,

就应该做到这一点:

should do the trick as long as the YAML header includes this:

output: pdf_document

它确实有效(这意味着pdf是使用lenghty脚本中指定的每个详细信息创建的),至少在以.R文件形式运行时是这样. 令我失望的是,当从.Rexec文件运行时,它无法正常工作:

And it does work (meaning that the pdf is created with every detail specified in the lenghty script), at least when it is run as an .R file. To my disappointment, it does NOT work when it is run from a .Rexec file like this:

library(knitr)
library(rmarkdown)
setwd('C:/repos/r_tutorials/executable R files')
rmarkdown::render("RexecKnit.Rmd")
Sys.sleep(3)

感谢您对此的关注!

推荐答案

假定一个人已经在Windows机器上独立安装了pandoc(在这种情况下是问题的根源),她可以通过以下方式进行操作:

Assuming one has independently installed pandoc on Windows machine (which turned out to be the root of the problem in this case), she can do it in the following manner:

第一:制作一个具有以下内容的.R文件

First: make a .R file with the content like below

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",并包含以下文本.但是,必须调整路径:

Create a .bat file, say "my_bat_file.bat", and include the text below. However, the paths have to be adjusted:

"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中的任务计划程序"的要求,以在晚上的特定时间每周一次重复执行.bat文件,因此报告在早晨出现.

If the data is updated frequently, one can shedule the Task Scheduler in Windows to repeatedly execute the .bat file once a week at certain time at night, so reports are there in the morning.

这篇关于如何通过单击可执行文件r从rmd脚本编织pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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