在 UBUNTU 中安装 R 包

在 UBUNTU 中安装 R 包

我有一个如下所示的 R 脚本:

install.packages("R.utils")
library("R.utils")
install.packages("vcfR")
library("vcfR")
install.packages("stringr")
library("stringr")
install.packages("tidyverse")
library("tidyverse")
install.packages("dplyr")
library("dplyr")
gunzip("gnomad.exomes.r2.1.1.sites.21.vcf.bgz", "gnomad.exomes.r2.1.1.sites.21.vcf")
vc=read.vcfR("gnomad.exomes.r2.1.1.sites.21.vcf")
df=vc@fix
data=as.data.frame(df)
data_snp=data %>%
  filter(str_length(ALT)==1 & str_length(REF)==1)#filtering for SNPs
data_snp$snp_id <- str_c("chr21","-", data_snp$POS)
data_snp$AF_total=str_extract(data_snp$INFO, "(?<=AF=)[^;]+")
data_snp$AF_latin=str_extract(data_snp$INFO, "(?<=AF_amr=)[^;]+")
data_snp$INFO=NULL
data_snp$key=str_c(data_snp$snp_id,"-",data_snp$ALT,"-",data_snp$REF)
df_for_filter=read.csv("merged_df.csv")
df_x=subset(df_for_filter,df_for_filter$CHROM=="chr21")
df_x$snp_id=str_c("chr21","-",df_x$POS)
df_x$key=str_c(df_x$snp_id,"-",df_x$ALT,"-",df_x$REF)
filter_df=data_snp %>% semi_join(df_x, by = "key")
results=read.csv("result_armitage_test.csv")
results$chrom=substr(results$snp_id,1,5)
results_y=subset(results,results$chrom=="chr21")
resuts_21=merge(x = results_y, y =filter_df, by = "snp_id", all.x = TRUE)
resuts_21$CHROM=NULL
resuts_21$POS=NULL
resuts_21$CHROM=NULL
resuts_21$FILTER=NULL
resuts_y$QUAL=NULL
resuts_21$key=NULL
write.csv(resuts_21,"result_chr21.csv")

我在 UBUNTU 中运行这个脚本:

xxxxx@xxxx:~% Rscrips chr_21.R

但它返回一个错误:

CORRECT>Rscript chr_21.R (y|n|e|a)? yes
Installing package into ‘/usr/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("R.utils") :
  'lib = "/usr/lib/R/site-library"' is not writable
Error in install.packages("R.utils") : unable to install packages
Execution halted

我不明白为什么无法安装该库以及如何修复它。谢谢:)

答案1

如果您在大学集群上工作,您可能需要要求系统管理员在整个站点范围内安装软件包 - 或者配置 R 将软件包安装到本地(用户)库位置。

如果您尚未设置本地库,那么最简单的方法似乎是R交互方式执行,尝试安装包,并在站点范围安装失败时按照说明进行操作。

首先,启动R并检查当前的库搜索路径:

> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[3] "/usr/lib/R/library"

好的,现在让我们尝试安装该stringr包:

> install.packages("stringr")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("stringr") :
  'lib = "/usr/local/lib/R/site-library"' is not writable

(失败,然后提示)

Would you like to use a personal library instead? (yes/No/cancel) yes
Would you like to create a personal library
‘/home/steeldriver/R/x86_64-pc-linux-gnu-library/4.2’ to install packages into? (yes/No/cancel) yes
also installing the dependencies ‘glue’, ‘magrittr’, ‘stringi’

(一段时间后)

* DONE (stringr)

The downloaded source packages are in
        ‘/tmp/RtmpsVbbe1/downloaded_packages’
> 

现在可以检查新创建的 libPath 是否已添加:

> 
> .libPaths()
[1] "/home/steeldriver/R/x86_64-pc-linux-gnu-library/4.2"
[2] "/usr/local/lib/R/site-library"                      
[3] "/usr/lib/R/site-library"                            
[4] "/usr/lib/R/library"

值得一提的是,默认用户库位置似乎来自 /etc/R/ 环境文件中的模板:

$ grep R_LIBS_USER /etc/R/*
/etc/R/Renviron:R_LIBS_USER=${R_LIBS_USER:-'%U'}
/etc/R/Renviron.site:#R_LIBS_USER=${R_LIBS_USER-'~/R/@R_PLATFORM@-library/@MAJ_MIN_VERSION@'}

一旦你完成了包,您的脚本应该运行并将其余部分安装到您的新用户库中。

相关内容