Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. get_symbols <- inline::cfunction(
  2. includes = "
  3. #define HSIZE 49157 /* The size of the hash table for symbols */
  4. extern SEXP* R_SymbolTable;
  5. ",
  6. body = "
  7. int symbol_count = 0;
  8. SEXP s;
  9. int j;
  10. for (j = 0; j < HSIZE; j++) {
  11. for (s = R_SymbolTable[j]; s != R_NilValue; s = CDR(s)) {
  12. if (CAR(s) != R_NilValue) {
  13. symbol_count++;
  14. }
  15. }
  16. }
  17.  
  18.  
  19. SEXP result = PROTECT(Rf_allocVector(STRSXP, symbol_count));
  20. symbol_count = 0;
  21. for (j = 0; j < HSIZE; j++) {
  22. for (s = R_SymbolTable[j]; s != R_NilValue; s = CDR(s)) {
  23. if (CAR(s) != R_NilValue) {
  24. SET_STRING_ELT(result, symbol_count, PRINTNAME(CAR(s)));
  25. symbol_count++;
  26. }
  27. }
  28. }
  29.  
  30. UNPROTECT(1);
  31. return result;
  32. "
  33. )
  34.  
  35. # Test it out
  36. get_symbols()
  37.  
  38.  
  39.  
  40. # new_symbols() returns a character vector of symbols that have been added since
  41. # the last time it was run.
  42. last_symbols <- get_symbols()
  43. new_symbols <- function() {
  44. cur_symbols <- get_symbols()
  45. res <- setdiff(cur_symbols, last_symbols)
  46. last_symbols <<- cur_symbols
  47. res
  48. }
  49.  
  50. # Example
  51.  
  52. # The first couple times it's run, R might do something that adds symbols, like
  53. # load the compiler package. Run it a bunch of times until it returns
  54. # character(0).
  55. new_symbols()
  56. new_symbols()
  57. new_symbols()
  58. # character(0)
  59.  
  60.  
  61. # After R stops loading things, add a new symbol and test if it's detected.
  62. abcdefg <- 1
  63. new_symbols()
  64. #> [1] "abcdefg"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement