This function is designed to insert the output of each chunk of R code into the source code without really breaking the source code, since the output is masked in comments.

tidy_eval(source = "clipboard", ..., file = "", prefix = "## ", envir = parent.frame())

Arguments

source

the input filename (by default the clipboard; see tidy_source)

...

other arguments passed to tidy_source

file

the file to write by cat; by default the output is printed on screen

prefix

the prefix to mask the output

envir

the environment in which to evaluate the code (by default the parent environment; if we do not want to mess up with the parent environment, we can set envir = NULL or envir = new.env())

Value

Evaluated R code with corresponding output (printed on screen or written in a file).

References

https://yihui.name/formatR

Examples

library(formatR) ## evaluate simple code as a character vector tidy_eval(text = c("a<-1+1;a", "matrix(rnorm(10),5)"))
#> a <- 1 + 1 #> a #> ## [1] 2 #> #> matrix(rnorm(10), 5) #> ## [,1] [,2] #> ## [1,] -0.08433889 -0.8571904 #> ## [2,] -0.55406480 -1.5247442 #> ## [3,] 0.74717660 1.9694248 #> ## [4,] -0.93481966 0.4631747 #> ## [5,] -0.46662045 -0.8561524 #>
## evaluate a file tidy_eval(system.file("format", "messy.R", package = "formatR"))
#> # a single line of comments is preserved #> 1 + 1 #> ## [1] 2 #> #> #> if (TRUE) { #> x = 1 # inline comments #> } else { #> x = 2 #> print("Oh no... ask the right bracket to go away!") #> } #> 1 * 3 # one space before this comment will become two! #> ## [1] 3 #> #> 2 + 2 + 2 # 'short comments' #> ## [1] 6 #> #> #> # only 'single quotes' are allowed in comments #> df = data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100)) #> lm(y ~ x1 + x2, data = df) #> ## #> ## Call: #> ## lm(formula = y ~ x1 + x2, data = df) #> ## #> ## Coefficients: #> ## (Intercept) x1 x2 #> ## 0.1742 0.0723 -0.2047 #> ## #> #> 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + #> 1 + 1 ## comments after a long line #> ## [1] 23 #> #> #> ## here is a long long long long long long long long long long long long long long #> ## long long long long long long comment