I am working on a R package using rstan. I have changed one parameter in the new stan code, but it is not revised in the new compiled program because I received this error messages:
"
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
variable does not exist; processing stage=data initialization; variable name=prec; base type=matrix_d"
but I don’t have variable “prec” any more, it has been changed to “prec2”
Hi, Ben,
I have used the bioconductor::biocCheck for this package which is developed via the rstan.package.skeleton(). I have received this note:
NOTE: Avoid sapply(); use vapply() found in files:
stanmodels.R (line 25, column 14)
I have changed sapply() to vapply(), but then I received this message:
"
Error in vapply(stan_files, function(f) { :
argument “FUN.VALUE” is missing, with no default
"
The FUN.VALUE seems to be the stanmode code. I don’t want to change the stanmodel’s original purpose. Could you please advise?
I would change sapply to lapply, but if you are going to use vapply then you have to specify FUN.VALUE according to its documentation. In this case, the return is an object of S4 class stanmodel.
I have changed to lapply(), the return is an object not a vector.
names(stanmodels) = sub("\.stan$", “”, basename(names(stanmodels)))
Error in basename(names(stanmodels)) :
a character vector argument expected
If I used vapply(), and define the returning value as stanmodel,
then it is okay, but when I compile it , it shows
Error in vapply(stan_files, function(f) { :
values must be type ‘character’,
but FUN(X[[1]]) result is type ‘S4’
What do they have against sapply out of curiosity?
And what does your actual call to vapply look like? My hunch is that FUN.VALUE is specified incorrectly as a string rather than as an actual S4 object. Hard to know without seeing what you’re actually trying.
names(stanmodels) = sub(“\.stan$”, “”, basename(names(stanmodels)))
Error in basename(names(stanmodels)) :
a character vector argument expected
use an empty object:
xobj=rstan::stan_model()
I will have a warning, and also this message :
names(stanmodels) = sub(“\.stan$”, “”, basename(names(stanmodels)))
Error in basename(names(stanmodels)) :
a character vector argument expected
I think, lapply() or vapply() requires return value to be vector or matrix, sapply( ) does not have this restriction? I probability have to use sapply()
sapply is never the one and only way to do something since it’s just avoiding writing a loop.
Is there anything else different in this file than in the original other than changing sapply to lapply? I think sapply was returning a list too in this case, so I think @bgoodri is right but I’m not sure why lapply doesn’t work for you .
The only difference is replace sapply() by lapply(), if using lapply().
If using vapply(), I have specified the FUN.VALUE to “stanmodel” which is an object.
If you’re still using the quotes around stanmodel (“stanmodel”) then it’s still a string naming the object and not that object itself. If stanmodel is being used in quotes I would remove them.
Thank you for the suggestions. vapply() need to specify FUN.VALUE to be an empty object, use new(), I have not made it working though.
I now found the difference in using sapply() vs lapply() for this case.
sapply() has the name of the stan model code as one of its attribute in the returning value, but lapply() only has the stan model code without the name of the code. That is why if I use lapply(), I will have error message from “basename(names(stanmodels))”.
This is the stanmodels result heading from sapply():
$exmple_code.stan
S4 class stanmodel ‘example_code’ coded as follows:
//RSTAN code for mlmm() function
//include “license.stan” // GPL2+
And the result heading from lapply():
[[1]]
S4 class stanmodel ‘example_code’ coded as follows:
//RSTAN code for example() function
//include “license.stan” // GPL2+
Thank you, there is no error message when running the stanmodel code.
While I receive this when using ::check() or ::build() though
"
Loading required package: Rcpp
Error in is(module, “character”) : object ‘m’ not found
"
The names(stan_files) dose not pass on the stan code name, because I tested it and it came back with
"
stanmodels
named list()
"
Instead of the stan model codes when using sapply() version.