-
Notifications
You must be signed in to change notification settings - Fork 1
/
artifact_first_custom_package.Rmd
93 lines (62 loc) · 2.7 KB
/
artifact_first_custom_package.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "My First Custom Package"
author:
- name: Sajal Shrestha
url: https://sajalshres.github.io
date: "`r Sys.Date()`"
output: distill::distill_article
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction
Packages are the awesome way of sharing and dsitributing R code that help us to extend the capabilities of R programs and collaborate with others. Throughout my journey to learn R, I've extensively used packages like `dplyr`, `tibble`, `ggplot2`, `boot` etc. I cannot imagine developing R code without support for packages as it made my life much easier and saved a lot of time. So, I wanted to create a custom package to learn how to create packages and install them.
I created a package SearchGithubRepository using the code from functions artifacts.
The Github link to package is: [https://github.com/sajalshres/SearchGithubRespositories](https://github.com/sajalshres/SearchGithubRespositories)
## How to create a custom package
To create a custom package, I used `usethis::create_package` function to create a R package:
```
usethis::create_package("SearchGithubRespositories")
```
Next I created function for package using `usethis::use_r`:
```
usethis::use_r("search_github_repositories")
```
The `use_r` function creates a file based function inside `/R` folder.
After creating required functions, I added `DESCRIPTION` file that providdes the summary of the package such as title, version, author etc.
Then, I create documentation for each of the functions that I've added inside `/R` folder. An example for `version` function is:
```
#' version
#'
#' @return A message
#' @export
#'
#' @examples version()
```
Lastly, I used `check` function to see everything was working as expected and then installed my package using `install` function.
## Demonstration
### Load Libraries
I've installed and used `devtools` library to install custom package as it is not published yet.
```{r extend-r-load-package}
library(devtools)
```
### Load my custom package
I've used `install_github` function to install the `SearchGithubRespositories` package.
```{r extend-r-load-custom-package}
install_github("sajalshres/SearchGithubRespositories")
```
### Sample usage
Below is the demonstration of the usage of the package:
```{r extend-r-custom-package-usage}
SearchGithubRespositories::version()
```
```{r extend-r-custom-package-usage-2}
response <- SearchGithubRespositories::search_github_repositories(query = "sta-518")
response$total_count
```
### View help text
Similarly, we can view the help documentation using `?` operator.
```{r extend-r-custom-package-usage-3}
?SearchGithubRespositories::search_github_repositories
```
![](images/help_SearchGithubRespositories.png){width=50%}