Skip to content

Commit

Permalink
update R files
Browse files Browse the repository at this point in the history
  • Loading branch information
Hua Zou committed Aug 29, 2019
1 parent da0a822 commit c0be305
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 9 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Site settings
title: Hua's Blog
subtitle: Less is more.
description: Bioinformatics, postgraduate, Worker..
description: Bioinformatics, postgraduate, Worker.
author: Hua Zou


Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-01-Program-R1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "R编程语言简介(一)"
title: "R:R编程语言简介(一)"
date: 2018-04-01 20:06:05
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-02-Program-R2.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "R语言编程书写规则(二)"
title: "R:R语言编程书写规则(二)"
date: 2018-04-02 23:48:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-05-Program-R3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "R语言入门基础(三)"
title: "R:R语言入门基础(三)"
date: 2018-04-05 19:00:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-07-Program-R4.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "R语言基础函数介绍(四)"
title: "R:R语言基础函数介绍(四)"
date: 2018-04-07 12:00:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-09-Program-R5.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "动态图生成方法(五)"
title: "R:动态图生成方法(五)"
date: 2018-04-09 13:10:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-11-Program-R6.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "如何合并多个图形(六)"
title: "R:如何合并多个图形(六)"
date: 2018-04-11 10:10:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-04-27-Program-R7.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "apply函数家族(七)"
title: "R:apply函数家族(七)"
date: 2018-04-27 10:10:00
categories: 编程
tags: R
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-05-24-Program-R8.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: post
title: "ggplot2的基本原理和使用方法(八)"
title: "R:ggplot2的基本原理和使用方法(八)"
date: 2018-05-24 20:36:00
categories: 编程
tags: R
Expand Down
133 changes: 133 additions & 0 deletions _posts/2019-08-29-Program-R10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
layout: post
title: "R:R包的创建及使用(十)"
date: 2019-08-29 21:36:00
categories: 编程
tags: R
---



### 目的

为了将经常使用的R函数打包成R包,从而方便自己使用,因此有必要学习制作R包

### 结构

```
pacakgename(包名字)
|
|--DESCRIPTION(描述文件,包名、版本号、依赖关系等)
|--NAMESPACE(命名空间,导入包和函数)
|--R/(函数源码目录)
|--Script1.R (包含函数的脚本)
|--Script2.R
|--....
|--man/(函数的帮助文档目录)
|--Script1.function1.Rd(函数的说明文件,latex语法)
|--Script1.function2.Rd
|--Script2.function1.Rd
|--....
|--data/(存放R类型数据目录)
|--inst/(存放原始数据目录)
|--vignettes/(存放包说明文档目录)
```

### 准备工作

##### 安装必要R包

- devtools
- roxygen2
- knitr

##### 使用Rstudio创建R project

### 修改文件

#### DESCRIPTION

```
• Package: 名字
• Type: 类型
• Title: 作用
• Version: 版本
• Author: 作者
• Maintainer: 邮箱
• Description: 描述
• License: 开源协议
• Encoding: 编码格式
• LazyData: true
• Date: 日期
• Depends: R环境或者依赖R包
• imports: 导入包
• Suggests: 建议包
```

#### Namespace

```
# import packages
• import(ggplot2)
# import function from packags
• importFrom(tidyr, spread)
# import functions edited by author in a hand manner
• export(function1)
```



#### R代码撰写原则

```
1. 命名:function.R
2. 代码风格:
§ install.package("formatR")
§ formatR::tidy_dir("R")重新编译当前目录所有R代码
3. 变量和函数名字小写,underscore下划线分割名字;变量名字是名词,函数是动词
§ day_one
4. 避免使用已经存在的变量和函数名字
5. infix operators(=, <- )均需要出现空格符号,即使是函数内部参数;每个comma后跟随一个space
§ average <- mean(feet / 12 + inches, na.rm = TRUE)
6. 左括号前空出一个空格,出了plot函数
§ if (degug) do(x); plot(x, y)
7. 每行80字符
8. R landscape:尽量不要改变landscape,下列方法会改变运行环境
i. 不要使用library()或者require()函数,而是使用DESCRIPTION调用依赖包
ii. 不要用source加载代码
iii. 保留原始环境退出:
1) old <- options(stringsAsFactors = FALSE)
2) on.exit(options(old), add = TRUE)
3) old <- setwd(tempdir())
4) oe.exit(setwd(old), add = TRUE)
iv. 函数避免加载时候同时输出图和结果,分开执行.
```

#### R函数表头信息

```
• @description: 描述函数作用
• @import ggplot2:导入函数需要依赖的包
• @importFrom ggthemes theme_wsj:导入某个包的函数
• @return: 返回类型
• @export: 输出格式
• @param: 参数具体描述
• @details: 参数的细节
• @examples: 函数使用范围
Ctrl+Shift+Alt+R(光标放在函数名上)会自动创建注释
```

### 生成包

先check,如果没有错误信息则直接生成包,包类型有二进制和源码格式

### 引用

1. [R包创建](http://r-pkgs.had.co.nz/intro.html)


参考文章如引起任何侵权问题,可以与我[联系](https://github.com/HuaZou/),谢谢。

0 comments on commit c0be305

Please sign in to comment.