R语言Windows编译环境及安装TwoSampleMR包

1,802次阅读
没有评论

首先需要构建 R 的编译环境

Window 下安装 R 的顺序为 R、Rtools、RStudio。安装基本上都是默认下一步,未做太多修改,可直接安装在 C 盘的可避免调整路径的麻烦。

R、Rtools、RStudio 下载地址:https://mirrors.tuna.tsinghua.edu.cn/CRAN/

R4.2.2:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/R-4.2.2-win.exe

devtools:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.2/devtools_2.4.5.zip

Rtools:https://cran.r-project.org/bin/windows/Rtools/rtools42/files/rtools42-5355-5357.exe

RStudio Desktop:https://download1.rstudio.org/desktop/windows/RStudio-2022.07.2-576.exe

usethis:https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.2/usethis_2.1.6.zip(之后可以在线安装)

Size: 190.49MB | Version: 2022.07.2+576 | | Released: 2022-09-21

https://posit.co/products/open-source/rstudio/
Open Source Edition
The Premier IDE for R
R 语言 Windows 编译环境及安装 TwoSampleMR 包
R 语言 Windows 编译环境及安装 TwoSampleMR 包
R 语言 Windows 编译环境及安装 TwoSampleMR 包

安装顺序(安装过程中的出错提示见文末):

一、安装 R

二、安装 Rtools42 for windows

三、安装 Rstudio 后启动,指定 R 的位置后进入操作界面;(RStudio 非必须安装,如果不安装 RStudio 则跳过第七步骤)

四、R 语言 - 路径设置与工作目录修改:

方法一:一劳永逸

执行以下代码

.libPaths("D:/Program Files/R/R-4.2.2/library")

方法二:使用 RStudio 窗口永久更改工作目录

Tools–>Global Options…—>Default working director (when not in a project):

修改后回到 RStudio 并重新启动

五、配置 rtools 路径

首先查看工作路径

1. 使用 getwd()函数来显示当前工作目录;

getwd()

2. 使用 setwd()函数更改当前目录;

setwd(“D:/R/Rwork”)

注意:
(1) 函数 setwd()不会自动创建一个不存在的目录。如果必要的话,可以使用函数 dir.create()来创建新目录,然后使用 setwd()将工作目录指向这个新目录。

(2)R 里的 dir.create()是不级联的,就是说一次只能创建有一个“/”的路径。如果需要两个“/”的路径,就需要用 dir.create()创建两次,然后文件才创建成功。再按照例子用 setwd()去设置即可。

(3)函数 setwd()是设置临时的工作路径。

(4)库的位置

.libPaths()
[1] “C:/Users/coral/AppData/Local/R/win-library/4.2”
[2] “D:/R/R-4.2.2/library”

方法一
进入 R Studio 里,运行以下代码

writeLines('PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"', con ="~/.Renviron")

没有出错就直接下一步骤,出现 In file(con, “w”) : 无法打开文件 ’C:/Users/???/Documents/.Renviron’: Invalid argument;错误的,执行方法二。

方法二

打开记事本,将 PATH=”${RTOOLS40_HOME}\usr\bin;${PATH}” 这段文字复制进去,保存到 R Studio 的工作路径下(不是安装路径!获取工作路径:在 R Studio 中运行 getwd()命令),文件名改为.Renviron,记住,不要有 txt 的后缀!

如果改名出现:必须键入文件名,请往下看,没有出错的直接下一步骤。

六、配置 Windows 的路径

以 Win10 为例,在系统 — 系统信息 — 高级系统设置 — 环境变量 —Path 编辑

编辑 R 程序所在的路径,编辑后注意放在第一、二行

R 语言 Windows 编译环境及安装 TwoSampleMR 包
R 语言 Windows 编译环境及安装 TwoSampleMR 包
R 语言 Windows 编译环境及安装 TwoSampleMR 包
R 语言 Windows 编译环境及安装 TwoSampleMR 包

七、关闭 R Studio,重新打开测试路径配置是否成功
运行 Sys.which(“make”) 这段代码,看输出是不是类似 ”C:\rtools40\usr\bin\make.exe”,具体因 rtools 安装路径而异,只要输出不是空字符串就行,表明路径配置成功。

八、手动安装 TwoSampleMR 的方法需要准备以下包 TwoSampleMR、MRInstruments、MR-PRESSO

手动安装 R 包,这里 R 包的安装顺序很重要。安装前必须要先安装好 devtools 这个包

TwoSampleMR:https://github.com/MRCIEU/TwoSampleMR

MR-PRESSO:https://github.com/rondolab/MR-PRESSO

MRInstruments:https://github.com/MRCIEU/MRInstruments

1、首先安装 MRinstruments 包:

devtools::install_local(‘./MRInstruments.zip’)

2、接下来安装 MRPRESSO 包:

devtools::install_local(‘./MRPRESSO.zip’)

3、最后,安装 TwoSampleMR 包:

devtools::install_local(‘./TwoSampleMR.zip’)

注意:最新的 0.5.6 版本可能存在报错

错误: Failed to install ‘unknown package’ from GitHub:
Timeout was reached: [api.github.com] Connection timed out after 10005 milliseconds

可以改为安装 0.4.22 版本(https://github.com/MRCIEU/TwoSampleMR/archive/refs/tags/0.4.22.zip

devtools::install_local(‘./TwoSampleMR-0.4.22.zip’)

4、安装完毕后加载 TwoSampleMR 包,出现如下提示即成功。

> library(TwoSampleMR)
Welcome to TwoSampleMR.
[>] Full documentation: https://mrcieu.github.io/TwoSampleMR
[>] Check news(package='TwoSampleMR') for bug fixes and updates
[>] By generating access tokens to retrieve data from the MR-Base
    database you consent to having your email address logged on
    our servers. For info on how this is used see logging_info()

最后要用到脚本实现功能,因此建议安装代码编辑器 Sublime text。

https://www.sublimetext.com/3


出错提示及解决方案

R 的 lib 没有被指定的解决办法

.libPaths() .libPaths(“D:/R/R-4.2.2/”)

可能安装报错提示缺少程辑包

以下包可以提前安装一遍

install.packages("fs")
fs
cli
ellipsis
lifecycle
miniUI
memoise
pkgbuild
pkgload
profvis
remotes
sessioninfo
urlchecker
usethis

如果安装报错

Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
不存在叫‘fs’这个名字的程辑包

打开 RGui(R 语言自己的界面),使用 install.packages(“fs”)命令安装程序包,然后程序包的位置会进行显示,然后点击菜单栏的程序包 -Install packages from local files…,根据提示的下载位置进行加载即可。

安装 TwoSampleMR 包提示错误

Error: Failed to install ‘TwoSampleMR-0.5.6.zip’ from local:
Timeout was reached: [api.github.com] Connection timed out after 10006 milliseconds

可以添加一个环境变量

Sys.setenv(CURL_CA_BUNDLE="C:/Users/coral/Documents/R/win-library/3.6/openssl/cacert.pem")

正文完
 0
Michael
版权声明:本站原创文章,由 Michael 于2022-11-15发表,共计3772字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码