Skip to content

Commit

Permalink
Merge branch 'master' into testlesson
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarchedi committed Mar 15, 2014
2 parents d96d1af + 8eaf248 commit f5caba0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
3 changes: 2 additions & 1 deletion R/author_lesson.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ author_lesson = function(lesson_name, course_name, new_course, type=NULL) {
} else {
# Read in course template and add custom YAML
message("Customizing lesson template ...\n")
path2temp <- file.path(path.package("swirl"), "templates", "lesson.Rmd")
path2temp <- file.path(system.file(package = "swirl"),
"templates", "lesson.Rmd")
temp <- readLines(path2temp, warn=FALSE)
dat <- list(lesson_name = lesson_name,
course_name = course_name,
Expand Down
58 changes: 38 additions & 20 deletions R/install_course.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ install_from_swirl <- function(course_name){
response <- GET(url)

# Construct path to Courses
path <- file.path(path.package("swirl"), "Courses", "temp.zip")
path <- file.path(system.file(package = "swirl"), "Courses", "temp.zip")

# Write the response as a zip
writeBin(content(response, "raw"), path)
Expand All @@ -29,18 +29,22 @@ install_from_swirl <- function(course_name){

# Filter list and extract
unzip_list <- Filter(function(x){grepl(course_name, x)}, file_names)
unzip(path, exdir=file.path(path.package("swirl"), "Courses"), files=unzip_list)
unzip(path, exdir=file.path(system.file(package = "swirl"), "Courses"),
files=unzip_list)

# Copy files from unzipped directory into Courses
top_dir <- file.path(path.package("swirl"), "Courses", sort(dirname(unzip_list))[1])
top_dir <- file.path(system.file(package = "swirl"), "Courses",
sort(dirname(unzip_list))[1])
dirs_to_copy <- list.files(top_dir, full.names=T)
file.copy(dirs_to_copy, file.path(path.package("swirl"), "Courses"), recursive=T)
file.copy(dirs_to_copy, file.path(system.file(package = "swirl"), "Courses"),
recursive=T)

# Delete unzipped directory
unlink(top_dir, recursive=T, force=T)

# If __MACOSX exists, delete it.
unlink(file.path(path.package("swirl"), "Courses", "__MACOSX"), recursive=T, force=T)
unlink(file.path(system.file(package = "swirl"), "Courses", "__MACOSX"),
recursive=T, force=T)

# Delete temp.zip
unlink(path, force=T)
Expand Down Expand Up @@ -88,7 +92,8 @@ zip_course <- function(path, dest=NULL){
setwd(zip_dir)

# Zip-A-Dee-Doo-Dah
zip(paste0(dest, "/", basename(path), ".zip"), list.files(getwd(), recursive=T))
zip(paste0(dest, "/", basename(path), ".zip"),
list.files(getwd(), recursive=T))

# Delete temporary directory
unlink(zip_dir, recursive=T, force=T)
Expand All @@ -110,7 +115,8 @@ zip_course <- function(path, dest=NULL){
#' uninstall_course("Linear Regression")
#' }
uninstall_course <- function(course_name){
path <- file.path(path.package("swirl"), "Courses", make_pathname(course_name))
path <- file.path(system.file(package = "swirl"), "Courses",
make_pathname(course_name))
if(file.exists(path)){
unlink(path, recursive=T, force=T)
message("Course uninstalled successfully!")
Expand All @@ -134,23 +140,28 @@ install_course_zip <- function(path, multi=FALSE){

# Filter list and extract
unzip_list <- Filter(function(x){grepl("/.+/", x)}, file_names)
unzip(path, exdir=file.path(path.package("swirl"), "Courses"), files=unzip_list)
unzip(path, exdir=file.path(system.file(package = "swirl"), "Courses"),
files=unzip_list)

# Copy files from unzipped directory into Courses
top_dir <- file.path(path.package("swirl"), "Courses", sort(dirname(unzip_list))[1])
top_dir <- file.path(system.file(package = "swirl"), "Courses",
sort(dirname(unzip_list))[1])
dirs_to_copy <- list.files(top_dir, full.names=T)
file.copy(dirs_to_copy, file.path(path.package("swirl"), "Courses"), recursive=T)
file.copy(dirs_to_copy, file.path(system.file(package = "swirl"),
"Courses"), recursive=T)

# Delete unzipped directory
unlink(top_dir, recursive=T, force=T)

} else {
# Unzip file into courses
file_list <- unzip(path, exdir=file.path(path.package("swirl"), "Courses"))
file_list <- unzip(path, exdir=file.path(system.file(package = "swirl"),
"Courses"))
}

# If __MACOSX exists, delete it.
unlink(file.path(path.package("swirl"), "Courses", "__MACOSX"), recursive=T, force=T)
unlink(file.path(system.file(package = "swirl"), "Courses", "__MACOSX"),
recursive=T, force=T)

message("Course installed successfully!")
invisible()
Expand Down Expand Up @@ -178,7 +189,8 @@ install_course_directory <- function(path){
}

# Copy files
file.copy(path, file.path(path.package("swirl"), "Courses"), recursive=T)
file.copy(path, file.path(system.file(package = "swirl"), "Courses"),
recursive=T)

message("Course installed successfully!")
invisible()
Expand All @@ -197,10 +209,12 @@ install_course_directory <- function(path){
#' install_course_github("bcaffo", "Linear_Regression")
#' install_course_github("jtleek", "Twitter_Map", "geojson")
#' }
install_course_github <- function(github_username, course_name, branch="master", multi=FALSE){
install_course_github <- function(github_username, course_name,
branch="master", multi=FALSE){

# Construct url to the zip file
zip_url <- paste0("http://github.com/", github_username, "/", course_name,"/zipball/", branch)
zip_url <- paste0("http://github.com/", github_username, "/",
course_name,"/zipball/", branch)

install_course_url(zip_url, multi=multi)
}
Expand Down Expand Up @@ -234,7 +248,8 @@ install_course_dropbox <- function(url, multi=FALSE){
#' }
install_course_google_drive <- function(url, multi=FALSE){
# Construct url to the zip file
zip_url <- sub("file/d/", "uc?export=download&id=", sub("/edit\\?usp=sharing", "", url))
zip_url <- sub("file/d/", "uc?export=download&id=",
sub("/edit\\?usp=sharing", "", url))

install_course_url(zip_url, multi=multi)
}
Expand All @@ -256,7 +271,7 @@ install_course_url <- function(url, multi=FALSE){
response <- GET(url)

# Construct path to Courses
path <- file.path(path.package("swirl"), "Courses", "temp.zip")
path <- file.path(system.file(package = "swirl"), "Courses", "temp.zip")

# Write the response as a zip
writeBin(content(response, "raw"), path)
Expand All @@ -273,11 +288,14 @@ install_course_url <- function(url, multi=FALSE){
old_name <- head( sort( file_names[which(file_names != ".")] ) , 1)

# Extract course name
course_name <- sub("/zipball", "", str_extract(url, perl("[^/]+/{1}zipball")) )
course_name <- sub("/zipball", "",
str_extract(url, perl("[^/]+/{1}zipball")) )

# Rename unzipped directory
file.rename(file.path(path.package("swirl"), "Courses", old_name),
file.path(path.package("swirl"), "Courses", course_name))
file.rename(file.path(system.file(package = "swirl"),
"Courses", old_name),
file.path(system.file(package = "swirl"),
"Courses", course_name))
}

# Delete downloaded zip
Expand Down

0 comments on commit f5caba0

Please sign in to comment.