ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [R] 오프라인 환경을 위한 R 라이브러리
    Data Analysis/R 2022. 2. 28. 17:19

    Table of Contents

    1. Introduction
    2. 오프라인 환경에서 R 라이브러리 설치를 위한 함수 이해하기
    3. 사용자 함수를 이용하여 처리하기
    4. R 라이브러리 설치하기
    5. Introduction

     

    Introduction

    기업 내부에 존재하는 데이터의 경우에는 보안을 민감하게 여기고 있기 때문에 외부망과 단절되어 있다. 이런 폐쇄망에서 시스템이 구성되어 있으며, 데이터가 저장되어 있는 데이터베이스가 내부망에 구성되어 있기 때문에 일반적으로 인터넷이 자유롭게 사용되는 환경에서처럼 R스튜디오를 운영할 수 없다. 이런 경우 외부에서 장비를 반입하기 전에 미리 분석 환경을 세팅하고 들어가거나 설치 파일을 USB 또는 CD에 담아서 들어가기도 한다.

     

    오프라인 환경에서 R 라이브러리 설치를 위한 함수 이해하기

    R과 R스튜디오의 경우 설치파일을 반입하여 세팅하기 쉬우나, 필요한 분석 라이브러리는 의존이 있는 라이브러리를 일일이 다 체크해야만 설치가 된다. 하나씩 검색해서 라이브러리의 의존도를 파악하는 것은 매우 비효율적이다. 이때, 우리는 tools 라이브러리의 package_dependencies() 함수를 활용하여 입력한 라이브러리를 설치하는데 필요한 다른 라이브러리의 의존성을 체크하고 의존성이 있는 모든 라이브러리 리스트를 출력할 수 있다.

    > tools::package_dependencies("ggplot2")
    $ggplot2
     [1] "digest"    "glue"      "grDevices" "grid"      "gtable"    "isoband"   "MASS"      "mgcv"     
     [9] "rlang"     "scales"    "stats"     "tibble"    "withr"

     

    의존성을 검사하는 과정에서 recursive 옵션을 TRUE로 변경하면, 서로간의 모든 의존성 검사 전체를 수행하게 된다. 옵션 없이 출력했을 때는 13개의 라이브러리를 출력한데 반해, 옵션을 입력하고 출력한 경우에는 37개의 라이브러리가 나타나는 것을 확인할 수 있다.

    tools::package_dependencies(packages = "ggplot2",
    +                           db = available.packages(),
    +                           which = c("Depends", "Imports", "LinkingTo"),
    +                           recursive = T)
    $ggplot2
     [1] "digest"       "glue"         "grDevices"    "grid"        
     [5] "gtable"       "isoband"      "MASS"         "mgcv"        
     [9] "rlang"        "scales"       "stats"        "tibble"      
    [13] "withr"        "utils"        "methods"      "graphics"    
    [17] "nlme"         "Matrix"       "splines"      "farver"      
    [21] "labeling"     "lifecycle"    "munsell"      "R6"          
    [25] "RColorBrewer" "viridisLite"  "ellipsis"     "fansi"       
    [29] "magrittr"     "pillar"       "pkgconfig"    "vctrs"       
    [33] "lattice"      "colorspace"   "cli"          "crayon"      
    [37] "utf8"

     

    의존성이 파악된 패키지는 download.packages() 함수를 이용해서 특정 위치에 라이브러리 설치 파일을 다운로드 할 수 있다.

    > download.packages(pkgs = "ggplot2",
    +                   destdir = "c:/Project",
    +                   type = "win.binary")
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/ggplot2_3.3.5.zip'을 시도합니다
    Content type 'application/zip' length 4126746 bytes (3.9 MB)
    downloaded 3.9 MB
    
         [,1]      [,2]                                          
    [1,] "ggplot2" "c:/Project/ggplot2_3.3.5.zip"

     type 옵션에 실제 설치해서 사용할 환경의 os에 맞춰 다운로드 할 수 있다.

    구분 Type 설명
    1 both binary 파일이 있는 경우 binary 파일을 다운로드하며, 없는 경우 source 파일로 다운로드
    2 source 컴파일 안된 소스 파일
    3 mac.binary MAC용 binary
    4 win.binary 윈도우용 binary

     

    사용자 함수를 이용하여 처리하기

    의존성을 확인하는 패키지는 list 형식으로 출력되기 때문에 라이브러리를 다운받기 위한 함수를 활용이 제한된다. 이를 변형 시키기 위한 함수를 생성하여 활용할 수 있다.

    pkgDep = function(pkg){
      pkgList = unlist(
        tools::package_dependencies(packages = pkg,
                                    db = available.packages(), 
                                    which = c("Depends", "Imports", "LinkingTo"),
                                    recursive = TRUE))
      pkgList = union(pkg, pkgList)
      return(pkgList)
    }
    
    > pkgDep("ggplot2")
     [1] "ggplot2"      "digest"       "glue"         "grDevices"   
     [5] "grid"         "gtable"       "isoband"      "MASS"        
     [9] "mgcv"         "rlang"        "scales"       "stats"       
    [13] "tibble"       "withr"        "utils"        "methods"     
    [17] "graphics"     "nlme"         "Matrix"       "splines"     
    [21] "farver"       "labeling"     "lifecycle"    "munsell"     
    [25] "R6"           "RColorBrewer" "viridisLite"  "ellipsis"    
    [29] "fansi"        "magrittr"     "pillar"       "pkgconfig"   
    [33] "vctrs"        "lattice"      "colorspace"   "cli"         
    [37] "crayon"       "utf8"

     

    형식 처리가 된 함수를 이용하여 라이브러리 다운로드한다.

    > pkgInfo = download.packages(pkgs = pkgDep("ggplot2"),
    +                             destdir = "c:/Project/pkg",
    +                             type = "win.binary")
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/ggplot2_3.3.5.zip'을 시도합니다
    Content type 'application/zip' length 4126746 bytes (3.9 MB)
    downloaded 3.9 MB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/digest_0.6.29.zip'을 시도합니다
    Content type 'application/zip' length 266372 bytes (260 KB)
    downloaded 260 KB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/vctrs_0.3.8.zip'을 시도합니다
    Content type 'application/zip' length 1252452 bytes (1.2 MB)
    downloaded 1.2 MB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/lattice_0.20-45.zip'을 시도합니다
    Content type 'application/zip' length 1183904 bytes (1.1 MB)
    downloaded 1.1 MB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/colorspace_2.0-3.zip'을 시도합니다
    Content type 'application/zip' length 2654221 bytes (2.5 MB)
    downloaded 2.5 MB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/cli_3.2.0.zip'을 시도합니다
    Content type 'application/zip' length 1258082 bytes (1.2 MB)
    downloaded 1.2 MB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/crayon_1.5.0.zip'을 시도합니다
    Content type 'application/zip' length 159581 bytes (155 KB)
    downloaded 155 KB
    
    URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/utf8_1.2.2.zip'을 시도합니다
    Content type 'application/zip' length 209837 bytes (204 KB)
    downloaded 204 KB

    패키지 리스트

     

    라이브러리 리스트를 csv 파일로 저장한다.

    > write.csv(basename(pkgInfo[,2]),
    +           file = "c:/Project/pkg/pkg.csv",
    +           row.names = FALSE)

     

    R 라이브러리 설치하기

    저장된 라이브러리의 위치를 기본 위치로 지정하고 install.packages() 함수를 이용하여 설치를 진행한다. 이때, repos 옵션에 NULL을 입력하면 로컬에 다운로드한 라이브러리 설치 파일을 참조하여 설치를 진행한다.

    setwd("c:/Project/pkg")
    pkgFilenames <- read.csv("pkgFilenames.csv", stringsAsFactors = FALSE)[, 1]
    install.packages(pkgFilenames, repos = NULL, type = "win.binary")

     

    R 라이브러리 불러오기

    설치한 라이브러리를 실행시킬때 library(패키지명)을 입력한다. ggplot2 라이브러를 불러올 때 다음과 같은 코드를 실행시키면 된다.

    library(ggplot2)

     

    댓글

Designed by Tistory.