On 27/04/2021 12:34 p.m., Cai Li wrote:
/Dear Duncan, Thank you very much for your helpful advice! In addition, could you kindly help answer my follow-up questions below? /

Sorry, I showed you how I did it. There are certainly other ways, but you'll have to dig around in the manuals to find them.

Duncan Murdoch

/
/
/Instead of passing the private R package environment to C, is there a way to alter the namespace so that files in the "R" directory of the package can be read by the C code but not exported to users? There are specialized export and import functions in the "roxygen2" package that can be used to alter the namespace, and I'm wondering whether altering the namespace would work as well (https://cran.r-project.org/web/packages/roxygen2/vignettes/namespace.html <https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcran.r-project.org%2Fweb%2Fpackages%2Froxygen2%2Fvignettes%2Fnamespace.html&data=04%7C01%7Ccai.li%40yale.edu%7Cfef92ea6326d4803577808d909969553%7Cdd8cbebb21394df8b4114e3e87abeb5c%7C0%7C0%7C637551364617695111%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=PKtQPhcpncRWSzSY5FrYR2Pcbn6LaYDDX0GhEgVtLtA%3D&reserved=0>)./
/
/
/However, if passing the private R package environment to C is the only/best way for the R functions to be read by C without getting exported, could you kindly offer additional advice for how to do this? I can pass an "environment" object from R to C within an SEXP object in C. However, I'm not sure how to pass the "environment" object between C functions. For instance, in the following function, could I declare a "char" object and save the "environment" object as a "char" within the C code?/
/
/
/void
maketree_diff(int n,
               int nc,
               double *y,
               double **data,
               int *types,
               int *ncat,
               int *treat,
               struct node *tree,
               int nmin2,
               int maxdepth,
               int method)/

On Mon, Apr 26, 2021 at 5:30 AM Duncan Murdoch <murdoch.dun...@gmail.com <mailto:murdoch.dun...@gmail.com>> wrote:

    On 25/04/2021 10:30 p.m., Cai Li wrote:
     > Thanks Duncan! I was advised not to export those functions in the
     > namespace and keep them as internals. In terms of this, could you
    advise
     > what specific changes should I make to the C code so that I can
    evaluate
     > them in a suitable environment? I tried something like
     > "PROTECT(func_R=lang2(install("Package:::R_function"),
    func_data))," but
     > it did not work. Sorry this may be too naive and thanks again.

    That line creates the expression to evaluate, it doesn't evaluate it.
    You need to call eval() (the C function) on the result.  Here's some
    code from rgl that does something similar:

    R code to initialize:

    rgl.init <- function(initValue = 0, onlyNULL = FALSE, debug =
    getOption("rgl.debug", FALSE))
        .Call( rgl_init,
          initValue, onlyNULL, environment(rgl.init), debug )

    The important thing here is "environment(rgl.init)", which is the
    private environment within the package.

    Then in C code, save that environment:

        rglNamespace = in_namespace;

    and use it later:

        result = eval(PROTECT(lang2(PROTECT(install("rglFonts")),

    PROTECT(ScalarString(mkChar(family))))), rglNamespace);

    Duncan Murdoch

     >
     > Cai
     >
     > On Sun, Apr 25, 2021 at 8:46 PM Duncan Murdoch
    <murdoch.dun...@gmail.com <mailto:murdoch.dun...@gmail.com>
     > <mailto:murdoch.dun...@gmail.com
    <mailto:murdoch.dun...@gmail.com>>> wrote:
     >
     >     On 25/04/2021 7:23 p.m., Cai Li wrote:
     >      > Hello All,
     >      >
     >      > I'm developing a package with C code. My question may be
    naive: I
     >     have
     >      > several internal R functions that need to be called by C,
    but I
     >     cannot get
     >      > it to work or find a solution. It can work if I export
    those internal
     >      > functions in namespace, but it fails if I just want to
    keep them as
     >      > internal functions without exporting them. I guess I need some
     >     flags to
     >      > indicate the "internal" R internal functions? Could you please
     >     share your
     >      > thoughts on how to fix this? Much appreciated in advance!
     >      >
     >      > //   set up call to R function "R_function"
     >      >      SEXP func_R;
     >      >      PROTECT(func_R=lang2(install("R_function"), func_data));
     >      >
     >
     >     It all depends on the environment where you evaluate that
    expression,
     >     just as it would if it was R code.  You can't see internal
    package
     >     functions from the global environment, you need to evaluate
    them in the
     >     package namespace environment.
     >
     >     Duncan Murdoch
     >


______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to