한 번에 여러 패키지로드
require 명령을 반복해서 입력하지 않고 여러 패키지를 한 번에로드하려면 어떻게해야합니까? 나는 충돌과 화상의 세 가지 접근 방식을 모두 시도했습니다.
기본적으로 패키지 이름의 벡터를로드 할 함수에 제공하고 싶습니다.
x<-c("plyr", "psych", "tm")
require(x)
lapply(x, require)
do.call("require", x)
제안 된 함수의 여러 순열은 작동하지만 character.only
인수를로 지정하는 경우에만 작동 합니다 TRUE
. 빠른 예 :
lapply(x, require, character.only = TRUE)
내가 유지하는 CRAN 패키지 팩맨 (Dason Kurkiewicz와 함께 작성)은 다음을 수행 할 수 있습니다.
따라서 사용자는 다음을 수행 할 수 있습니다.
## install.packages("pacman")
pacman::p_load(dplyr, psych, tm)
패키지가 없으면 p_load
CRAN 또는 Bioconductor에서 다운로드합니다.
트릭을 수행해야합니다.
lapply(x, FUN = function(X) {
do.call("require", list(X))
})
(핵심 요소는 args
인수 가 하나의 요소 만있는 경우에도 목록 에 do.call(what, args)
있어야한다는 것입니다!)
패키지를 동시에 설치하고로드하려는 사람을 위해 아래 링크 에서이 기능을 발견했습니다 .https : //gist.github.com/stevenworthington/3178163
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("ggplot2", "plyr", "reshape2", "RColorBrewer", "scales", "grid")
ipak(packages)
다른 옵션은 패키지에서 제공됩니다 easypackages
. 일단 설치되면 가장 직관적 인 방법으로 패키지를로드 할 수 있습니다.
libraries("plyr", "psych", "tm")
패키지에는 여러 패키지를 설치하는 기능도 포함되어 있습니다.
packages("plyr", "psych", "tm")
lubripack 패키지를 사용하면 새 패키지를 깔끔하게 설치 한 다음 모든 패키지를 한 줄에로드 할 수 있습니다.
lubripack("plyr", "psych", "tm")
다음은 RStudio에서 위의 코드를 실행 한 후의 출력입니다.
패키지 설치 방법 :
아래 코드를 실행하여 패키지를 다운로드하고 GitHub에서 설치하십시오. GitHub 계정이 없어도됩니다.
library(devtools)
install_github("espanta/lubripack")
daroczig의 솔루션을 기반으로 목록을 입력으로 지정하지 않으려면 사용할 수 있습니다
# Foo
mLoad <- function(...) {
sapply(sapply(match.call(), as.character)[-1], require, character.only = TRUE)
}
# Example
mLoad(plyr, dplyr, data.table)
~보다 짧은 ~
lapply(list('plyr', 'dplyr', 'data.table'), require, character.only = TRUE)
I use the following function:
mrip <- function(..., install = TRUE){
reqFun <- function(pack) {
if(!suppressWarnings(suppressMessages(require(pack, character.only = TRUE)))) {
message(paste0("unable to load package ", pack,
": attempting to download & then load"))
install.packages(pack)
require(pack, character.only = TRUE)
}
}
lapply(..., reqFun)
}
This tries to load, and if it fails installs and then try to load again.
I think the code that @daroczig has provided can be improved by replacing the require
with library
and wrapping the lapply
call inside the invisible()
function. So, the improved code will look like the following:
invisible(lapply(x, library, character.only = TRUE))
This code is improved because:
library()
is generally preferred overrequire()
for loading packages because the former gives an error if the package is not installed while the latter just gives a warning. Moreover,require()
callslibrary()
, so why not just uselibrary()
directly!library("time") # Error in library("time") : there is no package called ‘time’ require("time") # Loading required package: time # Warning message: # In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : # there is no package called ‘time’
lapply()
이 경우 호출에 의해 리턴되고 인쇄 된 목록 오브젝트는 의미가 없으므로 출력을 보이지 않게하는 것이 좋습니다. 분석 작업에 R Notebook을 사용한다고 가정하면이invisible()
기능을 사용 하면 목록 개체의 내용이 표시되지 않고 렌더링 된 노트북 파일의 혼란을 막을 수 있습니다.
pacman 설치 및로드 확인을 추가하는 Tyler Rinker의 답변의 약간의 수정 :
#Install/load pacman
if(!require(pacman)){install.packages("pacman");require(pacman)}
#Install/load tons of packages
p_load(plyr,psych,tm)
나는 인용을 피하기 때문에 p_load 솔루션을 좋아합니다!
참고 URL : https://stackoverflow.com/questions/8175912/load-multiple-packages-at-once
'development' 카테고리의 다른 글
Google 서비스 3.0.0에 api_key / 현재 키가 없습니다. (0) | 2020.06.06 |
---|---|
ImportError : 'Tkinter'라는 모듈이 없습니다. (0) | 2020.06.06 |
모두 버리고 최신 개정판을 깨끗하게 받으시겠습니까? (0) | 2020.06.06 |
새로운 Android Studio의 APK 위치 (0) | 2020.06.06 |
파이썬 로깅의 시간 형식을 사용자 정의하는 방법은 무엇입니까? (0) | 2020.06.06 |