Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- get_symbols <- inline::cfunction(
- includes = "
- #define HSIZE 49157 /* The size of the hash table for symbols */
- extern SEXP* R_SymbolTable;
- ",
- body = "
- int symbol_count = 0;
- SEXP s;
- int j;
- for (j = 0; j < HSIZE; j++) {
- for (s = R_SymbolTable[j]; s != R_NilValue; s = CDR(s)) {
- if (CAR(s) != R_NilValue) {
- symbol_count++;
- }
- }
- }
- SEXP result = PROTECT(Rf_allocVector(STRSXP, symbol_count));
- symbol_count = 0;
- for (j = 0; j < HSIZE; j++) {
- for (s = R_SymbolTable[j]; s != R_NilValue; s = CDR(s)) {
- if (CAR(s) != R_NilValue) {
- SET_STRING_ELT(result, symbol_count, PRINTNAME(CAR(s)));
- symbol_count++;
- }
- }
- }
- UNPROTECT(1);
- return result;
- "
- )
- # Test it out
- get_symbols()
- # new_symbols() returns a character vector of symbols that have been added since
- # the last time it was run.
- last_symbols <- get_symbols()
- new_symbols <- function() {
- cur_symbols <- get_symbols()
- res <- setdiff(cur_symbols, last_symbols)
- last_symbols <<- cur_symbols
- res
- }
- # Example
- # The first couple times it's run, R might do something that adds symbols, like
- # load the compiler package. Run it a bunch of times until it returns
- # character(0).
- new_symbols()
- new_symbols()
- new_symbols()
- # character(0)
- # After R stops loading things, add a new symbol and test if it's detected.
- abcdefg <- 1
- new_symbols()
- #> [1] "abcdefg"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement