package.Rproj
to load the project into RStudioinstall.packages(c("usethis", "devtools", "testthat", "pkgdown"))
usethis::use_description(check_name = FALSE)
DESCRIPTION
file (note, no spaces or -
allowed)math.R
) file in the R
folder and write a simple function, e.g.:R
#' Add together two numbers.
#'
#' @param x A number
#' @param y A number
#' @return the The sum of \code{x} and \code{y}
#' @examples
#' add_numbers(1, 2) # add 1 + 2
#' @export
add_numbers <- function(x, y) {
return(x+y)
}
devtools::load_all()
(Ctrl/Cmd
+ Shift
+ L
)add_numbers(2, 5)
@export
in their documentation will be made available to your users via regular installation_
prefix for all functions in your package (e.g. yay_add_numbers
, yay_multiply_numbers
) because it makes it very easy for your users to use the auto-complete feature to find the right function.devtools::document()
(Ctrl/Cmd
+ Shift
+ D
)?add_numbers()
?packagename
NAMESPACE
to see what it contains@export
to), it also lists a function that is imported into your package from another packge - this is important for all functions you want to use from other packages so it is clear where R should go find them?
functions from within R but it is very helpful for your users to have use cases and function documentation easily accessible also on the webpkgdown
, set it up with usethis::use_pkgdown()
pkgdown::build_site()
to demo your documentation web sitepkgdown
is extremely customizable and you can specify how your page should be structured and styled using the _pgkdown.yml
file. For details, check out the pgkdown website (generated itself by pkgdown
...)pkgdown
online documentation automatically after every push to the master branch once you enable Actions (in the menu at the top) for your repository and select the gh-pages
branch as Source in your repository Settings (in the menu at the top) under GitHub Pages. The resulting documentation page becomes available at https://cub-computational-tools.github.io/YOUR_REPO_NAME/ once built successfully for the first time.usethis::use_testthat()
usethis::use_test("math.R")
test-math.R
) in the test/testthat
folder and add a simple test, e.g.:R
test_that("addition works", {
expect_error(add_numbers())
expect_equal(add_numbers(1, 1), 2)
})
devtools::test()
(Ctrl/Cmd
+ Shift
+ T
)testthat::auto_test_package()
devtools::check()
(Ctrl/Cmd
+ Shift
+ E
)devtools::install()
(Ctrl/Cmd
+ Shift
+ B
)devtools::install_github('USER/REPO')
(at this point other people can start using your package simply by installing it from GitHub with this simple command!)usethis::use_github_action_check_standard()
in your console. Copy the resulting badge code at the top of your README.md file.import
statements in the NAMESPACE
file are automatically generated from the documentation, in this case from the line #' @importFrom methods is
in the R/package.R
file. It is recommended to keep all your imports in one location, e.g. the R/package.R
file.#' @importFrom dplyr select filter bind_rows
to import the select
, filter
and bind_rows
functions from the dplyr
package. Note that it is also possible to import an entire packages with e.g. #' @import ggplot2
although not generally recommended since it makes it hard to figure out which functions your package actually needs.DESCRIPTION
file - this is for the purpose of R figuring out automatically what else to install if someone installs your packageDESCRIPTION
file, e.g. withR
Imports:
methods,
dplyr,
ggplot2
DESCRIPTION
file (as detailed above) but then call the functions directly via the package::function
syntax (e.g. dplyr::select
). That way they don't need to be imported by the NAMESPACE and you don't need to document with @importFrom
.@importFrom
some function you use a lot and call rare functions via package::function
)usethis::use_vignette("example")
(where example
is the file name for your vignette) and it will copy a vignette template in your vignettes
folder. You can open and knit this vignette to see what it looks like.LICENSE
file and corresponding entry in your DESCRIPTION
) by running, respectively:usethis::use_mit_license()
usethis::use_gpl3_license()
A linter is a tool that analyzes code to flag programming and stylistic errors. It is useful to check code for readability and general programming conventions. There is a good linter available that integrates into RStudio:
- install with install.packages("lintr")
- check that it is available in RStudio: Tools -> Addins -> Brows Addins... -> check that lintr
is listed to lint the current open file or package
- it's useful to hit the Keyboard shortcuts...
at the bottom of this page to define a shortcut to lint the current file, e.g. something like option
+ shift
+ L
devtools::install_github('USER/REPO')
(installs the master branch)devtools::install_github('USER/REPO', ref = "BRANCH_NAME|TAG|COMMIT#")
install.packages("PACKAGE")
commandAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.