Last active
December 31, 2016 16:47
-
-
Save yamano357/7e03abb72cb823bbe14bdae0aedd8615 to your computer and use it in GitHub Desktop.
This file contains 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
# Githubに個人的に作られたTask Viewにまとめられたパッケージをインストール | |
# https://github.com/benmarwick/ctv-archaeology | |
# 関数定義部 ----------------------------------------------------------------------- | |
# 必要パッケージの設定 | |
SET_USE_PACKAGES <- c("dplyr", "rvest", "devtools", "stringr") | |
# インストールに必要な設定 | |
SET_CUSTOM_CTV <- list( | |
CRAN = "http://cran.rstudio.com", | |
URL = "https://github.com/benmarwick/ctv-archaeology/blob/master/README.md", | |
PACKAGE_XPATH = "//*[@id='readme']/article//li//a[@href and contains(@href, 'http://cran.rstudio.com') and contains(@href, 'packages')]", | |
GITHUB_XPATH = "//*[@id='readme']/article//li/a[@href and contains(@href, 'github.com') and string-length(@class ) = 0]" | |
) | |
# パッケージ読み込み ----------------------------------------------------------------------- | |
load_pack <- sapply( | |
X = SET_USE_PACKAGES, | |
FUN = library, character.only = TRUE, logical.return = TRUE | |
) | |
# 処理に必要なパッケージがなければインストール | |
if (any(load_pack)) { | |
install.packages( | |
pkgs = SET_USE_PACKAGES[!load_pack], | |
repo = SET_CUSTOM_CTV$CRAN, | |
type = "source" | |
) | |
} | |
# 実行部 ----------------------------------------------------------------------- | |
# 記載のあるパッケージのうち、CRANとGitHubに分けてインストールする | |
if (all(load_pack)) { | |
# READMEのMarkdownをHTMLとして得る | |
view_md <- rvest::html(x = SET_CUSTOM_CTV$URL) | |
# Markdown中に記載があるCRANパッケージ名を取得 | |
custom_packages <- view_md %>% | |
rvest::html_nodes(xpath = SET_CUSTOM_CTV$PACKAGE_XPATH) %>% | |
rvest::html_text(x = .) %>% | |
unique() | |
# CRAN上のパッケージをインストール | |
cond_inst_cran_packages <- setdiff( | |
x = custom_packages, | |
y = rownames(x = installed.packages()) | |
) | |
install.packages( | |
pkgs = cond_inst_cran_packages, | |
repo = SET_CUSTOM_CTV$CRAN, | |
type = "source" | |
) | |
# 確認 | |
installed_cran_packages <- setdiff( | |
x = cond_inst_cran_packages, | |
y = rownames(x = installed.packages()) | |
) %>% | |
# Markdown中に記載があるGitHub上のパッケージ名を取得 | |
custom_git_packages <- dplyr::data_frame( | |
github = view_md %>% | |
rvest::html_nodes(xpath = SET_CUSTOM_CTV$GITHUB_XPATH) %>% | |
rvest::html_attr(name = "href") | |
) %>% | |
dplyr::mutate( | |
user_name = stringr::str_match( | |
string = .$github, pattern = "github.com\\/(.*?)\\/(.*)" | |
)[, 2], | |
pkgs = stringr::str_match( | |
string = .$github, pattern = "github.com\\/(.*?)\\/(.*)" | |
)[, 3] | |
) | |
# GitHub上のパッケージをインストール | |
cond_inst_github_packages <- setdiff( | |
x = custom_git_packages$pkgs, | |
y = rownames(x = installed.packages()) | |
) | |
# 事前にインストールが必要なGitHubパッケージ | |
devtools::install_github("rsheets/linen") | |
devtools::install_github("ropensci/tabulizerjars") | |
devtools::install_github("gaborcsardi/readline") | |
devtools::install_github("gaborcsardi/falsy") | |
devtools::install_github("thomasp85/ggforce") | |
custom_git_packages %>% | |
dplyr::filter(is.element(set = cond_inst_github_packages, el = .$pkgs)) %>% | |
dplyr::filter( | |
!is.element( | |
set = c( | |
"ctv-archaeology", "googlesheet", "bayesian_first_aid", | |
"CAinterprTools", "CAseriation", "shapeR", "rrrpkg", "gRAN", "rocker" | |
), | |
el = .$pkgs | |
) & | |
!stringr::str_detect(string = .$pkgs, pattern = "/$") | |
) %>% | |
dplyr::rowwise(data = .) %>% | |
dplyr::do( | |
devtools::install_github(repo = stringr::str_c(.$user_name, .$pkgs, sep = "/"), force = TRUE) | |
) | |
# 確認 | |
installed_cran_packages <- setdiff( | |
x = cond_inst_github_packages, | |
y = rownames(x = installed.packages()) | |
) %>% | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment