DeveauP
DeveauP

Reputation: 1237

Missing `parse` information inside vignette build

Goal

The goal is to create a package that parses R scripts and lists functions (from the package - like mvbutils- but also imports).

Function

The main function relies on parsing R script with d<-getParseData(x = parse(text = deparse(x)))

Reproducible code

For example in an interactive R session the output of

 x<-test<-function(x){x+1}
 d<-getParseData(x = parse(text = deparse(x)))

Has for first few lines:

   line1 col1 line2 col2 id parent          token terminal     text
23     1    1     4    1 23      0           expr    FALSE         
1      1    1     1    8  1     23       FUNCTION     TRUE function
2      1   10     1   10  2     23            '('     TRUE        (
3      1   11     1   11  3     23 SYMBOL_FORMALS     TRUE        x
4      1   12     1   12  4     23            ')'     TRUE        )   

Error

When building a vignette with knitr containing - either with knit html from RStudio or devtools::build_vignettes, the output of the previous chunk of code is NULL. On the other hand using "knitr::knit" inside an R session will give the correct output.

Questions:

Is there a reason for the parser to behave differently inside the knit function/environment, and is there a way to bypass this?

Update

Changing code to:

x<-test<-function(x){x+1}
d<-getParseData(x = parse(text = deparse(x),keep.source = TRUE))

Fixes the issue, but this does not answer the question of why the same function behaves differently.

Upvotes: 1

Views: 81

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30124

From the help page ?options:

keep.source:

When TRUE, the source code for functions (newly defined or loaded) is stored internally allowing comments to be kept in the right places. Retrieve the source by printing or using deparse(fn, control = "useSource").

The default is interactive(), i.e., TRUE for interactive use.

When building the vignette, you are running a non-interactive R session, so the source code is discarded in parse().

parse(file = "", n = NULL, text = NULL, prompt = "?",
      keep.source = getOption("keep.source"), srcfile,
      encoding = "unknown")

Upvotes: 1

Related Questions