package.Rproj
to load the project into RStudioinstall.packages(c("devtools", "testthat"))
devtools::create_description()
DESCRIPTION
file (note, no spaces or -
allowed).R
) file in the R
folder and write a simple function, e.g.:#' 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 themimport
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
)devtools::use_testthat()
.R
) file in the test/testthat
folder and write a simple test, e.g.:R test_that("Math works", { expect_error(add_numbers()) expect_equal(add_numbers(1, 1), 2) })
devtools::test()
(Ctrl/Cmd
+ Shift
+ T
)testthat::auto_test_package()
LICENSE
file and corresponding entry in your DESCRIPTION
) by running, respectively:
devtools::use_mit_license()
devtools::use_gpl3_license()
devtools::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.devtools::check()
(Ctrl/Cmd
+ Shift
+ E
)devtools::install()
(Ctrl/Cmd
+ Shift
+ B
)devtools::install_github('USER/REPO')
?
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
(install with install.packages("pkgdown")
)pkgdown::init_site()
once to initalize the documentation folder structure (always created in the docs
directory)pkgdown::build_site()
to build your site (it will also preview it automatically)pkgdown
is extremely customizable and you can specify how your page should be structured and styled using the docs/pgkdown.yml
file. For details, check out the pgkdown website (incidentally, generated by pkgdown
…)docs
folder to GitHub and go to your repository Settings –> GitHub Pages –> Source: select master branch/docs folder
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 devtools::install_github("jimhester/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::use_travis()
to set up your package for automated continuous testing on Travis (creates a .travis.yml
configuration file that you can modify for further customization of the testing)devtools::use_appveyor()
to set up your package for automated continuous testing on AppVeyor (creates a appveyor.yml
configuration file that you can modify for further customization of the testing)devtools::install_github('USER/REPO')
(installs the master branch)devtools::install_github('USER/REPO', ref = "BRANCH_NAME|TAG|COMMIT#")
install.packages("PACKAGE")
command