Error in eval(expr, envir, enclos) when sourcing files inside the R file

3.5k Views Asked by At

I have a problem of environment when I try to "knit HTML" or generate a pdf with LyX. When I run directly the R code (or compile an HTML notebook with Rstudio), it's ok. I have done the test with [email protected] (dev version). I had not the problem with [email protected]

On the sample given here after (test.Rhtml > test.R > init.R > kCommon.R), The returned error is :

Period: From

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable

to
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable

test.R

## @knitr INIT
source("./init.R")
print(kStartDate)
print(kEndDate)

test.R sources init.R

if (!require(xtable)){
  install.packages("xtable")
  library(xtable)
}
source("./kCommon.R")

init.R sources kCommon.R

# Period
kStartDate   <- as.Date("2013-01-01", format="%Y-%m-%d")
kEndDate     <- as.Date("2013-06-30", format="%Y-%m-%d")

test.Rhtml

<html>

<head>
<title>TEST</title>
</head>

<body>

<!--begin.rcode set-options, echo=FALSE
output <- "html"
read_chunk('test.R')
opts_chunk$set(fig.width=7, fig.height=4.5, fig.align='center', tidy=FALSE, comment=NA) 
end.rcode-->
<!--begin.rcode INIT, echo=FALSE, message=FALSE, warning=FALSE
end.rcode-->
<p>
Period: From <!--rinline as.character(kStartDate) --> to <!--rinline as.character(kEndDate) -->
</p>


<hr/>
<p>
This document has been generated with <!--rinline version$version.string -->
on <!--rinline version$platform -->.  <br/>
[<!--rinline date() -->]
</p>
<!--begin.rcode FIN, echo=FALSE, message=FALSE, warning=FALSE
end.rcode-->

</body>
</html>

Session info

> library(knitr);sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.3.7

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.6 

formatR_0.9    stringr_0.6.2  tools_3.0.1   

Results (after removing the options)

output <- "html"
read_chunk("test.R")
opts_chunk$set(fig.width = 7, fig.height = 4.5, fig.align = "center", tidy = FALSE,
    comment = NA)
Period: From

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable

to
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable

This document has been generated with R version 3.0.1 (2013-05-16) on x86_64-apple-darwin10.8.0. 
[Thu Aug 8 10:35:22 2013]

#EOF#

Log

> require(knitr); knit('test.Rhtml', encoding='UTF-8');
Le chargement a nécessité le package : knitr


processing file: test.Rhtml
  |.........                                                        |  14%
  ordinary text without R code

  |...................                                              |  29%
label: set-options
  |............................                                     |  43%
  ordinary text without R code

  |.....................................                            |  57%
label: INIT
  |..............................................                   |  71%
   inline R code fragments

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable
  |........................................................         |  86%
label: FIN
  |.................................................................| 100%
  ordinary text without R code


output file: test.html

[1] "test.html"
> 
2

There are 2 best solutions below

0
On

The example code you gave in test.R in the post was overly simplified, and the script you sent to me is actually this:

##-------
## @knitr INIT
##-------
source("./init.R")

print(kStartDate)
print(kEndDate)

That makes a difference, because of the new syntax for external code chunks (search for ---- in the NEWS). If you just want to fold code chunks in RStudio, see this question Code folding in external files with knitr and RStudio

0
On

one way to solve the issue is to clear knit cache pressing the button or setting this environment variable to FALSE, see it below:

{r setup, include=TRUE, cache = FALSE}

Cheers!