R 4.4版本安装退役plyr包失败的解决方法咨询
如何在R 4.4版本中安装plyr包?
问题背景
将R版本更新至4.4后,大部分包丢失需要重新安装。尽管plyr包已退役,但由于安装phyloseq包依赖它,尝试安装plyr时出现编译错误,错误信息如下:
> install.packages("plyr") Installing package into ‘/home/sskoldas/R/x86_64-pc-linux-gnu-library/4.4’ (as ‘lib’ is unspecified) trying URL 'https://cloud.r-project.org/src/contrib/plyr_1.8.9.tar.gz' Content type 'application/x-gzip' length 401488 bytes (392 KB) ================================================== downloaded 392 KB * installing *source* package ‘plyr’ ... ** package ‘plyr’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’ using C++ compiler: ‘g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’ g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -fpic -g -O2 -ffile-prefix-map=/build/r-base-H8urij/r-base-4.4.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c RcppExports.cpp -o RcppExports.o In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp/routines.h:26, from /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:128, from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27, from RcppExports.cpp:4: /usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h: In member function ‘std::streamsize Rcpp::Rstreambuf<OUTPUT>::xsputn(const char*, std::streamsize) [with bool OUTPUT = true; std::streamsize = long int]’: /usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:53:20: warning: field precision specifier ‘.*’ expects argument of type ‘int’, but argument 2 has type ‘std::streamsize’ {aka ‘long int’} [-Wformat=] 53 | Rprintf("%.*s", num, s); | ~~^~ ~~~ | | | | int std::streamsize {aka ‘long int’} /usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h: In member function ‘std::streamsize Rcpp::Rstreambuf<OUTPUT>::xsputn(const char*, std::streamsize) [with bool OUTPUT = false; std::streamsize = long int]’: /usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:57:21: warning: field precision specifier ‘.*’ expects argument of type ‘int’, but argument 2 has type ‘std::streamsize’ {aka ‘long int’} [-Wformat=] 57 | REprintf("%.*s", num, s); | ~~^~ ~~~ | | | | int std::streamsize {aka ‘long int’} In file included from /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:181, from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27, from RcppExports.cpp:4: /usr/local/lib/R/site-library/Rcpp/include/Rcpp/print.h: In function ‘void Rcpp::warningcall(SEXP, const string&)’: /usr/local/lib/R/site-library/Rcpp/include/Rcpp/print.h:30:19: error: format not a string literal and no format arguments [-Werror=format-security] 30 | Rf_warningcall(call, s.c_str()); | ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ cc1plus: some warnings being treated as errors make: *** [/usr/lib/R/etc/Makeconf:204: RcppExports.o] Error 1 ERROR: compilation failed for package ‘plyr’ * removing ‘/home/sskoldas/R/x86_64-pc-linux-gnu-library/4.4/plyr’ Warning in install.packages : installation of package ‘plyr’ had non-zero exit status The downloaded source packages are in ‘/tmp/RtmpWy0Zao/downloaded_packages’
解决办法
方法一:临时禁用格式安全编译警告
错误核心是编译器将-Wformat-security相关警告当作错误处理,导致编译失败。可以临时修改环境变量关闭该错误检查:
# 设置编译选项,禁用format-security错误 Sys.setenv(CFLAGS="-Wno-error=format-security", CXXFLAGS="-Wno-error=format-security") # 重新安装plyr install.packages("plyr")
安装完成后,若不需要保留该设置,可执行Sys.unsetenv(c("CFLAGS", "CXXFLAGS"))恢复默认。
方法二:安装系统二进制包(Linux用户)
如果是Debian/Ubuntu系列系统,可以直接通过系统包管理器安装预编译好的plyr包,避免编译问题:
sudo apt-get install r-cran-plyr
安装完成后,在R中即可直接加载使用。
方法三:指定安装旧版本(若上述方法失效)
若新版本plyr在R4.4下兼容性问题无法解决,可以尝试安装适配性更好的旧版本:
# 先安装devtools(若未安装) install.packages("devtools") # 安装指定版本的plyr devtools::install_version("plyr", version = "1.8.8")
内容的提问来源于stack exchange,提问作者Seda Koldas
相关产品推荐
相关产品推荐

