Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create a new RStudio package project in a directory of your choice
- # We'll assume it's called, "mypkg"
- # Check "Create a git repository" if you use git!
- # Packages we'll need
- install.packages(c(
- "devtools",
- "usethis",
- "roxygen2",
- "testthat"
- ))
- # In Tools > Project Options > Build Tools make sure both
- # "Use devtools package functions if available", and
- # "Generate documentation with Roxygen" are checked (accept defaults)
- # Take a look at R/hello.R and man/hello.Rmd... then delete
- tryCatch(
- file.remove(c("R/hello.R", "man/hello.Rd")),
- error = NULL
- )
- # Oops, you've just realised you do want to use git!
- install.packages("git2r")
- git2r::config()
- usethis::use_git(message = "Initial commit")
- # usethis is soooo useful
- # Note: On my Windows installation this fails if the package is on a network drive
- usethis::use_readme_md() # or, usethis::use_readme_rmd() for RMarkdown
- usethis::use_mit_license("Your Name, Your Institution")
- usethis::use_r("my-function")
- usethis::use_testthat()
- usethis::use_test("my-function")
- # Go to my-function.R (or whatever you called it), and type or paste your
- # code. Add any dependencies with:
- # devtools::use_package("pkgname")
- # Then add some roxygen-compatible documentation to my-function.R
- # Roxygen documentation lines start with #' and should appear before the code
- # Don't forget "#' @import pkgname" for any dependencies and "#' @export"
- tryCatch(file.remove("NAMESPACE"), error = NULL) # recreate with roxygen
- devtools::document(roclets = c('rd', 'collate', 'namespace'))
- # Shortcut for previous line: Ctrl + Shift + D
- # Got to test-my-function.R (or whatever), and write some tests!
- devtools::test()
- # Shortcut for previous line: Ctrl + Shift + T
- # Go to README.md (or README.Rmd if you went that way) and make some changes!
- # Go to DESCRIPTION and make some changes!
- # Enter the package Title: and Version:, and update the Description: field
- # Add your name and email to the Author: and Maintainer: fields
- # Check the package
- devtools::check()
- # Shortcut for previous line: Ctrl + Shift + E
- # You're using git, right? Add and commit those changes
- git2r::add(path = "*")
- git2r::commit(message = "")
- # Load your package
- devtools::load_all(".")
- # Shortcut for previous line: Ctrl + Shift + L
- # Try out your function
- # Why not install your own package?
- devtools::install()
- # Or, Build > Install and Restart, or shortcut: Ctrl + Shift + B
- # Now to use it, just attach the library
- library(mypkg)
- # or, use the "pkgname::function()" syntax
- # Either way, enjoy your new personal R package!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement