A Step-by-Step Guide to Creating an R Package and Uploading to GitHub

A Step-by-Step Guide to Creating an R Package and Uploading to GitHub





1) Create a folder and R package

I’ll create an R package named kimindex. This package will contain a simple code to predict grain weight based on the area of a wheat grain. I have already developed a model equation: y=x^1.32, where y is the wheat grain weight and x is the grain area.

if(!require(devtools)) install.packages("devtools")
if(!require(usethis)) install.packages("usethis")
if(!require(available)) install.packages("available")
library(devtools)
library(usethis)
library(available)

available::available("intercept0")

Install available package if not installed

if (!requireNamespace(“available”, quietly = TRUE)) {
install.packages(“available”)
}

Check package name availability

available::available(“interptools”)

#to create a new folder
dir.create("C:/Users/R/package/kimindex")
#to create a package
create_package("C:/Users/package/kimindex")

The basic R package files for kimindex have been saved in the kimindex folder.






2) Set up git for the package (skip)

First, you need to download Git (https://git-scm.com/downloads). Then, I’ll run use_git().

library(devtools)
use_git()

There is an error stating that user.name was not found. To solve this issue, you need to configure username and email address.

First, push window key + R, and type powershell.

and type your name and email address.

git config --global user.name "First Last name"
git config --global user.email "[email protected]"
git config --global --list

The, the run the code again.

library(devtools)
use_git()

It’s successful!!

If you see Environment section in R studio, you can see Git tab. Let’s adjust tabs in the Environment section. Go to ToolsGlobal Options... and select Pane Layout. I’ll only select Environment, Build and VCS.

Now, the tabs are much simple.






3) Add R code to package

I’ll add codes for my R package. This code is to predict grain weight from area.

I’ll save this code to the folder under a new name as specified in the following code.

library(devtools)
use_r("kimindex")

The code will be saved in R folder in the designated folder (C:/Users/R/package/kimindex).

Let’s load all using the following code.

load_all()

rm(list = c("intercept0"))

Let’s check the package.

check()





4) Modify documentation

4.1) modify DESCRIPTION

4.2) Add license

use_mit_license()

I’ll commit those files (skip)






5) Insert Roxygen Skeleton

Go to CodeInsert Roxygen Skeleton

Let’s run the following code.

document()
?kimindex

I’ll commit those documents (SKIP)






6) Install package

Before installing the package, I’ll finally commit R file.

Next, I’ll run the following code.

install()





7) publish package to GitHub

modify 1)R / 2)mas folder in both local and Github

setwd("C:/Users/agron/OneDrive/Coding/R/package/datazip")
devtools::document()
devtools::install()

if(!require(remotes)) install.packages("remotes")
if (!requireNamespace("datazip", quietly = TRUE)) {
  remotes::install_github("agronomy4future/datazip", force= TRUE)
}
library(remotes)
library(datazip)

?datazip


detach("package:datazip", unload=TRUE)
unloadNamespace("datazip")







Comments are closed.