Last active
August 29, 2015 14:07
-
-
Save anarosner/a45f1d04716f0bfd0d92 to your computer and use it in GitHub Desktop.
purl and wrapper script demo
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
#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 extensions) | |
#it's nice to keep r and rmd separate | |
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" | |
# wrapper <- rbind( wrapper, | |
"setwd(\"C:/ALR/Models/boo/r\")" ) #maybe you want to specify a directory before sourcing files? | |
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) | |
# 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