2017-07-14 106 views
2

我想根據模板創建不同的報告集。是否可以在命令行中運行Rmarkdown時提供參數?

  1. 是否可以從命令行運行此報告而不是通過RStudio(knit PDF)運行它?

  2. 我有一個稱爲應用程序的矢量,我爲每個應用程序運行此報告並輸出值。從命令行運行此報表時,是否可以通過命令行選項提供應用程序?我不需要在Rmarkdown中添加應用,而需要知道我是否可以提供這個參數作爲參數?

  3. 每次我運行這個,pdf文件名都是一樣的。我怎樣才能改變這個,使pdf文件名與應用程序的值相同?


    title: "Application Report" 
    
    date: "July 13th, 2017" 
    header-includes: 
        - \usepackage{longtable} 
        - \usepackage[table]{xcolor} 
        - \usepackage{colortbl} 
        - \usepackage[utf8]{inputenc} 
    output: 
        pdf_document: 
        fig_caption: yes 
        fig_height: 6 
        fig_width: 7 
        highlight: zenburn 
        number_sections: yes 
        toc: yes 
        toc_depth: 3 
    keep_tex: yes 
    tables: yes 
    fontsize: 15 
    --- 
    
    ```{r message=FALSE, results = 'asis', echo=FALSE, warning=FALSE, fig.width=12, fig.height=10} 
    
    app<-c("Web","DB) 
    
    
    for (i in app){ 
    
        cat(paste("# ",app, " - Application","\n")) 
    } 
    
+1

探索'params'選項。 –

回答

3

簡短的回答:

例子:我會用兩個文件和命令行ê xample。使用makefile或擴展knit-application-report.R腳本將簡化您的工作流程。

第一個文件:application-report.Rmd我已經從您的示例文件中簡化了這篇文章。重要的一點是變量app已被定義。該變量將用於報告標題中,並可用於報告中的其他地方。

--- 
title: "`r app` Report" 
date: "`r date()`" 
output: pdf_document 
--- 

This is the report for the `r app` application. 

```{r} 
# do stuff 
``` 

文件2:knit-application-report.R呼叫到commandArgs,與trailingOnly = TRUE通命令行參數轉換爲R腳本。應用程序的名稱作爲第一個也是唯一的參數傳入。該值存儲在app變量中,該變量將在調用rmarkdown::render時用於評估.Rmd文件。

# file: knit-application-report.R 
# 
# Commandline Arguments: 
# 1. appliction a character string for the app 

app <- commandArgs(trailingOnly = TRUE) 

rmarkdown::render(input = "application-report.Rmd", 
        output_file = paste0(app, ".pdf")) 

命令行看起來像這樣(從我的linux命令行)。

[email protected]:~$ Rscript knit-application-report.R MyApplication 


processing file: application-report.Rmd 
    |................................         | 50% 
    inline R code fragments 

    |.................................................................| 100% 
label: unnamed-chunk-1 

output file: application-report.knit.md 

/usr/bin/pandoc +RTS -K512m -RTS application-report.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output MyApplication.pdf --template /home/pdewitt/R-dev/R-3.4.1/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' 

Output created: MyApplication.pdf 

注意,輸出爲命名的報告MyApplication.pdf看起來像這樣:

enter image description here

+0

這真的很棒。如何在rmarkdown中使用應用程序塊? ''''r''' – user1471980

+1

塊選項是R表達式。例如,您可以設置'echo = app ==「MyApplication」'來回應僅用於'MyApplication'的塊,並禁止所有其他應用程序的報告中的代碼。 – Peter

+0

我需要在大塊中引用應用程序。我如何引用r塊中的應用程序的價值? – user1471980

相關問題