How can I install packages in knitr? - r

Till now, I was using this chunk of code to load R packages and write .R files. But I am trying to use knitr
rm (list=ls(all=TRUE))
kpacks <- c('ggplot2','install_github','devtools','mapdata')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
options(max.print=5.5E5)
But now, when I put this chunk of code in a Knitr document, I get this error:
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror calls:......
How can I fix this?

The narrow answer to your question is that you should set your repos option:
options(repos=c(CRAN="<something sensible near you>"))
You're hitting the problem because R's default behaviour when the repository option is initially unset is to query the user -- and it can't do that when you're running code non-interactively.
More broadly, I would question whether you want to include this sort of thing in your R code; under some circumstances it can be problematic.
what if the user doesn't have a network connection?
what if they are geographically very far from you so that your default repository setting doesn't make sense?
what if they don't feel like downloading and installing a (possibly large) package?
My preferred practice is to specify in the instructions for running the code that users should have packages X, Y, Z installed (and giving them example code to install them, in case they're inexperienced with R).

One way to avoid installing the packages is to do something like
if(!require(package.name))
stop("you need to install package.name")
In your code chunk. Depending on your knitr document settings, this will generate the message in the document, in the console, or prevent the document from being knitted.

Related

Avoid being asked for CRAN mirror?

When running R from the terminal, we sometimes see
--- Please select a CRAN mirror for use in this session ---
Is there a universal, efficient, and memorable way to install.packages("example") without needing to memorise urls or interact with a dialogue box?
That is, is there a way to install an R package which is:
universal (works on any/all installations of R, irrespective of operating system)
Requires no memorisation of url(s)
Requires no interaction with duologue box(es)
Requires no files (e.g. .Rprofile) to be created or edited
Lastly, ideally, a method which is short and memorable (or easy use without having to look it up).
Here's a pseudo code example of an ideal solution (where 'force' is pseudocode for 'choose the most obvious defaults and press on at all costs!)
force(install.packages("example"))
If you run help("install.packages"), you can see that the default value for the repository is repos = getOption("repos"). If you follow this trail to help("getOption"), it provides some more insight. Here is what it says for the repos option.
repos:
URLs of the repositories for use by update.packages. Defaults
to c(CRAN="#CRAN#"), a value that causes some utilities to prompt for
a CRAN mirror. To avoid this do set the CRAN mirror, by something like
local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran";
options(repos = r)}).
You can see this by going into the 'etc/' subdirectory of your R installation. There is a file there called 'repositories'. While some other repos (e.g., R-Forge) have default URLs, CRAN does not. It shows #CRAN# as referenced by the help file.
The R documentation advises you that some utilities, such as what you are experiencing at the command line, will prompt you for a mirror, unless the option is explicitly set. The documentation does not indicate an alternative work around.
The reason why there cannot be a function to tell it to use the "most obvious default" is that there is actually no default. So a method such as your hypothetical force() would not be possible.
An edit with a bit more info:
You can use a few helpers from utils to set the repos option. I am not sure if it is easy enough to remember for your criteria, but there is chooseCRANmirror() and getCRANmirrors().
# this should work
chooseCRANmirror(ind = 1)
install.packages("example")
# or this clunky approach
install.packages("example", repos = getCRANmirrors()[1,"URL"])
But honestly at that point you may be better off just remembering repos = https://cloud.r-project.org/.

How to get rid of a size warning in R for package development

I am working on submitting an R package and when I run
devtools::check()
I get the following warning:
W checking sizes of PDF files under ‘inst/doc’ (1.6s)
‘gs+qpdf’ made some significant size reductions:
compacted ‘Vignette1.pdf’ from 544Kb to 256Kb
compacted ‘Vignette2.pdf’ from 328Kb to 69Kb
consider running tools::compactPDF(gs_quality = "ebook") on these files
I understand that the size of the PDFs are the issue, and that they need to be resized, but can anyone explain to me where I need to place tools::compactPDF(gs_quality = "ebook") in my code so that when the vignettes are created this is not an issue. You cannot submit an R package that throws a warning so I need to come up with a solution to fix this.
If you do R CMD build --help you will see the two lines
--compact-vignettes= try to compact PDF files under inst/doc:
"no" (default), "qpdf", "gs", "gs+qpdf", "both"
Per recent discussion on the mailing lists, you may want arguments gs+qpdf or both. You may want to experiment with both. Use of the option should render a package that does not trigger the NOTE.
And I presume there is a way to pass that onto devtools as well but I am not familiar with its functions so I can't give you a direct pointer.
As of devtools 2.4.5
devtools::check(build_args = "--compact-vignettes=gs+qpdf")
is the correct parameterization. But note that for building, the corresponding call is now
devtools::build(args = "--compact-vignettes=gs+qpdf")
Note that in neither case do you quote the argument passed down to R CMD BUILD's --compact-vignettes

Additional information to ensure R code will run on another computer?

sessionInfo() includes very useful info that will improve the chances of someone being able to run your code on their machine, including
OS and version
R version
Attached packages
What other info can be provided with an R script to ensure someone else will be able to run it in their environment?
NB please include how to get that info (i.e. what command to run or where to look for it)
While this is not a complete answer, I tend to include this function with scripts I send along as it will download a package if the computer does not have it. This is more of a suggestion for scripts. For packages, you can explicitly put what versions of other packages your package depends on.
package_load<-function(packages = NULL, quiet=TRUE,
verbose=FALSE, warn.conflicts=FALSE){
# download required packages if they're not already
pkgsToDownload<- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(pkgsToDownload)>0)
install.packages(pkgsToDownload, repos="http://cran.us.r-project.org",
quiet=quiet, verbose=verbose)
# then load them
for(i in 1:length(packages))
require(packages[i], character.only=T, quietly=quiet,
warn.conflicts=warn.conflicts)
}
## Example of use
package_load(c('dplyr', 'rgdal'))
This is helpful for one off scripts as it gets over the hurdle of a different computer not having the appropriate packages. However, I generally suggest to folks to make sure their version of R is up to date as well.
Is this the best solution? Probably not, but it does help with minor scripts you send along to others. For a larger code base, it would probably be better to put together a package or a docker image.
I think the criterion you listed are the "basics" of reusability of a script. The next levels would be the possible interaction of your scripts (e.g. R Shiny scripts will use web features: therefore, giving the web browser and its version used to produce the script is a good practice). Also, another kind of information would be commentaries precising the expected input and outputs.
NB: I would precise "attached packages and their versions", just for us to be sure...

Clear R environment of all objetcs & packages

I tried this command
remove(list = ls())
I expect to clear all R environment (Objects, packages)
The simplest and, more importantly, the only reliable way of doing this is to restart R. That takes care of everything.
Just make sure you’re not accidentally saving the current R image when quitting R.
In RStudio, you need to set the option “Save workspace to .RData file on exit” to “Never”, and disable restoring upon restart — this is strongly recommended!
After that, make sure that any previously existing .RData files in your project’s folder are deleted (heads up: .RData is an invisible file so you won’t normally see it in a file browser; you can delete it via the command line).
To restart R from within RStudio, you can use “Session” › “Restart R” or Cmd+Shift+F10.
The answer was already out there :-) https://stackoverflow.com/a/7506112/7902133
According to this answer, the following code should work
lapply(paste("package:", names(sessionInfo()$otherPkgs), sep=""),
detach,
character.only = TRUE,
unload = TRUE)
You may also want to check the first answer for a full description.
The freshr package has consolidated the previous answers into one simple function. Install it via
install.packages("freshr")
and then run
freshr::freshr()
in your console and it will unload all packages and variables for you.

Can I load a package's data set without installing the package?

In package ISLR, there is a data set called Default.
I want to use that data set, but the ISLR package is not installed on my machine.
data(Default)
# Warning message:
# In data(Default) : data set ‘Default’ not found
library(ISLR)
# Error in library(ISLR) : there is no package called ‘ISLR’
Since I'll probably never use it again, I don't want to install the package. I thought about reading it from the web, but it's not in the linked web page from the package description.
In general, is there a way to load a data set from a package without installing the package?
You can do this from within R:
download.file("http://cran.r-project.org/src/contrib/ISLR_1.0.tar.gz",
dest="ISLR.tar.gz")
untar("ISLR.tar.gz",files="ISLR/data/Default.rda")
L <- load("ISLR/data/Default.rda")
summary(Default)
If you want to keep a copy of the data file:
file.copy("ISLR/data/Default.rda",".")
Clean up:
unlink(c("ISLR.tar.gz","ISLR"),recursive=TRUE)
I'm not sure you can get around having to download the tarball -- in principle you might be able to run untar() directly on a network connection, but I don't think the underlying machinery can actually extract a file without downloading the whole tarball to somewhere on your machine first.
You said, "Since I'll probably never use it again, I don't want to install the package." If the fact that you'll never use it again is your main concern, then perhaps this solution is not quite what you want, but it is probably the simplest solution:
Install the package with install.packages().
Extract and save the dataset that you want.
Uninstall the package with remove.packages().
So the final result is what you want in three simple steps, though the process does involve installing the package, which you hoped to avoid. But you end up without the package in your system that you don't want, so the end result is the same as what you want.

Resources