# Run next lines one line at a timeusethis::create_package("testpkg")usethis::use_git() # initializes git repository, adds initial commitusethis::create_github_token() # create and securely store your PATgitcreds::gitcreds_set()usethis::use_github() # publishes to your GitHub accountusethis::use_roxygen_md() # check DESCRIPTION for roxygen notesusethis::use_r("my_function") # create or modify a function
Add this to the R script that opens in RStudio after use_r
#' Say Hello#'#' Prints a greeting.#' @exporthello <-function() {print("Hello, world!")}
Setup with {usethis}
Lastly, document your new function and pkg (you’ll rerun this function a lot as you develop a pkg)
devtools::document() # document pkg, rerun after pkg changes
Optional: Add a license to prevent Metadata error when testing your function
usethis::use_mit_license("Your Name")
Test, Install, Publish, Share!
Now, in the build pane next to git panes
Click Test
Click Install if you are satisfied with the tests
After install is complete, try your function in the console.
If it’s working, make another commit to GitHub with your updated files.
Install from GitHub
devtools::install_github("DOH-SRE1303/testpkg")
Installs your package from GitHub
Works in scripts, R projects, RStudio
You’ll probably need Rtools to build most R packages from source (i.e. GitHub repo).
Later, you can provide other means for obtaining your package without Rtools
Common Issues
Missing or malformed DESCRIPTION
Forgot to document (devtools::document())
Push not reflected on GitHub (check remotes)
in terminal:
git remote -v
or in R console:
system("git remote -v")
Wrap-Up
GitHub supports both scripts and packages
Packages are best for reusable logic
Use {usethis}, {devtools}, {roxygen2} to streamline