更改 R 库的位置会引发错误 [英] Change of location of R libraries throws error

查看:77
本文介绍了更改 R 库的位置会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 的库路径有疑问.也许答案已经在那里了,但我找不到它.我将 R 与 RStudio 版本 1.1.456 一起使用.我所有的附加库过去都在目录 dir1 <- \\SHARE\TOOLS_OLD$\R-3.5.1 中,我附加了 .libPaths(dir1).在此设置中,一切都按预期进行.

I have an issue with the library path of R. Maybe the answer is out there already, but I was not able to find it. I am using R together with RStudio Version 1.1.456. All my additional libraries used to be in a directory dir1 <- \\SHARE\TOOLS_OLD$\R-3.5.1 which I append with .libPaths(dir1). In this setup, everything works as expected.

问题发生在我将目录 \\SHARE\TOOLS_OLD$\R-3.5.1 中的所有库复制到新位置 \\NAS\TOOLSNEW$\R-3.5.1.我没有更新 R 版本,我想保持所有软件包版本相同,这样我的设置与以前完全相同.这就是我没有在新位置重新安装软件包的原因.

The problem occured after I copied all the libraries in directory \\SHARE\TOOLS_OLD$\R-3.5.1 to a new location \\NAS\TOOLSNEW$\R-3.5.1. I did not update the R version and I want to keep all the package versions the same such that I have exactly the same setup as before. That is why I did not reinstall the packages in the new location.

当我打开一个新的 R 会话并运行三行代码时

When I open a new R session and run the three lines of code

dir2 <-  \\NAS\TOOLSNEW$\R-3.5.1
.libPaths(dir2)
.libPaths()

我得到了 .libPaths()

[1] "\\\\NAS/TOOLSNEW$/R-3.5.1" "C:/Apps/R-3.5.1/library"

但 R 也会抛出错误:

but R also throws an error:

Error: invalid version specification ‘NA’
In addition: Warning message:
In utils:::packageDescription(packageName, fields = "Version") :
no package 'knitr' was found

显然有些软件包会记住它们的原始安装位置.有没有办法将软件包复制到新位置而不重新安装它们?如果这是不可能的,是否有一种简单(即非手动)的方法来重新安装我的包树而不升级任何包(包括依赖项),即定义每个包的版本?

Apparently some packages remember the original location where they were installed. Is there a way to copy packages to a new location without reinstalling them? If that is not possible, is there an easy (i.e. non-manual) way to reinstall my package tree without upgrading any of the packages (including the dependencies) i.e. defining the version of each package?

感谢您的帮助!

PS:这是我的会话信息,以防万一.

PS: Here is my session info, just in case.

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Switzerland.1252  LC_CTYPE=German_Switzerland.1252    
LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C                       
[5] LC_TIME=German_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0    

推荐答案

问题可能有多种原因,例如复制包文件夹可能会丢失一些依赖项,或者注册表项不再匹配,但我在这里猜测.当您从旧版本复制包时,您必须运行 update.packages(但这可能会将任何包更新到当前可用的版本,这是您不想要的).查看 R-FAQ 和这个 Stackoverflow 问题.

The problem can have many causes, e.g. there might be some dependencies which are lost by copying the package folder or that the registry entries don't match anymore, but I'm guessing here. When you copy packages from older versions you have to run update.packages (but this might update any package to the currently available version, which you don't want). Check out the R-FAQ and this Stackoverflow question.

您可以使用 devtools 包安装某个版本的包.

You can install packages of a certain version with the devtools package.

install.packages("devtools")
require(devtools)

install_version(somePackage, version = neededVersion)

我是如何做到的,我创建了一个data.frame,其中包含具有相应版本号的所有必需包,例如

How I did it was I created a data.frame of all the required packages with the corresponding version number, like

> requiredPkgs

                   Package              Version 
condformat         "condformat"         "0.9.0" 
DT                 "DT"                 "0.17"  
formattable        "formattable"        "0.2.1" 
ggplot2            "ggplot2"            "3.3.3" 
ggthemes           "ggthemes"           "4.2.4" 
htmlTable          "htmlTable"          "2.1.0" 

然后循环安装

for(iPkg in requiredPkgs) {
  install_version(iPkg[1], version = iPkg[2]
}

这篇关于更改 R 库的位置会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆