r2rmd: Parse R source file and return R markdown

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/r2rmd.R

Description

Parses an R source file and returns an R markdown document that can be turned into a human readable documentation of what the source file does.

Usage

1
r2rmd(file, out=paste(file, "md", sep=""), header=TRUE, extra)

Arguments

file

a file name or connection (see readLines.

out

an output file name passed to writeLines, or NULL (no file written).

header

logical, if a yaml header (enclosed between tribple dashes, ---) is to be parsed.

extra

character, optional string that is placed into the code chunk openings.

Details

Leading double hashes ## treated as non-code. Leading # followed by other than # is code comment. Leading # after whitespace is code comment. A newline is code when preceded and followed by code.

The leading double hash ## is trimmed for comment lines. R markdown chunk start/end stuff is added for code chunks. The argument extra adds chunk arguments, e.g. extra=', eval=FALSE' etc. See R markdown website at https://rmarkdown.rstudio.com/

Value

Returns a character vector invisibly, and writes a file as a side effects unless out=NULL in which case no file is written.

Author(s)

Peter Solymos <solymos@ualberta.ca>

See Also

https://rmarkdown.rstudio.com/

Examples

1
2
3
4
5
## Not run: 
(r2rmd(system.file("r2rmd_example.R", package="mefa4"), 
    out=NULL, extra=", eval=FALSE"))

## End(Not run)

Example output

Loading required package: Matrix
Loading required package: pbapply
mefa4 0.3-4 	 2016-10-12
 [1] "---"                                                                         
 [2] "title: \"r2rmd function usage example\""                                     
 [3] "author: \"Peter Solymos\""                                                   
 [4] "date: \"May 13, 2015\""                                                      
 [5] "output: pdf_document"                                                        
 [6] "---"                                                                         
 [7] ""                                                                            
 [8] "# Rules"                                                                     
 [9] ""                                                                            
[10] " * leading `##` is treated as *non-code*"                                    
[11] " * leading `#` followed by other than `#` is *code comment*"                 
[12] " * leading `#` after whitespace is *code comment*"                           
[13] " * newline is *code* when preceded and followed by code"                     
[14] ""                                                                            
[15] " The leading double hash `##` trimmed for comment lines."                    
[16] " R markdown chunk start/end stuff is added for code chunks."                 
[17] " The argument `extra` adds chunk arguments, e.g. `extra=', eval=FALSE'` etc."
[18] " The function returns vector if `out=NULL`."                                 
[19] ""                                                                            
[20] "# Trivial example"                                                           
[21] ""                                                                            
[22] " Addition"                                                                   
[23] "```{r CHUNK_1, eval=FALSE}\na <- 1"                                          
[24] "b <- a + 1 # comment\n```\n"                                                 
[25] " Function"                                                                   
[26] "```{r CHUNK_2, eval=FALSE}\n# another comment"                               
[27] "f <- function(x) x + a"                                                      
[28] "f (b)\n```\n"                                                                
[29] " That's all folks!"                                                          
[30] ""                                                                            

mefa4 documentation built on Oct. 7, 2021, 1:06 a.m.