R: develop R Package with bioconductor dependency (earlier solutions fail) - r

I'm developing an R package to put on cran and for one of the help pages/in the example section, there is need to load a Bioconductor data set.
As found here, the trick for automated installation of Bioconductor packages was to add biocViews: to the DESCRIPTION file, which does not work anymore.
Instead,
\examples{
\dontrun{
if (requireNamespace("GEOquery","Biobase")) {
myGEO <- GEOquery::getGEO("GDS3143")
eset <- GEOquery::GDS2eSet(myGEO)
xpr <- na.omit(data.frame(Biobase::exprs(eset)))
dose <- c(rep(0,10),rep(0.5,4),rep(1,8),rep(5,4),rep(10,9),rep(30,5),rep(50,4))
plot(dose, xpr[78,], col=as.factor(dose), lwd=2, ylab ="expression")
}
}}
"works", but is obviously not the appropriate way to proceed. (the :: is used to prevent the Bioconductor packages masking existing R functions)
Is there another way to automatically install Bioconductor packages upon (my) package installation? (some modification of DESCRIPTION file & NAMESPACE?)

Related

Package YieldCurve

My question is about package YieldCurve of R.
The R YieldCurve package used for modeling yield curves with parametric models like Nelson Siegel and Svensson has been removed from rcran, so it is not possible to download it in RStudio.
Is there a way to load it from another repository?
Is there a package that replaces it and allows to do the same thing that YieldCurve did?
Especially if packages are recently archived, they can usually be installed from source without a problem.
I've been meaning to write this helper function for a little while:
go to CRAN archive page for the package
find the most recent/last version of the package (I'm not sure this will work perfectly if the package versions are such that they are sorted inconsistently in alphabetical order ...)
construct the location of the 'tarball' (compressed source archive)
install
If the package has compiled components you'll need to have development tools installed.
This is very much like remotes::install_version() but (1) it finds the latest archived version automatically (2) you don't need to install the remotes package.
install_last_archived <- function(pkg, verbose = TRUE) {
arch_url <- "https://cran.r-project.org/src/contrib/Archive/"
rr <- readLines(paste0(arch_url, pkg))
last <- tail(rr[grepl(pkg,rr)],1)
tarball <- gsub(sprintf(".*(%s_[0-9.]*\\.tar\\.gz).*", pkg), "\\1", last)
if (verbose) cat("installing ", tarball, "\n")
install.packages(paste0(arch_url, pkg, "/", tarball), repos = NULL)
}
install_last_archived("YieldCurve")
Possible enhancements: (1) work harder to make sure we have the most recent version (check dates explicitly?) (2) extract the DESCRIPTION file to see if the package has compiled components, if so then warn user ...

How do I conditionally use R packages in help file examples?

I'm an R package author, and I received a notice form CRAN that my package was going to be archived because the packages in my Suggests need to be used conditionally. In my main code, they are used conditionally; I wrote a check.packages() function that throws an error if the package is not able to be loaded. In the examples in my Help files, though, I simply attach the package with library(), which is likely where I made my mistake. The problem arose because a package I included in Suggests was archived.
My question is how do I conditionally use packages in examples in Help files? I tried using
ifelse{\Sexpr{isTRUE(requireNamespace("pkg"))}}{
#Example with pkg
}{
\dontrun{
#Example with pkg
}}
that is, if the package is loadable, display the example as usual, and if not, wrap \dontrun{} around it. This worked when I previewed the Help file, but the CRAN check tells me I can't use \ifelse in examples. If it's important that my examples attach packages in my Suggests, is my only option to wrap \dontrun around all of them, regardless of whether the package can be loaded?
CRAN were happy with a simple if statement in the vignette of one of my packages:
if (requireNamespace('pkg', quietly = TRUE)) {
library('pkg')
# Example with pkg
} else {
message("'pkg' not available")
}
I could be missing something, but I can't see why this approach shouldn't work in examples too; perhaps you would need to add an unloadNamespace('pkg') after the example to clean up?

R package choroplethr says use zip_choroplethr instead of zip_map, but function not available

I did install.packages("choroplethr"), followed by library(choroplethr). I want to find out how to do a zip code choropleth, so I start typing in RStudio,
"?choroplethr::zip..." The only function that RStudio finds is zip_map. I go to its help file and see the following documentation:
This function is deprecated as of choroplethr version 3.0.0. Please
use ?zip_choropleth instead. The last version of choroplethr in which
this function worked was version 2.1.1, which can be downloaded from
CRAN here:
http://cran.r-project.org/web/packages/choroplethr/index.html
Okay, I guess I'll find out about this zip_choroplethr function then.
?choroplethr::zip_choroplethr
# No documentation for ‘zip_choroplethr’ in specified packages and libraries:
# you could try ‘??zip_choroplethr’
Wut.
Thank you for using choroplethr.
zip_map is, indeed, deprecated. It used scatterplots, which wasn't the best way to visualize zip codes, especially because they are so small.
Within choroplethr, Zip code choropleths are managed by a new, separate package: choroplethrZip. You can see the installation instructions and documentation here.
CRAN rejected choroplethrZip due to the size of the map, which is why it is in separate package and on github.

Making an R package PDF manual using devtools

I am making an R package using devtools and roxygen2. I can get a PDF manual using R CMD but I am really curious as to whether this can be done using devtools. devtools' build(), check(), install() all don't make a PDF manual. Is this tied to making vignettes?
I have read and referred to a similar thread Package development : location of pdf manual and vignette
After you install it, you can use:
pack <- "name_of_your_package"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),
"CMD", "Rd2pdf", shQuote(path)))
There is
devtools::build_manual()
Maybe also
devtools::check(manual=TRUE)
could work.
For the PDF manual of one specific function, you can run
fun <- "name_of_function"
help(fun, package = "name_of_package", help_type = "pdf")
system(paste0("open ", fun, ".pdf"))
assuming you have the package installed.

Check for installed packages in R

Based on the answer to this question: Elegant way to check for missing packages and install them?
I'm using the following code to make sure that all packages are installed when I upgrade R, or set up other users:
list.of.packages <- c("RODBC", "reshape2", "plyr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
I've placed it in my .First function in my .Rprofile, but when I start up R it gives the following error and continues starting up:
Error in match(x, table, nomatch = 0L) :
could not find function "installed.packages"
If I run the code after I get a prompt it works fine. Any ideas why?
Thanks!
It appears from reading ?Startup that:
Next, if a function .First is found on the search path, it is executed
as .First(). Finally, function .First.sys() in the base package is
run. This calls require to attach the default packages specified by
options("defaultPackages").
Now, installed.packages is in the utils package, which is typically one of the default packages. So it's not available at the time .First is called.
Perhaps try replacing installed.packages with utils::installed.packages?
As Josh notes below my eyes skimmed over the piece that addresses this issue directly, namely:
Note that when the site and user profile files are sourced only the
base package is loaded, so objects in other packages need to be
referred to by e.g. utils::dump.frames or after explicitly loading the
package concerned.

Resources