How to Develop a Package in R

How to develop a package in R?

Consult the following reference material:

  1. Chapter 1, Creating R packages, of the Writing R extensions manual. This is the canonical source. It's the ultimate reference point, but not necessarily the best starting point.
  2. A short presentation outlining the key ideas in package development and using the devtools package for development
  3. Hadley's devtools wiki, particular the Package basics section.
  4. The R help for ?package.skeleton and ?create in devtools.
  5. The presentation by Uwe Ligges at useR!2010 on package development.
  6. R Packages by Hadley Wickham.

How to create custom start-up messages for R packages?

Your question isn't completely clear. Are you asking about how to create the ASCII art showing MCLUST, or how to display a startup message? I'll assume the latter.

You add a function like

.onAttach <- function(libname, pkgname) {
packageStartupMessage("This is version ", packageVersion(pkgname),
" of ", pkgname)
}

somewhere in your package code. By convention, you'd put this in a file called R/zzz.R, but it can go anywhere in the R code.

Don't print things other than errors or warnings from .onLoad(); that should normally be silent.

Creating a package in R using RStudio

Start with following the steps in this video:

Build an R Package in under 2 minutes with RStudio

Then read more about RStudio's Package Development feature, and also Hadley Wickam's Package basics.

R package documentation: link to a whole package, not function

You cannot link to the page that comes up when you click on the name of a package in RStudio's Packages pane. RStudio invisibly calls help(package = "<name>"), which renders the package's index.

From the R-exts manual:

The markup \link{foo} (usually in the combination \code{\link{foo}}) produces a hyperlink to the help for foo. Here foo is a topic, that is the argument of \alias markup in another Rd file (possibly in another package).

Hence \link and the equivalent Markdown markup supported by roxygen2 can only link to topics. A package index is not a topic in this sense because there is no corresponding Rd file.

It might be best just to remind users that they can use help to access the index of the package to which you are referring.

Adding Dataset in R Package

Alright, I can finally answer.

The issue is what is written in your src/Rhone.R file.
Here you are redefining Rhone. That's not what this file is supposed to contain.

Instead this file src/Rhone.R is supposed to contain documentation for the dataset Rhone.

#' Documentation for `Rhone` should reside here.
#'
#'
"Rhone"

Now, here's how I solved it.
First, open the package as a project in Rstudio.
Then (if you don't have it already) install {usethis} package via install.packages("usethis").

Then run usethis::use_data_raw("Rhone"). You can read the documentation of
this function, to know what it does:

> usethis::use_data_raw("Rhone")
√ Setting active project to
[REDACTED]
√ Creating 'data-raw/'
√ Adding '^data-raw$' to '.Rbuildignore'
√ Writing 'data-raw/Rhone.R'
* Modify 'data-raw/Rhone.R'
* Finish the data preparation script in 'data-raw/Rhone.R'
* Use `usethis::use_data()` to add prepared data to package

Now, go to the data-raw/Rhone.R file, and put the definition of `Rhone in there.

At the end, data-raw/Rhone.R should look like this:

## code to prepare `Rhone` dataset goes here

Rhone <-
c(
1355,
1492,
1692,
1766,
1903,
2040,
2177,
2314,
2451,
2588,
2725,
2862,
2999,
3136,
3273,
3410,
3547,
3686,
3822,
3959,
4096,
4233,
4370
)
usethis::use_data(Rhone, overwrite = TRUE)

Run/source this script. This now saves a file named Rhone.rda to the data/-folder.

Now, when you build your package, then this data-file is the only file that is
connected with the name Rhone. And not a redefinition of it, as what happened before.

If you change the definition of Rhone, then you have to re-run/re-source data-raw/Rhone.R. But you only have to do that if you change Rhone. And you only can change Rhone inside of data-raw/Rhone.R. Nowhere else is it possible to change Rhone.

If you build and load your package, then the code you used before will work.

> library(DEEVD)
> data(Rhone)
> Rhone
[1] 1355 1492 1692 1766 1903 2040 2177 2314 2451 2588
[11] 2725 2862 2999 3136 3273 3410 3547 3686 3822 3959
[21] 4096 4233 4370

How to make R package recommend a package hosted on GitHub?

From the manual (and quoting source here):

@c DESCRIPTION field Additional_repositories

The @samp{Additional_repositories} field is a comma-separated list of
repository URLs where the packages named in the other fields may be
found. It is currently used by @command{R CMD check} to check that the
packages can be found, at least as source packages (which can be
installed on any platform).

You can add the package to Suggests: and point to additional repositories -- possibly created using drat. There used to a package doing that, and IIRC there is another one doing it now but its name escaped me now.

Edit: Found it! See here in the source DESCRIPTION file of RNeXML -- and note how the line disappears in the posted DESCRIPTION on CRAN. Better still, note how two of the packages in Suggests: are not listed a hyperlinks on CRAN. I thinks those come from the additional repos. And yes, rOpenSci uses drat to manage that.

Edit 2: And just to close the loop, you (easily) use drat to host such an additional repo on GitHub -- the prime use case for drat.

Edit 3: RNeXML has dropped the additional repository, but the github history still has it.

Edit 4: Currently (i.e. on 2020-03-13), the CRAN packages EMC, bcmaps, blkbox, broom.mixed, epikit, grattan, gtsummary, hurricaneexposure, memoise, multinomialeq, noaastormevents, pointblank, provSummarize, provViz, spData, swephR, tashu, taxadb, waveformbildar all list a field Additional_repositories containing a URL pointing to a drat repo.



Related Topics



Leave a reply



Submit