[Rcpp-devel] Extract a function from a list and create a call
Douglas Bates
bates at stat.wisc.edu
Fri Mar 19 20:21:59 CET 2010
I must be missing something horribly obvious but I have now spent
several hours trying to find a way to extract a character string from
a list. I am more-or-less certain that I am copying a construction in
the unitTests/runit.CharacterVector.R but the compiler just keeps
spitting out error messages at me.
The background is that a glm "family" object in R is a named list (or
should be, it has an S3 class but that doesn't mean anything about the
structure). The standard ones look like
> str(poisson())
List of 12
$ family : chr "poisson"
$ link : chr "log"
$ linkfun :function (mu)
$ linkinv :function (eta)
$ variance :function (mu)
$ dev.resids:function (y, mu, wt)
$ aic :function (y, n, mu, wt, dev)
$ mu.eta :function (eta)
$ initialize: expression({ if (any(y < 0))
stop("negative values not allowed for the Poisson family") n <-
rep.int(1, nobs) mustart <- y + 0.1 })
$ validmu :function (mu)
$ valideta :function (eta)
$ simulate :function (object, nsim)
- attr(*, "class")= chr "family"
All I want to do is to take such a list and extract the "family" and
"link" strings as std::string. If the name of the Rcpp::List object
in the C++ code is lst, I can get as far as
StringVector fam = lst["family"];
but after that it all turns to custard. In the class I am defining
the family member is declared as a std::string. In the constructor
for the class from a List object I try
family = Rcpp::as<std::string>(fam[0]);
or even, mimicking a couple of the tests in unitTests/runit.CharacterVector.R,
family += fam[0];
but I have not been able to construct anything that my compiler will accept.
So, how do I get the value of one of the elements of a CharacterVector
(or StringVector, I think they are synonyms) as a std::string, short
of using std::string(CHAR(STRING_ELT(fam, 0))
More information about the Rcpp-devel
mailing list