Skip to content

Instantly share code, notes, and snippets.

@anarosner
Last active August 29, 2015 14:07

Revisions

  1. anarosner revised this gist Nov 25, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions purl_wrapper_demo
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ setwd("C:/ALR/Models/boo") #example using local windows directory, can easily sw
    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
    # 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") )
    @@ -24,12 +24,14 @@ purl( paste0(dir,"/rmd/",i,".Rmd"), paste0(dir,"/r/",i,".R") )
    #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\")" ) #maybe you want to specify a directory before sourcing files?
    # "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)
    # 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\")"))
    }
  2. anarosner revised this gist Nov 25, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions purl_wrapper_demo
    Original file line number Diff line number Diff line change
    @@ -23,8 +23,9 @@ purl( paste0(dir,"/rmd/",i,".Rmd"), paste0(dir,"/r/",i,".R") )
    # (i.e. script1.rmd, script2.rmd, etc)
    #you can purl and write a master/wrapper script all at once, it's really quick

    wrapper <- NULL
    # wrapper <- "setwd(\"C:/ALR/Models/boo/r\")" #maybe you want to specify a directory before sourcing files?
    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") )
  3. anarosner created this gist Oct 3, 2014.
    41 changes: 41 additions & 0 deletions purl_wrapper_demo
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #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 <- NULL
    # 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