步骤 1:- 在 PC 上安装 R base(64 位)和 R Studio。
步骤 2:- 插入 Pendrive 或 PC 上保存离线包文件夹的位置。
第三步:-。以编辑模式打开 R studio(R 脚本)。
getDependencies <- function(packs){
dependencyNames <- unlist(
tools::package_dependencies(packages = packs, db = available.packages(),
which = c("Depends", "Imports"),
recursive = TRUE))
packageNames <- union(packs, dependencyNames)
# Remove base dependencies, these are installed with R and not published on CRAN
basePackages <- c("base","compiler","datasets","graphics","grDevices","grid",
"methods","parallel","splines","stats","stats4","tcltk","tools","utils")
packageNames <- setdiff(packageNames, basePackages)
packageNames
}
packages <- getDependencies(c("tidyverse", "pacman"))
setwd("E:/offline package R installation")
pkgInfo <- download.packages(pkgs = packages, destdir = getwd(), type = "win.binary")
# Save just the package file names (basename() strips off the full paths leaving just the filename)
write.csv(file = "pkgFilenames.csv", basename(pkgInfo[, 2]), row.names = FALSE)
Step5:在excel表格中获取所需包以及文件夹中的包后,将其存储在您想要的任何地方。之后在任何其他计算机上打开 R 编辑器。并执行以下代码
# Set working directory to the location of the package files
setwd("E:/offline package R installation")
# Read the package filenames and install
pkgFilenames <- read.csv("pkgFilenames.csv", stringsAsFactors = FALSE)[, 1]
install.packages(pkgFilenames, repos = NULL, type = "win.binary")
现在,您终于可以在任何计算机上轻松使用离线包及其 deoendancies 而无需任何互联网连接