problem with cache function

123 views
Skip to first unread message

Thomas Keller

unread,
Aug 16, 2012, 1:50:20 PM8/16/12
to ProjectTemplate
# cache as defined gives an error (using the tutorial 'letters'
project)
> cache
function (variable)
{
save(list = variable, envir = .GlobalEnv, file =
file.path("cache",
paste(variable, ".RData", sep = "")))
}
<environment: namespace:ProjectTemplate>
> save(list = first.letter.counts, envir = .GlobalEnv, file = file.path("cache", paste('first.letter.count', ".RData", sep="")))
Error in FUN(X[[1L]], ...) : invalid first argument

However this works:
> save(first.letter.counts, envir = .GlobalEnv, file = file.path("cache", paste('first.letter.count', ".RData", sep="")))

and this works:
> save(list = c('second.letter.counts'), envir = .GlobalEnv, file = file.path("cache", paste('second.letter.count', ".RData", sep="")))

I'm new to this, so if there is a better way to approach this, please
let me know.
cheers,
Tom K

John Myles White

unread,
Aug 16, 2012, 1:56:32 PM8/16/12
to project...@googlegroups.com
Hi Thomas,

I'll have to look into this later tonight, but my guess is that I used a calling convention for cache() in the tutorial that isn't implemented. Specifically, your example suggests that I suggested doing

cache(some.variable)

whereas you really need to run

cache('some.variable')

Does that solve your problem? If so, I'll update the docs to reflect that.

-- John

Thomas Keller

unread,
Aug 16, 2012, 4:11:49 PM8/16/12
to project...@googlegroups.com
Hi John,
I tried cache('some.variable') too and it gave the same error. It seems that the save function takes either a scalar or you ca give the list parameter a list, but you can't give list a scalar. 
So save(list = c('some.variable') ... ) works, or save('some.variable', ...) also works, but save(list = 'some.variable', ...) does not work.

John Myles White

unread,
Aug 16, 2012, 4:22:56 PM8/16/12
to project...@googlegroups.com
Can you submit a specific example of fully executable code that doesn't work so I can fix it tonight?

 -- John

Thomas Keller

unread,
Aug 16, 2012, 6:11:15 PM8/16/12
to project...@googlegroups.com
John,
I was just working through the tutorial and noticed that the example in the tutorial does not work.
 But you are correct that putting the variable name in quotes does work.
> library(ProjectTemplate)
Loading required package: testthat
Warning message:
package ‘testthat’ was built under R version 2.15.1 
> setwd("~/Computing/R/letters")
> load.project()
Loading project configuration
Autoloading helper functions
 Running helper script: helpers.R
Autoloading packages
 Loading package: reshape2
 Loading package: plyr
 Loading package: ggplot2
 Loading package: stringr
 Loading package: lubridate
Autoloading data
 Loading cached data set: first.letter.count
 Loading cached data set: second.letter.count
 Loading data set: letters
Warning message:
package ‘stringr’ was built under R version 2.15.1 
> cache(first.letter.counts)
Error in FUN(X[[1L]], ...) : invalid first argument
> cache('first.letter.counts')
> ## no error and the object was saved in ./cache

I could have sworn the latter command also gave the error previously. My mistake.

thanks,
Tom K

John Myles White

unread,
Aug 16, 2012, 6:15:31 PM8/16/12
to project...@googlegroups.com
Ok. To confirm: the bug is that tutorial asserts you can use an unquoted variable name, when, in fact, you need a quoted variable name?

 -- John
Reply all
Reply to author
Forward
0 new messages