Skip to content

Instantly share code, notes, and snippets.

@anarosner
Last active August 29, 2015 14:07
Show Gist options
  • Save anarosner/a45f1d04716f0bfd0d92 to your computer and use it in GitHub Desktop.
Save anarosner/a45f1d04716f0bfd0d92 to your computer and use it in GitHub Desktop.
purl and wrapper script demo
#purling is in the knitr package
library(knitr)
setwd("C:/ALR/Models/boo") #example using local windows directory, can easily switch
#it can be really simple
purl( "script1.Rmd", "script1.R" )
#just specify the rmd filename, then r filename, with extensions
# you could also specify a directory here (use paste instead of file.path so you can add/change extensions)
# I like to keep separate directories for r and rmd, but you don't need to
dir <- "C:/ALR/Models/boo"
i<-"script2"
purl( paste0(dir,"/rmd/",i,".Rmd"), paste0(dir,"/r/",i,".R") )
#if you have multiple scripts you want to run in sequence,
# (i.e. script1.rmd, script2.rmd, etc)
#you can purl and write a master/wrapper script all at once, it's really quick
wrapper <- "#!/usr/bin/env Rscript"
# maybe you want the wrapper script to specify a directory before sourcing files?
# wrapper <- rbind( wrapper,
# "setwd(\"C:/ALR/Models/boo/r\")" )
for (i in c("script1","script2", "script3") ) {
purl( paste0(dir,"/rmd/",i,".Rmd"), paste0(dir,"/r/",i,".R") )
# or simpler version w/o directory info, replace above line with:
# purl( paste0(i,".Rmd"), paste0(i,".R") )
wrapper <-rbind(wrapper, paste0("source(\"",i,".R\")"))
}
cat(wrapper)
writeLines( text=wrapper, con=file.path(dir,"R","wrapper_script.R" ) )
#now you have a single r script, that can be called/spawned by the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment