In [1]:
dir.create('/data/example-project', showWarnings = FALSE)
setwd('/data/example-project')
packrat::init()
In [2]:
setwd('/data/example-project')
packrat::on()
In [3]:
.libPaths() # Should only container folders within the example folder
In [4]:
find.package('fortunes', quiet = TRUE) # Expected to return nothing if package is not installed
In [5]:
install.packages('fortunes')
In [6]:
find.package('fortunes') # should be a sub-folder of the './packrat/lib' wihtin the project
In [7]:
packrat::snapshot() # Add fortune to the packrat snapshot
In [8]:
unlink(paste0(.libPaths()[1], '/fortunes'), recursive = TRUE) # Delete fortunes lib folder
In [9]:
find.package('fortunes', quiet = TRUE) # Package is missing
In [10]:
packrat::restore() # Restores the missing package
In [11]:
find.package('fortunes', quiet = TRUE)
In [12]:
# Spark preamble
library(SparkR, lib.loc=file.path(Sys.getenv('SPARK_HOME'), 'R', 'lib'))
In [13]:
sparkFunct <- function(idx) {
# Open project
setwd('/data/example-project')
packrat::on(clean.search.path = FALSE)
# Install if needed
packrat::restore()
# Run code on cluster
library(fortunes)
return(fortune())
}
In [14]:
# Create Spark context
sparkR.session(master=Sys.getenv('MASTER'))
# Run function on cluster
output <- spark.lapply(seq(4), sparkFunct)
# Delete Spark context
sparkR.session.stop()
In [15]:
lapply(output, function(item) { item$author })
In [16]:
setwd('~')
unlink('/data/example-project', recursive = TRUE)
In [ ]: