[Rcpp-devel] Manipulating exposed C++ classes in R

Dirk Eddelbuettel edd at debian.org
Tue Jan 25 04:01:00 CET 2011


On 25 January 2011 at 10:08, Mark.Palmer at csiro.au wrote:
| Hi,
| 
|  
| 
| I hope I don?t make too many naïve statements and assumptions in this post.
| Essentially what ive done is expose a C++ class to R using Rcpp modules. Within
| my C++ code I create an object of this class and pass it back to R, where its
| placed in a list. I then, at a later stage within R, want to get access to the
| elements in this list, whereupon I get the error message ?$ operator is invalid
| for atomic vectors?. I?m running R2.12.1 and presumably the latest versions  of

That error can be had with pure R code too:

  R> vv <- c(a=1, b=2, c=44)
  R> vv$c
  Error in vv$c : $ operator is invalid for atomic vectors
  R> vv[["c"]]
  [1] 44
  R> 

where lists and data.frames support it:

  R> dl <- data.frame(a=1,b=2,c="foo")
  R> dl$a
  [1] 1

Can you index your Rcpp modules-created object by name or position?

Otherwise, a small self-contained example would help. It's a little tricky to
follow your code example here...

Dirk

| the relevant packages, Rcpp, and inline.
| 
|  
| 
| In more detail, the following code in R creates an element in the list, in a
| function that is called by ?lapply?
| 
|  
| 
| tape  <- new(adFun)
| 
| tape$CreateTape(coefs, Datas)
| 
| tape$ForwardR(coefs)
| 
| result <- tape
| 
| return(result)
| 
|  
| 
| which returns to a function?, and the end result is a list  (Nllh.Check),
| within a function, created by
| 
|  
| 
| Nllh.Check <<- CheckGEVFit(AllData, NCovarsW, NCovarsWgev, npmu, npsc, npsh,
| Order, Theta.24, Ccoefs,
| 
|                               Nmodel, shlink =
| SHLINK, Which = WhichMH, adFun)
| 
|  
| 
| Because of the ?<<-?, I actually have saved Nllh.Check, and I can look at that
| from the R prompt,
| 
|  
| 
| > str(Nllh.Check)
| 
| List of 580
| 
|  $ :Formal class 'Rcpp_adFun' [package ".GlobalEnv"] with 1 slots
| 
|   .. ..@ .xData:<environment: 07c7bac8>
| 
|  $ :Formal class 'Rcpp_adFun' [package ".GlobalEnv"] with 1 slots
| 
|   .. ..@ .xData:<environment: 087055e8>
| 
|  $ :Formal class 'Rcpp_adFun' [package ".GlobalEnv"] with 1 slots
| 
|   .. ..@ .xData:<environment: 086a236c>   etc
| 
|  
| 
| And
| 
|  
| 
| > Nllh.Check[[1]]
| 
| C++ object <013CBD38> of class 'adFun' <01689C50>
| 
|  
| 
| Or calling one of the functions of the C++ class
| 
|  
| 
| > Nllh.Check[[400]]$TapeProperties()
| 
| Summary of tape
| 
| Domain dimension 6
| 
| Range dimension 1
| 
| 0-th component of range space corresponds to a parameter in the operation
| sequence 0
| 
| Number of variables in operation sequence + phantom 6754
| 
| Number of parameters in operation sequence  135
| 
| Number of operations 5429
| 
| Total number of operator arguments in operator sequence 10314
| 
| Total characters used in PrintFor 0
| 
| Number of VecAD vectors 0
| 
| Amount of memory required to store the operation sequence 64052
| 
|  
| 
| Which to me indicates that this object is ok, the list ive constructed is what
| I think it should be etc.  However if I try to do something within the
| function that created the list  like
| 
|  
| 
| Nllh.Check <<- CheckGEVFit(AllData, NCovarsW, NCovarsWgev, npmu, npsc, npsh,
| Order, Theta.24, Ccoefs,
| 
|                               Nmodel, shlink =
| SHLINK, Which = WhichMH, adFun)
| 
| Nllh.Check[[400]]$TapeProperties()
| 
|  
| 
| I get the error message
| 
| Error in Nllh.Check[[400]]$TapeProperties :
| 
|   $ operator is invalid for atomic vectors
| 
|  
| 
| So, to me the question is, is what I am trying to do possible, or am I going
| about it the wrong way, or lastly is it a bug?
| 
|  
| 
| I hope this is enough detail, and apologise if its not. Its not easy to produce
| a small illustrative example, because the above relies on an external C++
| package .
| 
|  
| 
| Thanks
| 
| Mark Palmer
| Senior Statistician 
| CSIRO Mathematics, Informatics and Statistics
| 
| Phone: +61 8 9333 6293 | Fax: +61 8 9333 6121 | Mobile: 0427502353
| Mark.Palmer at csiro.au | www.csiro.au | www.csiro.au/cmis
| Address: Private bag 5, Wembley, WA 6913, Australia
| 
| PLEASE NOTE
| The information contained in this email may be confidential or privileged. Any
| unauthorised use or disclosure is prohibited. If you have received this email
| in error, please delete it immediately and notify the sender by return email.
| Thank you. To the extent permitted by law, CSIRO does not represent, warrant
| and/or guarantee that the integrity of this communication has been maintained
| or that the communication is free of errors, virus, interception or
| interference.
| 
| Please consider the environment before printing this email.
| 
|  
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list