Created
October 22, 2019 19:11
-
-
Save strboul/1cad459ce15fbee562caa949ccff0eff to your computer and use it in GitHub Desktop.
Create language elements in R at C level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Create language elements in R at C level | |
## Create class with Rf_lang | |
inline::cfunction(c(x = "numeric"), ' | |
SEXP class_call = PROTECT(Rf_lang2(R_ClassSymbol, x)); | |
UNPROTECT(1); | |
return class_call; | |
') -> class_expr | |
(e <- class_expr(1L)) | |
eval(e) | |
## Data frame creation and eval | |
## Source: https://r.789695.n4.nabble.com/question-regarding-lang2-command-in-C-tp4688070p4688072.html | |
inline::cfunction(c(age = "numeric", gender = "character"), ' | |
SEXP call = PROTECT(Rf_lang4(Rf_install("data.frame"), age, gender, Rf_ScalarLogical(FALSE))); | |
SET_TAG(CDR(call), Rf_install("age")); | |
SET_TAG(CDDR(call), Rf_install("gender")); | |
SET_TAG(CDR(CDDR(call)), Rf_install("stringsAsFactors")); | |
SEXP evald_df = PROTECT(Rf_eval(call, R_GlobalEnv)); | |
UNPROTECT(2); | |
return evald_df; | |
') -> make_df | |
(df <- make_df(c(21, 22), c("male", "female"))) | |
str(df) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment