使用Rstudio进行project管理

1 创建Rstudio project

1.1 需要跟github配合使用,首先在github创建repo.

1.2 本地创建Rstudio project

2 创建必须文件夹

2.1 在Rstudio project的本地文件夹下创建必须的文件夹.

2.2 github忽略某些文件/文件夹

如果不想要上传某些文件或者文件夹到github,在.gitgnore中写明.

2.3 第一次上传到github

git add.
git commit -m "first commit"
git push

3 创建website

使用distill,参考网站:

  1. https://distill.netlify.app/

  2. https://rstudio4edu.github.io/rstudio4edu-book/make-distill.html

3.1 安装distill

remotes::install_github("rstudio/distill")

3.2 创建网站地址

不要将webiste文件放到root路径,可以在root路径下创建一个文件夹,比如website_files,然后把文件都放到这里面.

distill::create_website(dir = "website_files/", gh_pages = TRUE)

3.3 Build website

rmarkdown::render_site(input = "website_files/")

3.4 将doc文件夹复制到root路径

file.copy(from = "website_files/docs/", to = ".", recursive = TRUE, overwrite = TRUE)

这时候会在root有一个doc文件夹.

3.5 发布website到github pages

3.5.1 提交github.

git add.
git commit -m "create website"
git push

3.5.2 设置website

这时候就可以看到网址了.

https://jaspershen.github.io/rstudio_project_test/

这时候网站就已经公开了.

4 将github repo设置为private

4.1 在文章发表之前,不想别人看到我们的数据/code等.

这时候虽然别人看不到你的github repo,但是网址仍然可见.

4.2 邀请合作者

这样,合作者还是可以看到repo.

5 装扮website

5.1 website_files简单介绍

  1. index.Rmd: This will be our site’s homepage. This file must be named index.Rmd.

  2. about.Rmd: This will be an additional page on our site, and you can change the name of this file to be whatever you want.

  3. _site.yml: is not a file that gets turned into a site page, but it is necessary for site setup.

  4. docs/: folder: This is the output folder, where knitted versions of our .Rmds live once they’ve been processed to become .html files.

5.2 修改页面布局

打开_site.yml

然后按照下图进行修改.

编辑好文章之后,重新render并将docs copy到root路径

rmarkdown::render_site(input = "website_files/")
file.copy(from = "website_files/docs/", to = ".", recursive = TRUE, overwrite = TRUE)

重新生成网站如下:

但是这时候还没有提交到github.只有再次提交到github网址链接的网站才会更新.

5.3 创建新的文章并将其加入到网站中

5.3.1 创建新的rmarkdown文件,放在website_files文件夹中.

5.3.2 编辑好文章之后,添加到网页.

这里我们生成的文章名字为figure1.html,所以在_site.yml修改:

然后重新render,并提交到github即可.

打开链接:

6 我自己的project管理模式

6.1 文件结构

  1. data:存放原始数据. 不上传到github.

  2. data_analysis: 存放分析结果. 发表前不上传到github.

  3. code: 分析脚本. 公开.

  4. summary: slides, document等. 不公开.

  5. manuscript: figures. 不公开.

  6. docs: project网站介绍,公开.

6.2 分析及重复

数据分析自己以及别人重复很重要.

6.2.1 几个原则

  1. 以Rstudio project为中心组织数据和code

所有的数据和code都以Rstudio project为中心进行组织. 文章发表之后,public project,如果别人要重复分析,下载Rstudio project, 包含了数据和code.

  1. 使用相对路径

在code中,尽量不要使用绝对路径,使用相对路径. 每个code中,都首先将work directory定为到Rstudio project所在位置.

如果想要使用tinyTools,从github安装:

devtools::install_github("jaspershen/tinyTools")
  1. 代码命名

代码按照分析步骤进行命名.

代码运行的可能会有先后依赖关系,命名时最好注明.

6.2.2 直接使用r script生成html文件

我比较习惯直接使用r script而不是rmarkdown.为了能够直接render成html格式,需要在前面加上一些:

#' ---
#' title: "Stool microbiome metabolome correlation"
#' author: 
#'   - name: "Xiaotao Shen" 
#'     url: https://www.shenxt.info/
#'     affiliation: Stanford School of Medicine
#' date: "2021-05-12"
#' site: distill::distill_website
#' output: 
#'   distill::distill_article:
#'     code_folding: false
#' ---

#+ r setup, echo=TRUE, eval = FALSE, include = TRUE

参考网址:

https://bookdown.org/yihui/rmarkdown-cookbook/spin.html

一些基本用法:

  1. #'后是markdown语法内容, 比如想要使用一级标题,需要这样写:
#' # title
  1. #+后面是r code chunk设置.比如我们想要设置某段代码不运行但是显示,需要这样写:
#+ eval=FALSE, echo = TRUE

相当于rmarkdown的:

{r eval=FALSE, echo = TRUE}

6.2.3 一个例子

在code中创建r script

然后运行之后,会在保存该script的路径下生成html文件.

将该html文件copy到docs文件夹下.

sxtTools::setwd_project()
file.copy(from = "code/microbiome_analysis/data_summary.html", to = "docs/")

将该文件放到网页中.

然后重新render website,copy webiste_files/docs../docs.

rmarkdown::render_site(input = "website_files/")
file.copy(from = "website_files/docs/", to = ".", recursive = TRUE, overwrite = TRUE)

然后提交到github.

6.2.4 仍然使用rmakdown

仍然在code中建立相应的rmakdown文件.

最后需要手动copy到website_files文件夹中.

后续步骤相同.

Avatar
Xiaotao Shen
Postdoctoral Research Fellow

Metabolomics, Multi-omics, Bioinformatics, Systems Biology.

Related

Next
Previous
comments powered by Disqus