Skip to content

[Rcpp-devel] Locating an Instance of a Reference Class in the Environment

10 messages · Matthew Supernaw - NOAA Federal, Dirk Eddelbuettel, Jeff Newmiller

#
I'm trying to get an instance of a reference class
(vonBertalanffyInterface) from the R environment with the following code:

void SetName(vonBertalanffyInterface& v) {

    Rcpp::Environment env = Rcpp::Environment::global_env();
    Rcpp::List l = Rcpp::as<Rcpp::List>(env.ls(true));

    for (int i = 0; i < l.size(); i++) {
        std::cout << typeid (l[i]).name() << std::endl;

        if(&v == l[i].get()){
            v.name Rcpp::as<std::string>(l[i]);
        }
    }
}

The problem is that elements in the list are of type
*N4Rcpp8internal13generic_proxyILi19ENS_15PreserveStorageEEE*.

I'm assuming that PreserveStorage is a wrapper class. Is there a way to
retrieve the reference class from the PreserveStorage object?
Thanks.
Matthew
1 day later
#
On 22 March 2023 at 13:31, Matthew Supernaw - NOAA Federal wrote:
| I'm trying to get an instance of a reference class (vonBertalanffyInterface)
| from the R environment with the following code:
| 
| void SetName(vonBertalanffyInterface& v) {
| 
|     Rcpp::Environment env = Rcpp::Environment::global_env();
|     Rcpp::List l = Rcpp::as<Rcpp::List>(env.ls(true));
| 
|     for (int i = 0; i < l.size(); i++) {
|         std::cout << typeid (l[i]).name() << std::endl;
|        
|         if(&v == l[i].get()){
|             v.name Rcpp::as<std::string>(l[i]);
|         }
|     }
| }
| 
| The problem is that elements in the list are of type 
| N4Rcpp8internal13generic_proxyILi19ENS_15PreserveStorageEEE.
| 
| I'm assuming that PreserveStorage is a wrapper class. Is there a way to
| retrieve the reference class from the PreserveStorage object?

Interesting question.  Can you do it in plain R (once you created the object) ?

Dirk
#
I'm sure it's possible to do from the R side, however, the requirement is
to hide this from the end user and set the reference objects name to the
declared name from the R environment. If I view the environment in RStudio,
there is much more information available. ls() only returns a list of
objects by their declared name. I'm wondering how to get more details about
elements in the environment.
On Thu, Mar 23, 2023 at 6:03 PM Dirk Eddelbuettel <edd at debian.org> wrote:

            

  
    
#
On 24 March 2023 at 13:08, Matthew Supernaw - NOAA Federal wrote:
| I'm sure it's possible to do from the R side, however, the requirement is to
| hide this from the end user and set the reference objects name to the declared
| name from the R environment. If I view the environment in RStudio, there is
| much more information available. ls() only returns a list of objects by their
| declared name. I'm wondering how to get more details about elements in the
| environment.

Thanks for additional color.  That may be a very interesting and valuable
question _for the C API of R_ and youe guess of "maybe Rcpp has a helper" is
a good one.  Sadly, I fear we may not have anything for you.  

Dirk
#
Matthew... the normal context for sharing Rcpp code is within a package. It is a no-brainer to have an R API called by the user that calls whatever Rcpp code it needs to, which is why your objection seems inappropriate to me.
On March 24, 2023 10:43:34 AM PDT, Dirk Eddelbuettel <edd at debian.org> wrote:
#
Hi Jeff,
I'm not sure I understand your comment. The code is part of a package.
We're just trying to sync the declared object name in the R environment to
the object in the Rcpp module programmatically from c++ for internal use. I
just wondered if there was a way to access the wrapped object in the
Rcpp:::PreserveStorage class and Dirk answered that question.
Thanks.
Matthew


On Fri, Mar 24, 2023 at 2:34 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

  
    
#
Well, feel free to ignore my post, but the gist was that anything you have access to in R can be passed by your R API code to your Rcpp code. Also, due to R's GC you should usually avoid caching references between calls to your Rcpp code anyway.
On March 24, 2023 12:40:43 PM PDT, Matthew Supernaw - NOAA Federal <matthew.supernaw at noaa.gov> wrote:

  
    
3 days later
#
Hi Dirk,
Thanks for your attention on this. I was able to find a solution that works
for us.
Matthew

https://github.com/NOAA-FIMS/ModularTMBExample/blob/map/src/tmb_objective_function.cpp#L133

On Fri, Mar 24, 2023 at 4:36 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

  
    
#
Hi Matthew,

Thanks for circling back!
On 28 March 2023 at 11:23, Matthew Supernaw - NOAA Federal wrote:
| Hi Dirk,
| Thanks for your attention on this. I was able to find a solution that works for
| us.
| Matthew
| 
| https://github.com/NOAA-FIMS/ModularTMBExample/blob/map/src/
| tmb_objective_function.cpp#L133

Ok -- if you want we can maybe offline work on smoothing that over. You have
Rcpp already at your disposal there, you should not need the darn PROTECT and
UNPROTECT that makes my eyes burn.

Total aside:  I needed a finger exercise for turning factor to character and
vice verse and just did this (for now (?) in RcppExamples):

https://github.com/eddelbuettel/rcppexamples/blob/master/src/makeFactor.cpp

and looks like I forgot to rename the file which I may do to FactorExample.cpp

Dirk
 
| On Fri, Mar 24, 2023 at 4:36 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
| wrote:
| 
|     Well, feel free to ignore my post, but the gist was that anything you have
|     access to in R can be passed by your R API code to your Rcpp code. Also,
|     due to R's GC you should usually avoid caching references between calls to
|     your Rcpp code anyway.
| 
|     On March 24, 2023 12:40:43 PM PDT, Matthew Supernaw - NOAA Federal <
| matthew.supernaw at noaa.gov> wrote:
|     >Hi Jeff,
|     >I'm not sure I understand your comment. The code is part of a package.
|     >We're just trying to sync the declared object name in the R environment to
|     >the object in the Rcpp module programmatically from c++ for internal use.
|     I
|     >just wondered if there was a way to access the wrapped object in the
|     >Rcpp:::PreserveStorage class and Dirk answered that question.
|     >Thanks.
|     >Matthew
|     >
|     >
|     >On Fri, Mar 24, 2023 at 2:34 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
| >wrote:
|     >
|     >> Matthew... the normal context for sharing Rcpp code is within a package.
|     >> It is a no-brainer to have an R API called by the user that calls
|     whatever
|     >> Rcpp code it needs to, which is why your objection seems inappropriate
|     to
|     >> me.
|     >>
|     >> On March 24, 2023 10:43:34 AM PDT, Dirk Eddelbuettel <edd at debian.org>
| >> wrote:
|     >> >
| >> >On 24 March 2023 at 13:08, Matthew Supernaw - NOAA Federal wrote:
|     >> >| I'm sure it's possible to do from the R side, however, the
|     requirement
|     >> is to
|     >> >| hide this from the end user and set the reference objects name to the
|     >> declared
|     >> >| name from the R environment. If I view the environment in RStudio,
|     >> there is
|     >> >| much more information available. ls() only returns a list of objects
|     by
|     >> their
|     >> >| declared name. I'm wondering how to get more details about elements
|     in
|     >> the
|     >> >| environment.
|     >> >
|     >> >Thanks for additional color.  That may be a very interesting and
|     valuable
|     >> >question _for the C API of R_ and youe guess of "maybe Rcpp has a
|     helper"
|     >> is
|     >> >a good one.  Sadly, I fear we may not have anything for you.
|     >> >
|     >> >Dirk
|     >> >
|     >> >--
|     >> >dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
|     >> >_______________________________________________
|     >> >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
|     >> --
|     >> Sent from my phone. Please excuse my brevity.
|     >>
|     >
|     >
| 
|     --
|     Sent from my phone. Please excuse my brevity.
| 
| 
| 
| --
| Matthew Supernaw
| Scientific Software Developer
| National Oceanic and Atmospheric Administration
| Office Of Science and Technology
| NOAA Fisheries | U.S. Department of Commerce
| Phone 248 - 396 - 7797
| 
|
#
Thanks! Working offline would be great!
On Tue, Mar 28, 2023 at 11:34 AM Dirk Eddelbuettel <edd at debian.org> wrote: