Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Forked from wch/shim.R
Created March 8, 2016 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsievert/386b3eb041a08ae72020 to your computer and use it in GitHub Desktop.
Save cpsievert/386b3eb041a08ae72020 to your computer and use it in GitHub Desktop.
Shim system.file for htmlwidgets so that a dependent package can be used with devtools::load_all()
library(inline)
inc <- '
/* This is taken from envir.c in the R 2.15.1 source
https://github.com/SurajGupta/r-source/blob/master/src/main/envir.c
*/
#define FRAME_LOCK_MASK (1<<14)
#define FRAME_IS_LOCKED(e) (ENVFLAGS(e) & FRAME_LOCK_MASK)
#define UNLOCK_FRAME(e) SET_ENVFLAGS(e, ENVFLAGS(e) & (~ FRAME_LOCK_MASK))
'
src <- '
if (TYPEOF(env) == NILSXP)
error("use of NULL environment is defunct");
if (TYPEOF(env) != ENVSXP)
error("not an environment");
UNLOCK_FRAME(env);
// Return TRUE if unlocked; FALSE otherwise
SEXP result = PROTECT( Rf_allocVector(LGLSXP, 1) );
LOGICAL(result)[0] = FRAME_IS_LOCKED(env) == 0;
UNPROTECT(1);
return result;
'
unlockEnvironment <- cfunction(signature(env = "environment"),
includes = inc,
body = src)
imports <- parent.env(asNamespace("htmlwidgets"))
unlockEnvironment(imports)
imports$system.file <- devtools:::shim_system.file
# After the code above has been run, you can load an in-development package
# that uses htmlwidgets (like dygraphs or leaflet). After being loaded this way,
# When the JS or CSS resources of the package are edited, they'll immediately
# be available, without having to build and install the package.
load_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment