R语言学习
R源码安装常见报错及更换package安装源
统计检验应用
卡方检验案例应用
linux本地安装ggplot2
shell脚本中运行R代码
conda构建虚拟环境
karyoploteR基因组数据可视化
Linux环境中R语言绘图问题
【实用】Bioconductor的正确使用
本文档使用 MrDoc 发布
-
+
up
down
首页
R源码安装常见报错及更换package安装源
# R语言安装及常见报错 ## 20240731 提示`x86_64-conda-linux-gnu-cc not found` 方法[来源](https://blog.csdn.net/weixin_64316191/article/details/128945014) 1. 类似如下提示未找到`x86_64-conda-linux-gnu-cc`  2. 根据提示找到R所在lib的路径文件`/path/to/lib/R/etc/Makeconf`中的第 171 行代码。  3. 变量`CC`,在前面已经声明  4. 将变量`CC`的值修改为`x86_64-conda-linux-gnu-cc`的绝对路径  5. 本次错误通过上面👆方法修复了,另一种情况就是你本地就没安装 `x86_64-conda-linux-gnu-cc`。如果是这样,你需要在你使用的 R 所在的 conda 虚拟环境中安装它。安装命令如下: ``` conda install gcc_linux-64 conda install gxx_linux-64 conda install gfortran_linux-64 ``` ## 20240730 提示某个版本不可用 1. 安装ggplot2报错,提示MASS未安装 2. 那就`install.packages("MASS")`,却报错“package ‘MASS’ is not available for this version of R” 3. 那就找对应能用的版本,实用mysearch函数 ``` mysearch <- function(pkg){ require(tidyverse) require(pkgsearch) cran_package_history(pkg) |> select(Package,Date,Version,dependencies) |> rowwise() |> mutate(Date=ymd(Date), R_VERSION = filter(dependencies,package=="R")) |> ungroup() |> arrange(desc(Date)) } ``` 4. 搜索R版本对应的版本`mysearch("MASS")` 5. `install.packages` 函数并不支持版本的选择,但是如果知道R包的源文件的位置:https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/Archive/MASS/ 或者其他镜像,则可以直接通过 `source` 的方式安装: ``` packageurl = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/Archive/MASS/MASS_7.3-58.tar.gz" install.packages(packageurl, repos = NULL, type = 'source') ``` ## 1、下载 tar zxvf R-3.4.1.tar.gz ``` tar xzvf R-3.4.1.tar.gz cd R-3.4.1 ./configure --prefix=/home/lhlv/projects/softwares/app/R-3.4.1 --enable-R-shlib ``` **报错:** ``` checking for zlib.h... yes checking if zlib version >= 1.2.5... no checking whether zlib support suffices... configure: error: zlib library and headers are required ``` **原因:**缺少zlib或者zlib版本过低 ## 2、安装zlib (1)安装前,设置安装目录的环境变量,在.bashrc配置中,输入如下内容: ``` export APP_PATH=/home/lhlv/projects/softwares/app export LD_LIBRARY_PATH=$APP_PATH/packages/lib:$LD_LIBRARY_PATH export CFLAGS="-I$APP_PATH/packages/include" export LDFLAGS="-L$APP_PATH/packages/lib" ``` 且之后缺少的软件都安装到$APP_PATH/packages目录下。 (2)zlib安装: ``` tar xvf zlib-1.2.11.tar.gz cd zlib-1.2.11 #构建静态库安装 ./configure --prefix=$APP_PATH/packages make && make install #构建共享库安装 make clean ./configure --shared make test make install cp zutil.h /usr/local/include cp zutil.c /usr/local/include ``` ## 3、继续安装R,并指定LDFLAGS和CPPFLAGS库 ``` cd R-3.4.1 ./configure --prefix=$APP_PATH/R-3.4.1 --enable-R-shlib ##或者 cd R-3.4.1 ./configure --prefix=$APP_PATH/R-3.4.1 --enable-R-shlib LDFLAGS="-L/$APP_PATH/packages/lib" CPPFLAGS="-I/$APP_PATH/packages/include" #这儿就开始出现以下报错啦 make && make install ``` **报错:** ``` checking for zlib.h... yes checking if zlib version >= 1.2.5... yes checking whether zlib support suffices... yes checking mmap support for zlib... yes checking for BZ2_bzlibVersion in -lbz2... no checking whether bzip2 support suffices... configure: error: bzip2 library and headers are required ``` **原因:**zlib版本符合要求,缺少bzip2 ## 4、安装bzip2 ``` tar zxvf bzip2-1.0.6.tar.gz cd bzip2-1.0.6 make -f Makefile-libbz2_so make clean make make install PREFIX=$APP_PATH/packages ``` ## 5、重复第3步 **报错:** ``` checking for lzma_version_number in -llzma... no configure: error: "liblzma library and headers are required" ``` **原因:**liblzma (xz软件包)缺少 ## 6、安装xz ``` tar zxvf xz-5.2.3.tar.gz cd xz-5.2.3 ./configure --prefix=$APP_PATH/packages make -j3 make install ``` ## 7、重复第3步 **报错:** ``` checking whether bzip2 support suffices... no checking for lzma_version_number in -llzma... yes checking lzma.h usability... yes checking lzma.h presence... yes checking for lzma.h... yes checking if lzma version >= 5.0.3... yes checking for pcre_fullinfo in -lpcre... no checking whether PCRE support suffices... configure: error: pcre >= 8.10 library and headers are required ``` **原因:**prce版本过低或者缺少 ## 8、安装pcre ``` tar zxvf pcre-8.41.tar.gz cd pcre-8.41 ./configure --prefix=$APP_PATH/packages make -j3 && make install ``` ## 9、重复第3步,再试 (1)报错: ``` checking pcre/pcre.h usability... no checking pcre/pcre.h presence... no checking for pcre/pcre.h... no checking if PCRE version >= 8.20, < 10.0 and has UTF-8 support... no checking whether PCRE support suffices... configure: error: pcre >= 8.20 library and headers are required ``` **原因:**PCRE需要安装--enable-utf8 (2)重新安装pcre ``` cd pcre-8.41 ./configure --prefix=$APP_PATH/packages --enable-utf8 make -j3 && make install ``` ## 10、继续第3步 **报错:** ``` checking if libcurl is version 7 and >= 7.22.0... no configure: error: libcurl >= 7.22.0 library and headers are required with support for https ``` **原因:**未安装curl ## 11、安装curl ``` tar zxvf curl-7.46.0.tar.gz && cd /root/curl-7.46.0 ./configure --prefix=$APP_PATH/packages && make && make install #如果 提示libcurl is version 7 and >=7.22.0 ...yes,但有提示checking if libcurl supports https... no #则需要安装openssl,然后在重装curl库 tar -xzvf openssl-1.0.1u.tar.gz cd openssl-1.0.1u ./config -fPIC --prefix=/usr/local/openssl enable-shared ./config -t make && make install #并设置环境变量: export OPENSSL=$APP_PATH/packages/bin export PATH=$OPENSSL:$PATH:$HOME/bin #重新安装curl:指定openssl ./configure --prefix=$APP_PATH/packages --with-openssl-include=$APP_PATH/packages/include/ --with-openssl-lib=$APP_PATH/packages/lib && make -j3 ``` ## 12、再次第3步 警告: ``` configure: WARNING: you cannot build info or HTML versions of the R manuals configure: WARNING: neither inconsolata.sty nor zi4.sty found: PDF vignettes and package manuals will not be rendered optimally ``` ## 13、如遇到如下报错: ``` checking curl/curl.h usability... yes checking curl/curl.h presence... yes checking for curl/curl.h... yes checking if libcurl is version 7 and >= 7.28.0... yes checking if libcurl supports https... no configure: error: libcurl >= 7.28.0 library and headers are required with support for https ##这是一个大坑需要在环境变量中加上curl的bin路径,如果没有报错,那肯定是之前系统默认安装的curl造成的,为了消除潜在风险,是否有报错,请必须执行下面的操作,系统默认安装的curl请保留,防止造成别的系统程序依赖缺失. 添加如下到.bashrc中并source export PATH=$PATH:$JAVA_HOME/bin:$APP_PATH/packages/bin ``` ## 14、继续安装 `./configure --prefix=$APP_PATH/R-3.4.1`完成后就可以运行: `make && make install` 报错: ``` /usr/bin/ld: /home/lhlv/projects/softwares/app/packages/lib/libbz2.a(bzlib.o): relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC /home/lhlv/projects/softwares/app/packages/lib/libbz2.a: could not read symbols: Bad value collect2: ld returned 1 exit status make[3]: *** [libR.so] Error 1 make[3]: Leaving directory `/home/lhlv/projects/softwares/R-3.4.1/src/main' make[2]: *** [R] Error 2 make[2]: Leaving directory `/home/lhlv/projects/softwares/R-3.4.1/src/main' make[1]: *** [R] Error 1 make[1]: Leaving directory `/home/lhlv/projects/softwares/R-3.4.1/src' make: *** [R] Error 1 ``` **原因:**此处报错是由于没有找到动态库, **解决方法**:添加动态库(可惜需要root权限) ``` #cat /etc/ld.so.conf include ld.so.conf.d/*.conf /home/lhlv/projects/softwares/app/packages/lib ``` 然后执行 `ldconfig` 再运行 ``` make make install ``` 如果没有报错,即可在`vim .bashrc` 中添加R源环境变量 ``` export PATH=$PATH:$JAVA_HOME/bin:$APP_PATH/packages/bin:$APP_PATH/R-3.4.1/bin ``` `source .bashrc` ##让环境变量立马生效 # R更换package安装源 有时候利用R安装R包,出现错误: ``` Warning in install.packages : unable to access index for repository https://cran.rstudio.com/src/contrib: cannot open URL 'https://cran.rstudio.com/src/contrib/PACKAGES' Warning in install.packages : package ‘readxl’ is not available (for R version 3.4.1) ``` **原因:**可能是因为国外的rstudio或者其他源链接不上,此时换成国内源即可解决问题,更换方法如下: ## 1. 使用Rstudio 打开 Tools -> Global options -> Packages  ## 2. 使用终端 在使用终端命令行安装包的时候,可以直接指定源,命令如下: ``` install.packages(‘gdata’, repos = ‘https://mirrors.tuna.tsinghua.edu.cn/CRAN’) ``` ## 3. 更改配置文件 linux或mac系统下,安装速度过慢,更改为国内源方案,编辑~/.Rprofile, ``` # 指定国内CRAN源: options(repos=structure(c(CRAN=“https://mirrors.tuna.tsinghua.edu.cn/CRAN/”))) #指定国内bioconductor源: source(“http://bioconductor.org/biocLite.R”) options(BioC_mirror=“http://mirrors.ustc.edu.cn/bioc/”) biocLite(“clusterProfiler”) ``` ## 通过Rprofile自定义函数: ``` source.bioconductor <- function(){ source("http://bioconductor.org/biocLite.R") options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/") } ``` 通过调用source.bioconductor()函数,改变源并安装包 # shell脚本中运行R代码 ## 问题 你正在编写一个命令脚本,如UNIX或OS X系统的shell脚本,或Windows系统中的批处理脚本,并且你希望在这些脚本中执行R代码。 ## 解决方案 使用带有CMD BATCH子命令的方式运行R程序,并给出脚本文件名和输出文件名: ``` $ R CMD BATCH scriptfile outputfile ``` 如果需要将输出结果发送到标准输出设备中,或者希望将命令行参数传递到脚本中,可以考虑应用Rscript命令: ``` $ Rscript scriptfile arg1 arg2 arg3 ``` ## 讨论 R是一个交互式软件,它提示用户输入,然后显示输出结果。有时你想在批处理模式下运行R,从脚本读取命令。这对于shell内部的脚本,例如含有统计分析的脚本而言特别有用。 CMD BATCH子命令把R转到批处理模式,它读取脚本文件scriptfile并且把输出写入输出文件outputfile。这个运行过程中不与用户交互。 你可能会根据具体情况使用命令行选项,调整R的批处理过程。例如,使用--quiet选项来避免启动信息,否则将使输出信息混乱: ``` $ R CMD BATCH --quiet myScript.R results.out ``` 下面是一些其他批处理模式下的实用命令: - \-\-slave 类似于--quiet,它禁止回送输入的信息,使R软件输出的信息更为简洁。 - \-\-no-restore 在R启动时不还原工作空间。对于希望以空白工作空间启动R的脚本而言,这个选项很有必要。 - \-\-no-save 在退出R时,不保存工作空间;否则,R会保存当前工作空间并覆盖原有工作目录中的.RData文件。 - \-\-no-init-file 不读取.Rprofile文件或者~/. Rprofile文件。 在脚本运行结束后,CMD BATCH子命令一般会使用proc.time函数显示其执行的时间。如果你不需要显示该时间,可以在代码最后一行调用参数为runLast=FALSE的q函数,它将防止调用proc.time函数。 CMD BATCH子命令有两个限制条件:输出必须总是传送到一个文件中,并且无法简单地将命令行参数传送到脚本中。如果这两个限制成为问题,可以考虑使用R软件自带的Rscript程序。Rscript命令的第一个命令行参数是脚本文件的名称,其余的参数将传递给脚本代码: ``` $ Rscript myScript.R arg1 arg2 arg3 ``` 在脚本中,命令行参数可以通过调用commandArgs函数来获取,该函数会把参数作为一个字符串向量返回: argv <- commandArgs(TRUE) Rscript程序和CMD BATCH命令使用上面所提到的相同的命令行选项。 将输出结果输出到标准输出设备中,该设备是R从调用它的shell脚本中继承来的。当然,可以通过一般的重定向方法将输出重定向到一个文件中: $ Rscript --slave myScript.R arg1 arg2 arg3 >results.out 下面是一个名为arith.R的简易R脚本文件,它对两个命令行参数进行四个算术运算: ``` argv <- commandArgs(TRUE) x <- as.numeric(argv[1]) y <- as.numeric(argv[2]) cat("x =", x, "\n") cat("y =", y, "\n") cat("x + y = ", x + y, "\n") cat("x - y = ", x - y, "\n") cat("x * y = ", x * y, "\n") cat("x / y = ", x / y, "\n") ``` 脚本以下面的形式调用: ``` $ Rscript arith.R 2 3.1415 ``` 产生如下结果: ``` x = 2 y = 3.1415 x + y = 5.1415 x - y = -1.1415 x * y = 6.283 x / y = 0.6366385 ``` 在Linux或UNIX系统中,你可以在脚本的开头添加#!后跟随Rscript程序的路径,这样脚本就是完全自我包含的了(即代码变得完全独立于外部)。假定Rscript程序安装在/usr/bin/Rscript目录中,你可以在arith.R脚本文件中添加下面一行,使其成为自我包含代码: ``` #!/usr/bin/Rscript --slave argv <- commandArgs(TRUE) x <- as.numeric(argv[1]) ``` 在提示符处,我们把脚本文件标记为可执行文件: ``` $ chmod +x arith.R ``` 此时我们可以不用Rscript前缀而直接调用脚本代码: ``` $ arith.R 2 3.1415 ```
laihui126
2024年7月31日 09:38
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 MrDoc
觅道文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅道文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅道文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
下载Markdown文件
分享
链接
类型
密码
更新密码