測試專區 - wush978/DataScienceAndR GitHub Wiki

這裡記載了我用於測試課程內容所撰寫的Script

測試安裝

以下的Script會忽略所有已經安裝的套件,而將所有套件重新安裝至./lib之下後進行測試

.libPaths(new = "lib")
file.copy(file.path(.Library, "tools"), "lib", recursive = TRUE)

unlockBinding(".Library", baseenv())
assign(".Library", "", envir = baseenv())
.libPaths(new = "lib")

install.packages(c("swirl", "curl"), repos = "http://taiwanrusergroup.github.io/R")
library(swirl)
install_course_github("wush978", "DataScienceAndR", "course")

設定Rstudio Server

建立docker instance

# on host
docker run -d -p 28787:8787 -e USER=$RSTUDIO_USER -e PASSWORD=$RSTUDIO_PASSWORD --name rstudio rocker/rstudio
docker exec rstudio apt-get install libxml2-dev -y
docker exec -it rstudio R

在RSTUDIO_USER之下設定環境...

dir.create("R")
.libPaths(new = "R")
# .libPaths()
source("http://wush978.github.io/R/init-swirl.R")
utils::install.packages(c("Rcpp", "BH"))

透過R 把環境複製到其他使用者:

library(magrittr)
rstudio_user <- "RSTUDIO_USER"
user <- c("user1", "user2")
password <- c("password1", "password2")

sprintf("/usr/sbin/useradd %s", user) %>% 
  sapply(system)

sprintf("echo %s:%s | chpasswd", user, password) %>% 
  sapply(system)

sprintf("/home/%s", user) %>%
  sapply(dir.create)

sprintf("cp -r /home/%s/R /home/%s", rstudio_user, user) %>%
  sapply(system)

sprintf("echo \".libPaths(new = '/home/%s/R')\" > /home/%s/.Rprofile", user, user) %>%
  sapply(system)

sprintf("echo \"options(repos = c(cran = 'http://cran.csie.ntu.edu.tw'))\" >> /home/%s/.Rprofile", user, user) %>%
  sapply(system)

sprintf("chown -R %s:%s /home/%s", user, user, user) %>% 
  sapply(system)


投影片開發

install.packages(c("magrittr", "devtools"))

library(magrittr)

src <- 
  dir("slide", "Rmd$", full.names = TRUE) %>%
  lapply(readLines) %>%
  lapply(grep, pattern = "library", value = TRUE) %>%
  unlist

pkgs <- 
  regexec("library\\(([^\\)]+)\\)", src) %>%
  regmatches(x = src) %>%
  sapply(`[`, 2) %>%
  grep(pattern = "^[a-zA-Z0-9\\.]+$", value = TRUE)

for(pkg in pkgs) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
  }
}


devtools::install_github("wush978/Rtwmap")
devtools::install_github("ramnathv/slidify")
devtools::install_github("wush978/slidifyLibraries")