Skip to contents

This rule executes Markdown rendering to create text files in various supported formats such as PDF, DOCX, etc.

Usage

markdownRule(target, script, depends = NULL, params = list(), task = "all")

Arguments

target

Name of the output file to be created

script

Name of the markdown file to be rendered

depends

A vector of file names that the markdown script depends on, or NULL.

params

A list of R values that become available within the script in a params variable.

task

A character vector of parent task names. The mechanism of tasks allows grouping rules. Anything different from 'all' will cause the creation of a new task depending on the given rule. Executing make taskname will then force building this rule.

Value

Instance of S3 class rmake.rule

Details

This rule executes the following command in a separate R process: params <- params; rmarkdown::render(script, output_format=format, output_file=target)

That is, the parameters given in the params argument are stored in the global variable and then the script is rendered with rmarkdown. Note that the re-generation of the Makefile with any change to params will not cause the re-execution of the recipe unless other script dependencies change.

Issuing make clean from the shell causes removal of all files specified in the target parameter.

Author

Michal Burda

Examples

r <- markdownRule(target='report.pdf',
                  script='report.Rmd',
                  depends=c('data1.csv', 'data2.csv'))

# generate the content of a makefile (as character vector)
makefile(list(r))
#>  [1] "# This file is generated by rmake - do not edit by hand"                                                                              
#>  [2] "ifeq ($(origin .FEATURES),undefined)"                                                                                                 
#>  [3] "$(error This Makefile requires GNU Make. Please install it and use 'gmake' or 'make' depending on your system)"                       
#>  [4] "endif"                                                                                                                                
#>  [5] "REQUIRED_VERSION := 3.82"                                                                                                             
#>  [6] "ifeq ($(filter $(REQUIRED_VERSION),$(firstword $(sort $(MAKE_VERSION) $(REQUIRED_VERSION)))),)"                                       
#>  [7] "$(error GNU Make $(REQUIRED_VERSION) or higher is required. You are using $(MAKE_VERSION))"                                           
#>  [8] "endif"                                                                                                                                
#>  [9] "SHELL=/bin/sh"                                                                                                                        
#> [10] "R=\"$(R_HOME)/bin$(R_ARCH)/Rscript\""                                                                                                 
#> [11] "RM=rm"                                                                                                                                
#> [12] "CP=cp"                                                                                                                                
#> [13] ".ONESHELL:"                                                                                                                           
#> [14] ""                                                                                                                                     
#> [15] ".PHONY: all"                                                                                                                          
#> [16] "all: Makefile report.pdf"                                                                                                             
#> [17] "\t"                                                                                                                                   
#> [18] "report.pdf: report.Rmd data1.csv data2.csv"                                                                                           
#> [19] "\t$(R) - <<'EOFrmake'"                                                                                                                
#> [20] "\t{"                                                                                                                                  
#> [21] "\t    params <- list(.target = \"report.pdf\", .script = \"report.Rmd\", .depends = c(\"data1.csv\", \"data2.csv\"), .task = \"all\")"
#> [22] "\t    targets <- list(report.pdf = \"pdf_document\")"                                                                                 
#> [23] "\t    for (tg in names(targets)) {"                                                                                                   
#> [24] "\t        rmarkdown::render(\"report.Rmd\", output_format = targets[[tg]], output_file = tg)"                                         
#> [25] "\t    }"                                                                                                                              
#> [26] "\t}"                                                                                                                                  
#> [27] "\tEOFrmake"                                                                                                                           
#> [28] ".PHONY: clean"                                                                                                                        
#> [29] "clean: "                                                                                                                              
#> [30] "\t$(RM) report.pdf"                                                                                                                   
#> [31] "Makefile: Makefile.R"                                                                                                                 
#> [32] "\t$(R) - <<'EOFrmake'"                                                                                                                
#> [33] "\t{"                                                                                                                                  
#> [34] "\t    params <- list(.target = \"Makefile\", .script = \"Makefile.R\", .depends = NULL, .task = \"all\")"                             
#> [35] "\t    source(\"Makefile.R\")"                                                                                                         
#> [36] "\t}"                                                                                                                                  
#> [37] "\tEOFrmake"                                                                                                                           

# generate to file
tmp <- tempdir()
makefile(list(r), file.path(tmp, "Makefile"))