Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

organize R code in R package #81

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

organize R code in R package #81

wants to merge 9 commits into from

Conversation

romainfrancois
Copy link
Contributor

@romainfrancois romainfrancois commented May 13, 2024

closes #32

This puts the support R code into an R package called hera instead of having volatile R code that is source() from the directory. The plan is for hera to be tested as a standalone package with typical R code tools, i.e. testthat ...

So currently, the kernel void interpreter::configure_impl() will try to load hera:

void interpreter::configure_impl()
{
    SEXP sym_library = Rf_install("library");
    SEXP chr_hera = PROTECT(Rf_mkString("hera"));
    SEXP call_library_hera = PROTECT(Rf_lang2(sym_library, chr_hera));

    SEXP sym_try = Rf_install("try");
    SEXP sym_silent = Rf_install("silent");
    SEXP call_try = PROTECT(Rf_lang3(sym_try, call_library_hera, Rf_ScalarLogical(TRUE)));
    SET_TAG(CDDR(call_try), sym_silent);

    SEXP loaded = PROTECT(Rf_eval(call_try, R_GlobalEnv));
    if (!Rf_inherits(loaded, "try-error")) {
        SEXP sym_asNamespace = Rf_install("asNamespace");
        SEXP call_as_Namespace = PROTECT(Rf_lang2(sym_asNamespace, chr_hera));

        env_hera = PROTECT(Rf_eval(call_as_Namespace, R_GlobalEnv));
        R_PreserveObject(env_hera);
        UNPROTECT(2);
    }

    UNPROTECT(4);
}

And then e.g. execute_request_impl() will call the R function execute that lives in hera:

SEXP result = invoke_hera_fn("execute", code_, execution_counter_, silent_);

Then the R code might need to call the routines made available by xeusr, e.g.

publish_stream <- function(name, text) {
  .Call("xeusr_publish_stream", name, text, PACKAGE = "(embedding)")
}

This is all similar to what was, but having it in a proper package will help.

For testing purposes, i.e. when R is not embedded, and therefore those routines are not available, we'll have to replace them with mockups.

An issue I have with this, is that e need to make sure that the 📦 is installed, so I probably need to investigate that.

@IsabelParedes
Copy link
Member

Hi @romainfrancois ! I've been testing xeus-r-lite and I ran into an issue finding the files in resources. Without the changes in this PR, it relies on Sys.which to find the location of these files. However, in r-base this is normally a call to the system's built-in which command (/usr/bin/which) and we are disabling it here for the wasm case.

I think if we had a separate hera package, it would make it straightforward to find all these resource files instead of hard-coding the paths (which is what I'm currently doing for testing). So are there still plans of merging this PR? Is it still possible to rebase/update?

Thanks!

cc: @anutosh491 in case you're interested :)

@IsabelParedes
Copy link
Member

Hi! I made some changes regarding Sys.which in #111 :)

@SylvainCorlay
Copy link
Member

I would be interested in resurecting this work on having a clean split!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework R code separation
3 participants