
大家好,我是邓飞,今天介绍一下TwoSampleMR包如何设置token,从而可以使用数据库的数据进行孟德尔随机化的分析。
1,安装TwoSampleMR包这个包在github上面,之前流行用devtools包安装github上的R包,但是devtools本身就特别难安装,好在现在有了 remotes包,这个包比较好安装,进而github上面的包也比较好安装了。
# install.packages("remotes")library(remotes)install_github("MRCIEU/TwoSampleMR")将上面的代码贴到Rstudio中执行,就可以了。如果显示没有 remote包,就把注释去掉,再运行就行了。
图片
2, 运行MR示例数据library(TwoSampleMR)# List available GWASsao <- available_outcomes()# Get instrumentsexposure_dat <- extract_instruments("ieu-a-2")# Get effects of instruments on outcomeoutcome_dat <- extract_outcome_data(snps=exposure_dat$SNP, outcomes = "ieu-a-7")# Harmonise the exposure and outcome datadat <- harmonise_data(exposure_dat, outcome_dat)# Perform MR然后发现报错了:
> library(TwoSampleMR)TwoSampleMR version 0.6.8 载入程辑包:'TwoSampleMR’The following object is masked from 'package:remotes’: add_metadata> # List available GWASs> ao <- available_outcomes()Error in `dplyr::bind_rows()`:! Argument 1 must be a data frame or a named atomic vector.
报错的信息是:Error in dplyr::bind_rows():! Argument 1 must be a data frame or a named atomic vector.Run rlang::last_trace() to see where the error occurred.
看起来是dplyr的错,其实不是,继续运行看看能不能读取ieu的数据:
> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')
现在的报错信息是:> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')Error in if (nrow(d) == 0) return(NULL) : 参数长度为零
忧伤,如果你以为自己是R语言大神,想从R语言包安装的角度,包冲突的角度去解决问题,最后很大可能是 砸电脑!!!

正确的解决方法,是查看官网,如果你把报错信息贴到网上面,大概率也是找不到答案,因为之前TwoSampleMR还没有这个问题,网上的东西是互相抄,庄闲和游戏网垃圾信息满天飞,第一手资料永远是官网。
https://mrcieu.github.io/ieugwasr/articles/guide.html
里面有一句话:
From 1st May 2024, most queries to the OpenGWAS API will require user authentication. For more information on why this is necessary, see this [blog post](https://blog.opengwas.io/posts/user-auth-spring-2024/).
{jz:field.toptypename/}从2024年5月1号,TwoSampleMR包需要设置token之后,才可以访问数据库,所以,下面就是如何设置token的问题了。
3,TwoSampleMR设置token官方推荐方案:
A:更新你的R包
Please update your TwoSampleMR and ieugwasr packages - you can use the following command to do this.
建站客服QQ:88888888install.packages("TwoSampleMR", repos = c("https://mrcieu.r-universe.dev", "https://cran.r-project.org"))B:设置token
Then you need to obtain an OPENGWAS_JWT token - see the ieugwasr documentation https://mrcieu.github.io/ieugwasr/articles/guide.html - and store it in your .Renviron file - then restart R.
Login to https://api.opengwas.io/profile/
Generate a new token
Add OPENGWAS_JWT=<token> to your .Renviron file. This file could be either in your home directory or in the working directory of your R session. You can check the location of your .Renviron file by running Sys.getenv("R_ENVIRON_USER") in R.
Restart your R session
To check that your token is being recognised, run [ieugwasr::get_opengwas_jwt()](https://mrcieu.github.io/ieugwasr/reference/get_opengwas_jwt.html). If it returns a long random string then you are authenticated.
To check that your token is working, run [user()](https://mrcieu.github.io/ieugwasr/reference/user.html). It will make a request to the API for your user information using your token. It should return a list with your user information. If it returns an error, then your token is not working.
下面,我将上面的步骤,结合我自己的成功操作过程,介绍一下,跟着我的步骤,你也肯定能搞定啦!
3.1 登录opengwas,注册一下登录注册,用github账号,https://api.opengwas.io/profile/
3.2 创建token按照下面的截图就能搞定。
图片
把token复制一下。
3.3 在R语言中检测是否有token如果之前没有设置过,肯定是没有的。
Sys.getenv("R_ENVIRON_USER")我的返回结果:
> Sys.getenv("R_ENVIRON_USER")[1] ""可以看到,没有设置。
那就需要在文档文件夹中,新建一个.Renviron文件
图片
3.4 把token放到新建的.Renviron文件中3.5 重启R语言(必须)OPENGWAS_JWT="这里粘贴你的token"图片
重启R语言,然后键入下面代码,测试token是否设置成功:
## 测试token是否有效library(ieugwasr)user()
图片
可以看到个人的信息,就设置成功了。
4. 测试MR示例数据library(TwoSampleMR)# List available GWASsao <- available_outcomes()# Get instrumentsexposure_dat <- extract_instruments("ieu-a-2")# Get effects of instruments on outcomeoutcome_dat <- extract_outcome_data(snps=exposure_dat$SNP, outcomes = "ieu-a-7")# Harmonise the exposure and outcome datadat <- harmonise_data(exposure_dat, outcome_dat)# Perform MRres <- mr(dat)运行结果:
图片
作图结果:
图片
这就搞定了。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报。