Sweave can't see a vector if run from a function? - r

I have a function that sets a vector to a string, copies a Sweave document with a new name and then runs that Sweave. Inside the Sweave document I want to use the vector I set in the function, but it doesn't seem to see it.
(Edit: I changed this function to use tempdir(() as suggested by Dirk)
I created a sweave file test_sweave.rnw;
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{gb02413}
\maketitle
<<>>=
ls()
Sys.time()
print(paste("The chosen study was ",chstud,sep=""))
#
\end{document}
and I have this function;
onOK <- function(){
chstud<-"test"
message(paste("Chosen Study is ",chstud,sep=""))
newfile<-paste(chstud,"_report",sep="")
mypath<-paste(tempdir(),"\\",sep="")
setwd(mypath)
message(paste("Copying test_sweave.Rnw to ",paste(mypath,newfile,".Rnw",sep=""),sep=""))
file.copy("c:\\local\\test_sweave.Rnw",
paste(mypath,newfile,".Rnw",sep=""), overwrite=TRUE)
Sweave(paste(mypath,newfile,".Rnw",sep=""))
require(tools)
texi2dvi(file = paste(mypath,newfile,".tex",sep=""), pdf = TRUE)
}
If I run the code from the function directly, the resulting file has this output for ls();
> ls()
[1] "chstud" "mypath" "newfile" "onOK"
However If I call onOK() I get this output;
> ls()
[1] "onOK"
and the print(...chstud...)) function generates an error.
I suspect this is an environment problem, but I assumed because the call to Sweave occurs within the onOK function, it would be in the same enviroment, and would see all the objects created within the function. How can I get the Sweave process to see the chstud vector ?
Thanks
Paul.

I have similar problem. Eventually I found a work-around that "works for me", although it may not be the most elegant way to solve this issue.
Within my function, before 'Sweave' is executed, I put a statement to globally store the local environment:
temp <<- environment()
Using your code example it would look something like:
testFoo<-function(){
foo<-"My Test String"
temp <<- environment()
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo) testFoo()
Then in the LaTeX file to be 'Sweaved' in the beginning of the first chunk I restore the necessary variables, but you can also use 'temp$foo' to access the 'foo' variable that was created within the function. This way I avoid globally storing multiple variables.
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{Paul Hurley}
\maketitle
<<>>=
if(exists("foo", envir=temp)) { print(temp$foo) }
ls()
Sys.time()
#
\end{document}
Like I wrote above - this works, but is not very elegant.

OK, I realise that my initial ideas of a 'simple, self contained example' wasn't particularly simple or usefull. So I redid my example and got a sort of answer for myself, although I'd love someone to exaplain why and even suggest a better solution,
Here's my example test_sweave.Rnw file
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{Paul Hurley}
\maketitle
<<>>=
if(exists("foo")){print(foo)}
ls()
Sys.time()
#
\end{document}
If I run this code;
testFoo<-function(){
foo<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo) testFoo()
my resulting file does NOT contain the contents of the string foo.
> if (exists("foo")) {
+ print(foo)
+ }
> ls()
[1] "testFoo"
If I run this code (i.e, the same thing, just run directly)
rm(foo)
foo<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
my resulting file does contain the foo string
> if (exists("foo")) {
+ print(foo)
+ }
[1] "My Test String"
> ls()
[1] "foo" "testFoo"
and if I run this code
testBar<-function(){
foo<<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo)
testBar()
My resulting file also contains the foo string
> if (exists("foo")) {
+ print(foo)
+ }
[1] "My Test String"
> ls()
[1] "foo" "testBar" "testFoo"
So, it seems that sweave runs in the global environment, not in the environment it was called from. This means the only way to pass variables to sweave when sweave is run from a function is to use the <<- operator to put the variable in the global environment. (I think).
Anyone else want to comment who knows more about environments ?

Related

rmarkdown: recursive chunk evaluation?

I would like to have a string produced by an R chunk in an rmarkdown document to be re-evaluated - is that possible? For demonstration purposes consider the following document:
---
params:
B: 'test'
---
```{r simple, results='asis', echo=FALSE}
write(params[['B']], file = "")
```
```{r recursive-evaluation-questionmark, results='asis', echo=FALSE}
write(
"How to get \"params[['B']]\" evaluated here? This \"`r params[['B']]`\" is
expected to be \"test\" ...",
file = "")
```
Currently this produces the following when knitted:
test
How to get “params[[‘B’]]” evaluated here? This “r params[['B']]” is expected to be “test” …
but I want:
test
How to get “params[[‘B’]]” evaluated here? This “test” is expected to be “test” …
You can't get knitr to treat R code as text, but you can solve the problem using pure R code. I'd also recommend using cat() instead of write(). For your example,
write(
paste0("How to get \"params[['B']]\" evaluated here? This \"", params[['B']], "\" is
expected to be \"test\" ..."),
file = "")
or the simpler
cat("How to get \"params[['B']]\" evaluated here? This \"", params[['B']], "\" is
expected to be \"test\" ...", sep = "")
For more complicated macro-like substitutions, you may want to use the sub() or gsub() functions, e.g.
msg <- "How to get \"params[['B']]\" evaluated here? This \"%param%\" is
expected to be \"test\" ..."
cat(sub("%param%", params[['B']], msg))

Having multiple pander()s in a function

How do I create multiple outputs via pander() in a knitted document "asis" ?
When I have multiple calls of pander in a function, only the most recent one is shown in the HTML output. Here's an example:
tmp = function() {
pander('A')
pander('B')
pander('C')
}
tmp()
In the knitted document this gives: C
I could set panderOptions('knitr.auto.asis', FALSE) or I could use cat() so that the pander() output is written to the standard output. But then it's formatted as code, not as part of the document. As I need pander() to format a few tables for me, this does not help.
The tmp function will return only the last object -- that's why only C is printed. If you want to write each object to the stdout right away without the auto-asis convenience option, then you have to both disable the option like you did and use the relate knitr chunk option, eg:
```{r results='asis'}
library(pander)
panderOptions('knitr.auto.asis', FALSE)
tmp = function() {
pander('A')
pander('B')
pander('C')
}
tmp()
```
See more examples in the related "Using pander with knitr" vignette.

knitr HTML output too large

I have been using rmarkdown/knitr's knit to html capability to generate html code for some blogs. I've found it extremely helpful and convenient, but have been running into some problems lately with file size.
When I knit a script that has graphics that use shapefiles or ggmap images, the html file gets too big for the blog host to make sense of it (I've tried with both blogger and wordpress). I believe this has to do with the relatively large data.frames/files that are the shapefiles/ggmap being put into html form. Is there anything I can do to get a smaller html file that can be parsed by a blog host?
For reference, the html output from an rmarkdown script with one graphic using a ggmap layer, a layer of shapefiles and some data is 1.90MB, which is too big for blogger or wordpress to handle in html input. Thanks for any ideas.
Below are 3 different options to help you reduce the file size of HTML files with encoded images.
1. Optimize an existing HTML file
You can run this Python script on an existing HTML file. The script will:
decode the base64 encoded images
run pngquant to optimize the images
re-encode the optimized images as base64
Usage:
python optimize_html.py infile.html
It writes output to infile-optimized.html.
2. Use the built-in knitr hook for optimizing PNG images
knitr 1.15 includes a hook called hook_optipng that will run the optipng program on generated PNG files to reduce file size.
Here is a .Rmd example (taken from: knitr-examples/035-optipng.Rmd):
# 035-optipng.Rmd
This demo shows you how to optimize PNG images with `optipng`.
```{r setup}
library(knitr)
knit_hooks$set(optipng = hook_optipng)
```
Now we set the chunk option `optipng` to a non-`NULL` value,
e.g. `optipng=''`, to activate the hook. This string is passed to
`optipng`, so you can use `optipng='-o7'` to optimize more heavily.
```{r use-optipng, optipng=''}
library(methods)
library(ggplot2)
set.seed(123)
qplot(rnorm(1e3), rnorm(1e3))
```
3. Write your own knitr hook for any image optimizer
Writing your own hook is also quite easy, so I wrote a hook that calls the pngquant program. I find that pngquant runs faster, and the output files are smaller and look better.
Here is a .R example that defines and uses hook_pngquant (taken from this gist).
#' ---
#' title: "pngquant demo"
#' author: "Kamil Slowikowski"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' self_contained: true
#' ---
#+ setup, include=FALSE
library(knitr)
# Functions taken from knitr/R/utils.R
all_figs = function(options, ext = options$fig.ext, num = options$fig.num) {
fig_path(ext, options, number = seq_len(num))
}
in_dir = function(dir, expr) {
if (!is.null(dir)) {
owd = setwd(dir); on.exit(setwd(owd))
}
wd1 = getwd()
res = expr
wd2 = getwd()
if (wd1 != wd2) warning(
'You changed the working directory to ', wd2, ' (probably via setwd()). ',
'It will be restored to ', wd1, '. See the Note section in ?knitr::knit'
)
res
}
is_windows = function() .Platform$OS.type == 'windows'
in_base_dir = function(expr) {
d = opts_knit$get('base.dir')
if (is.character(d) && !file_test('-d', d)) dir.create(d, recursive = TRUE)
in_dir(d, expr)
}
# Here is the code you can modify to use any image optimizer.
hook_pngquant <- function(before, options, envir) {
if (before)
return()
ext = tolower(options$fig.ext)
if (ext != "png") {
warning("this hook only works with PNG")
return()
}
if (!nzchar(Sys.which("pngquant"))) {
warning("cannot find pngquant; please install and put it in PATH")
return()
}
paths = all_figs(options, ext)
in_base_dir(lapply(paths, function(x) {
message("optimizing ", x)
cmd = paste(
"pngquant",
if (is.character(options$pngquant)) options$pngquant,
shQuote(x)
)
message(cmd)
(if (is_windows())
shell
else system)(cmd)
x_opt = sub("\\.png$", "-fs8.png", x)
file.rename(x_opt, x)
}))
return()
}
# Enable this hook in this R script.
knit_hooks$set(
pngquant = hook_pngquant
)
#' Here we set the chunk option `pngquant='--speed=1 --quality=0-50'`,
#' which activates the hook.
#+ use-pngquant, pngquant='--speed=1 --quality=0-50'
library(methods)
library(ggplot2)
set.seed(123)
qplot(rnorm(1e3), rnorm(1e3))
I prefer to write my reports in R scripts (.R) instead of R markdown documents (.Rmd). See http://yihui.name/knitr/demo/stitch/ for more information on how to do that.
One thing you could do would be to not use embedded image and other resources. To achieve this, you can set the self_contained option in the YAML header for your document to false, e.g.:
---
output:
html_document:
self_contained: false
---
More info here: http://rmarkdown.rstudio.com/html_document_format.html

How do I set conditional LaTeX code using R?

Suppose in my R/Sweave created LaTeX document, I want to have \subsection{Foo} if in my R code we have that x==1, whereas if x==2 in my R code I want \subsection{Bar} to be displayed.
How do I accomplish this? I know how to include R code by encapsulating it in <<>>= and # but I'm not sure how to do something such as the following (perhaps nonsensical) pseudo-code:
if(x==1)
Show \subsection{Foo} in LaTeX document
else if(x==2)
Show \subsection{Bar} in LaTeX document
What I also want to do that is very closely related is have subheading \subsection{z} in my LaTeX document, where z is defined strictly in my R code as some arbitrary string that I can toggle.
\Sexpr{} can evaluate expressions inline:
Show \subsection{\Sexpr{if(x==1) "Foo" else "Bar"}} in LaTeX document
\subsection*{\Sexpr{z}}
Use results=tex and cat the LaTeX code you want.
<<results=tex>>=
if(x==1) {
z <- "Foo"
} else if(x==2) {
z <- "Bar"
}
cat("\\subsection{", z, "}\n", sep="")
#

Sweave + RweaveHTML: cat output does not appear in the output

I have a problem with Sweave + RweaveHTML
I want the output of cat ends to up in the html file being generated. I have a case in which it does not and I can't figure out why :(
test = function()
{
#bla bla;
cat("Result is...")
}
And then in the Rnw file I tried all of these:
<<echo=FALSE, results=html, include=TRUE>>=
test()
#
<<results=html, include=TRUE>>=
test()
#
<<results=html>>=
test()
#
<<>>=
test()
#
But I don't get the cat output in the resulting HTML file.
I'm pretty sure this is supposed to work...
Any ideas of what I'm supposed to do to get the stdout ouput to the final html file?
Thx!
The RweaveHTML driver works differently than the RweaveLatex driver in that to create output, the result from every line of code is processed with the generic function HTML. Other ways of creating output don't work. So to get output from within a function, there are two ways I know of; one is to return a value to be processed by the HTML generic, and the other is to call HTML directly. The following replacement of your test function demonstrates both.
test <- function() {
#bla bla;
HTML("Result is...")
"Return value is"
}
It's also possible to replace cat with HTML; then your original function would work. But it's a bit of a hack and could have unforeseen consequences; you'd put
cat <- HTML
in a (probably hidden) Sweave chunk at the beginning of the document.

Resources