Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sexpr values from external chunk not found #229

Closed
fonnesbeck opened this issue May 14, 2012 · 11 comments
Closed

Sexpr values from external chunk not found #229

fonnesbeck opened this issue May 14, 2012 · 11 comments
Labels
bug Bugs
Milestone

Comments

@fonnesbeck
Copy link

I have a large external R script, which I am trying to incorporate via knitr's read_chunk function. However, for some reason none of the variables referred to in the surrounding text are able to be found. I have confirmed that the R script runs cleanly on its own, so it is not a case of the script failing before variables are defined. I had a look to see if others have had similar problems, but could not find anything, so I assume I am doing something wrong.

I am reading in the file using read_chunk in a setup section:

<<set-options, echo=FALSE, cache=FALSE>>=
read_chunk("study.R")
@

And below that, I use pieces of the script that I have annotated as follows:

<<km, echo=FALSE, results='hide'>>=
@

So, to give one example, there is an object d2 that is defined in the script within the km section, and in the text below, I refer to it as follows:

There are at present \Sexpr{sum(d2$recur)} total events (recurrences or deaths), and \Sexpr{sum(d2$death)}
 deaths out of \Sexpr{nrow(d2)} patients randomized.

However, I end up with errors as follows:

Error in eval(expr, envir, enclos) : object 'd2' not found
Error in eval(expr, envir, enclos) : object 'd2' not found
Error in nrow(d2) : object 'd2' not found

In fact, there is not a single instance of a Sexpr statement being properly processed. Any idea what I am doing wrong?

@yihui
Copy link
Owner

yihui commented May 15, 2012

so you have a line like ## @knitr km in study.R? and what is your sessionInfo()?

@mnel
Copy link
Contributor

mnel commented May 15, 2012

Do you use plyr or reshape2

If so, it could be related to #207,

you may need to update to the most recent github version of reshape2

see #18.

@fonnesbeck
Copy link
Author

I do not use plyr or reshape2 for this project.

I do indeed have knitr markup in my code. For example:

## @knitr "km"

## Estimate KM curves
pdf("Fig/kmGI.pdf", width=10, height=10)
par(mfrow=c(2,2),las=1)

etc.

Here is my sessionInfo after running the script:

> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

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

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

other attached packages:
[1] knitr_0.3.4   ggplot2_0.9.0

loaded via a namespace (and not attached):
 [1] codetools_0.2-8    colorspace_1.1-1   dichromat_1.2-4    digest_0.5.1       evaluate_0.4.1    
 [6] formatR_0.3-4      grid_2.14.1        highlight_0.3.1    MASS_7.3-16        memoise_0.1       
[11] munsell_0.3        parser_0.0-14      plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5
[16] Rcpp_0.9.10        reshape2_1.2.1     scales_0.2.0       stringr_0.6        tools_2.14.1 

@mnel
Copy link
Contributor

mnel commented May 16, 2012

Try

## @knitr km

I tried on a simple example, and removing the quotation marks worked

@fonnesbeck
Copy link
Author

This did not do the trick, unfortunately. Is there a problem with referencing variables from a previous chunk? For example, I have:

<<base, echo=FALSE, results='hide'>>=
@

\section{Pre-planned Analysis (All data)}

\subsection{Kaplan Meier Estimates}

<<km, echo=FALSE, results='hide'>>=
@
%\newpage
There are at present \Sexpr{sum(d2$recur)} total events (recurrences or deaths), and \Sexpr{sum(d2$death)}
 deaths out of \Sexpr{nrow(d2)} patients randomized. 

...

where the object d2 is defined in base rather than in km. That should not matter, right? Yet, I get:

  |>>>>>>>>>>                                                       |  16%
   inline R code fragments

Error in eval(expr, envir, enclos) : object 'd2' not found
Error in eval(expr, envir, enclos) : object 'd2' not found
Error in nrow(d2) : object 'd2' not found
Error in eval(expr, envir, enclos) : object 'ncut' not found 

@mnel
Copy link
Contributor

mnel commented May 16, 2012

Hmmmm. Are d2 etc in the global environment when you finish knitting the document?

If you have cache=T then have you deleted the cache files to force everything to be reevaluated.

@fonnesbeck
Copy link
Author

They are not when I finish knitting the document, but they are if I just run the R script on its own.

I am not cacheing anything, so that should not be the issue.

@yihui
Copy link
Owner

yihui commented May 16, 2012

Can you remove echo=FALSE, results='hide' for the chunk km and check if this chunk is evaluated at all? I mean check the tex output.

Although I do not think the version matters for this problem, the current knitr version on CRAN is 0.5 now, so you may update it before moving on.

@fonnesbeck
Copy link
Author

I think I discovered the problem: d2 was defined in a chunk of code previous to "km" that was not given a knitr label. I made a label at the beginning of the file, and now d2 etc. are seen. Sorry for the confusion. I did not realize that all portions of the code needed to have a @knitr section label.

@yihui
Copy link
Owner

yihui commented May 17, 2012

You are welcome. Glad you sorted it out finally!

yihui added a commit that referenced this issue May 22, 2012
…can realize the problem when the chunk is not supposed to be empty; for #229
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

No branches or pull requests

3 participants