Travis builds fail on dplyr vector calls within vignette - r

When I push my package to travis the vignette checks fail. I'm aware I can skip it but it's bugging me.
I have identified at least one of the issues belongs to a specific call in my code.
The RMD chunk in question:
get_storm_data("fstadv", link = al.1998.charley %>% .$Link)
al.1998.charley:
al.1998.charley <- structure(list(Year = 1998,
Name = "TROPICAL STORM CHARLEY",
Basin = "AL",
Link = "http://www.nhc.noaa.gov/archive/1998/1998CHARLEYadv.html"),
class = "data.frame",
row.names = c(NA, -1L),
.Names = c("Year", "Name", "Basin", "Link"))
The link parameter is a character string sent to the function get_storm_data which does some web scraping.
The following three values are identical:
a <- al.1998.charley %>% .$Link
b <- al.1998.charley %>% `[[`('Link')
c <- "http://www.nhc.noaa.gov/archive/1998/1998CHARLEYadv.html"
identical(a, b)
[1] TRUE
identical(b, c)
[1] TRUE
However, only the value c passed to the link parameter will pass travis.
The travis error:
Building with: R CMD build
6.09s$ R CMD build .
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘Hurricanes’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Quitting from lines 86-87 (getting-started.Rmd)
Error: processing vignette 'getting-started.Rmd' failed with diagnostics:
is.character(url) is not TRUE
Execution halted
All of my previous attempts to resolve this can be found here, if necessary.
Edit
This vignette never caused issues before. Build 16 passed. Nothing was different in the vignette. I did git diff between that commit and HEAD to bring the branch to it's exact match (since original branch had since been deleted). Other than cosmetic differences in other files (spaces, newlines, etc.), it still failed.
This led me to believe it was an upgrade or change on travis' end. I read over the blog but saw no related changes between the last successful pass (Build 16) and current.
Final Edit
When I mentioned in the last edit the vignette had not changed since the last succesful build, I should have been more clear. That entire commit that passed in March would not pass today. I'm still not clear why.
I would like to point out for any newcomers: check the travis packages to make sure they match your system. R CMD build on my system passed with no issues. But some of my packages were outdated compared to travis'. Thanks to #jimhester (GitHub) for pointing that out.
Session Info
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
_LC_CTYPE=en_US.UTF-8_, _LC_NUMERIC=C_, _LC_TIME=en_US.UTF-8_, _LC_COLLATE=en_US.UTF-8_, _LC_MONETARY=en_US.UTF-8_, _LC_MESSAGES=en_US.UTF-8_, _LC_PAPER=en_US.UTF-8_, _LC_NAME=C_, _LC_ADDRESS=C_, _LC_TELEPHONE=C_, _LC_MEASUREMENT=en_US.UTF-8_ and _LC_IDENTIFICATION=C_
attached base packages:
stats, graphics, grDevices, utils, datasets, methods and base
other attached packages:
Hurricanes(v.0.1.0), dplyr(v.0.5.0), purrr(v.0.2.2), readr(v.1.0.0), tidyr(v.0.6.1), tibble(v.1.2), ggplot2(v.2.2.1), tidyverse(v.1.0.0) and magrittr(v.1.5)
loaded via a namespace (and not attached):
Rcpp(v.0.12.7), digest(v.0.6.10), assertthat(v.0.1), R6(v.2.2.0), grid(v.3.4.0), plyr(v.1.8.4), DBI(v.0.5-1), gtable(v.0.2.0), scales(v.0.4.1), lazyeval(v.0.2.0), data.table(v.1.10.4), tools(v.3.4.0), pander(v.0.6.0), munsell(v.0.4.3), compiler(v.3.4.0) and colorspace(v.1.3-0)

Link in al.1998.charley is stored as a factor on Travis (perhaps you have set the stringsAsFactors option to FALSE somewhere locally?). httr won't coerce a factor vector to character, hence you get an error. Try wrapping as.character() around al.1998.charley %>% .$Link.

Related

Index error when running maxnet function (maxnet package)

I use the maxnet function (maxnet package) as one of the model algorithms in an ensemble model. Sometimes, the code executes without an error. Other times, it gives me the error message you see below. I am working on a windows 10 Pro (R version 3.6.1, Rstudio version 1.2.5042).
Code:
dm.Maxent <- maxnet(p = train$species, data = train[-train$species],
maxnet.formula(p = train$species,
data = train[-train$species],
classes = "default"))
Error:
Error in intI(j, n = x#Dim[2], dn[[2]], give.dn = FALSE) :
index larger than maximal 185
train is a dataframe with 621 rows (one row for every occurrence/absence point), and 29 columns (28 columns containing variables and 1 column "species" that indicates presence or absence of the species (0/1)).
I am having the same issue. It is unpredictable, since for several species it ran fine, then out of a sudden it stopped.
I found a response on this link: https://github.com/jamiemkass/ENMeval/issues/62
In the new version of maxnet (check the Github repo, as it looks like the CRAN version gas not been updated yet), there is a new argument "addsamplestobackground". When set to TRUE, it solves some of these errors. Currently, you will have to use install_github to reinstall maxnet to use this argument. Once you do, install_github to get the dev branch version of ENMeval (v2), which will implement this by default. Hopefully that fixes these problems.
I reinstalled maxnet from github :
install.packages("remotes")
remotes::install_github("mrmaxent/maxnet")
and set addsamplestobackground = T Maybe this would help you.

How to quickly replicate/update local library under $R_LIBS_USER?

Suppose that
the version of R I have installed is 3.3.1;
my environment variable $R_LIBS_USER is set to $HOME/my/R_lib/%V; and
I have a directory $HOME/my/R_lib/3.3.1 containing a large number of packages I've installed over time.
Now I want to upgrade my version of R, to 3.4.1, say.
I'm looking for a convenient way to install a new collection of packages under the new directory $HOME/my/R_lib/3.4.1 that is the "version-3.4.1-equivalent" of the library I currently have under $HOME/my/R_lib/3.3.1.
(IOW, I'm looking for a functionality similar to what one can do with Python's pip installer's "freeze" option, which, essentially, produces the input one would have to give the installer in the future to reproduce the current installation.)
You can use function installed.packages for that purpose. Namely:
installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")
The returned object contains a lot of informations (most fields from each package DESCRIPTION file) but the names of the packages are in the first column. So something like the following should do the trick:
inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")
install.packages(inst[,1], lib="$HOME/my/R_lib/3.4.1", dependencies=FALSE)
To answer the additional question in the comments:
If your old library contained packages from other sources than CRAN, you will have to do some gymnastic based on the content of the DESCRIPTION files, and thus will depend on how well the package author documented it.
Argument field in installed.packages allows you to select some additional fields from those files. Fields of interest to determine the source of a package are fields Repository, URL and Maintainer. Here are some ideas on how to split them apart:
CRAN vs non-CRAN:
inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1",
fields=c("URL","Repository","Maintainer"))
inst <- as.data.frame(inst, row.names=NA, stringsAsFactors=FALSE)
cran <- inst[inst$Repository%in%"CRAN",]
non_cran <- inst[!inst$Repository%in%"CRAN" & !inst$Priority%in%"base",]
Bioconductor packages:
bioc <- inst[grepl("Bioconductor",inst$Maintainer),]
source("https://bioconductor.org/biocLite.R")
biocLite(pkgs=bioc$Packages)
Github packages:
git <- non_cran[grepl("github", non_cran$URL),]
install.packages("devtools")
library(devtools)
for(i in seq(nrow(git))){
install_github(repo=gsub("http://github.com/","",git$URL[i]))
}

Determine version of a specific package

How can I get the version number for a specific package?
The obvious way is to get the dictionary with all installed packages, and then filter for the one of interest:
pkgs = Pkg.installed();
pkgs["Datetime"]
Getting the list of all installed packages is very slow though, especially if there are many packages.
EDIT: For Julia version 1.1+
Use the Pkg REPL notation:
] status # Show every installed package version
] status pkgName # Show the specific version of the package
] status pkgName1 pkgName2 # Show the named packages. You can continue the list.
The ] enters the Pkg REPL, so you basically write status ...
So in your case, write after entering the Pkg REPL:
status DataFrame
Or use the object-oriented approach (NB: Here you don't enter the Pkg REPL, i.e. DON'T use ]:
Pkg.status("DataFrame")
EDIT: For Julia version 1.0
Pkg.installed seems to have "regressed" with the new package system. There are no arguments for Pkg.installed. So, the OP's original method seems to be about the best you can do at the moment.
pkgs = Pkg.installed();
pkgs["Datetime"]
EDIT: For Julia version upto 0.6.4
You can pass a string to Pkg.installed. For example:
pkgs = Pkg.installed("JuMP")
I often check available calling arguments with methods. For example:
julia> methods(Pkg.installed)
# 2 methods for generic function "installed":
installed() at pkg/pkg.jl:122
installed(pkg::AbstractString) at pkg/pkg.jl:129
or
julia> Pkg.installed |> methods
# 2 methods for generic function "installed":
installed() at pkg/pkg.jl:122
installed(pkg::AbstractString) at pkg/pkg.jl:129
I would try Pkg.status("PackageName")
This will print out a little blurb giving the package name.
Here is an example
julia> Pkg.status("QuantEcon")
- QuantEcon 0.0.1 master
In Julia 1.1 you can use
(v1.1) pkg> status "name_of_the_package"
to find the version of any package in a given environment.
In order to look of a version of an indirectly included package (e.g. top-level project includes Module A which depends on Module B, where you need to know info about Module B), you have to pull the info either from the Manifest.toml directly, or you have to bring in the Context object in from Pkg.
Below is done with Julia 1.3.1 ... there may be changes to Pkg's internals since then.
using Pkg
ctx = Pkg.Operations.Context()
# Get the version of CSV.jl
version = ctx.env.manifest[UUID("336ed68f-0bac-5ca0-87d4-7b16caf5d00b")].version
if version <= v"0.5.24"
# handle some uniqueness about the specific version of CSV.jl here
end
UPDATE: Or without a UUID and just the package name (thanks #HHFox):
using Pkg
pkg_name = "Observables"
m = Pkg.Operations.Context().env.manifest
v = m[findfirst(v->v.name == pkg_name, m)].version
or to do the same with the Manifest.toml
using Pkg
# given the path to the Manifest.toml file...
manifest_dict = Pkg.TOML.parsefile(manifest_path)
# look for a named package like `CSV`
package_dict = manifest_dict[package_name][1]
#show package_dict
Well this didn't print well in the comment section...
Here is a version that matches the name rather than the UUID
using Pkg
m = Pkg.Operations.Context().env.manifest
v = m[findfirst(v -> v.name == "CSV", m)].version
For packages which are dependencies of the specified packages in the project file one can use status -m <packageName> or shorter st -m <packageName> in package mode (After ]`).
For a full list, just use st -m.
This is an extension to https://stackoverflow.com/a/25641957.

How to make devtools::test() consider package dependencies

Actual question
Seems like devtools::test() does not make sure that package dependencies as stated in a package's DESCRIPTION file are loaded prior to running the unit tests. How can I change that?
Details
I'm writing a package (B) that imports another one of my packages (A).
When I try to run my unit tests via devtools::test(), or, to be more precise via the shortcut SHFT + CRTL + T in RStudio, certain tests fail as the imported package seems to be disregarded/not loaded and thus a certain function (isPackageInstalled) can't be found.
Trying to load the imported package A manually before running devtools::test() didn't help either. I guess that's due to the fact that devtools (or testthat) "simulates" a fresh workspace state? Running the unit tests "one by one" works just fine after manually loading package A beforehand, though.
I thought that devtools would look up package dependencies in the DESCRIPTION file of B and thus load them as would be the case when running require("B"), but apparently not.
Here's my DESCRIPTION file:
Package: B
Type: Package
Title: What the package does (short line)
Version: 0.1.0.1
Date: 2014-08-05
Author: Who wrote it
Maintainer: Who to complain to <yourfault#somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports: A
Here's the code I ran:
devtools::load_all() # or SHFT + CTRL + L in RStudio
devtools::test() # or SHFT + CTRL + T in RStudio
That's what RStudio's build pane gave me:
==> devtools::test()
Loading required package: testthat
Testing B
Loading B
Creating a new generic function for 'signalCondition' in package 'B'
package : 1
package : ......
1. Error: getPackageDescription ------------------------------------------------
could not find function "isPackageInstalled"
1: expect_is(res <- getPackageDescription(), expected) at test-getPackageDescription.r:13
2: expect_that(object, is_a(class), info, label)
3: condition(object)
4: paste0(class(x), collapse = ", ")
5: getPackageDescription()
6: getPackageDescription() at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:37
7: getPackageDescription(from = from, fields = fields, drop = drop, encoding = encoding,
...) at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:154
8: getPackageDescription(from = from, fields = fields, drop = drop, encoding = encoding,
...) at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:37
Am I missing something here?
Screenshot of build tools dialogue:
The usual approach would be to use roxygen2 to automatically generate your NAMESPACE file from special comments in your source code, but maintain your DESCRIPTION file manually. There's no special stuff that I'm aware of to keep them in sync, but R CMD CHECK will tell you if there's something missing/extra in your DESCRIPTION.

Check if R is running in RStudio

I am looking for a way to test if R is being run from RStudio. For some reason I could find the answer on google yesterday but not today, but I think it had to do with testing if a certain system variable was set.
This is from ?rstudio:
# Test whether running under RStudio
isRStudio <- Sys.getenv("RSTUDIO") == "1"
There is also rstudioapi::isAvailable(), but checking this is not as reliable because RStudio doesn't seem to really need the rstudioapi package to work correctly.
Check the .Platform$GUI option for "RStudio"
is.rstudio = function(){
.Platform$GUI == "RStudio"
}
See:
http://thecoatlessprofessor.com/programming/detecting-if-r-is-in-rstudio-and-changing-rstudios-default-graphing-device/
There is no "running inside RStudio". RStudio is merely an IDE layer that wraps around R; at the end of the day it just launches the normal R executable you need to have on your $PATH anyway to operate RStudio.
As a proxy, and as R Studio You could test available.packages() for the 'manipulate' package though, or as a shorter version see if RStudio added itself to the .libPaths() content:
R> any(grepl("RStudio", .libPaths()))
[1] TRUE
R>
R>
Edit in May 2020 or eight years later The question does come up, and one can query a variety of things from within. Here is an example from the terminal of RStudio:
$ env | grep -i rstudio | sort
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/rstudio.desktop
PATH=[...redacted...]
RMARKDOWN_MATHJAX_PATH=/usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH=/usr/lib/rstudio/bin/rpostback
RSTUDIO=1
RSTUDIO_CONSOLE_COLOR=256
RSTUDIO_CONSOLE_WIDTH=111
RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc
RSTUDIO_PROGRAM_MODE=desktop
RSTUDIO_PROJ_NAME=chshli
RSTUDIO_SESSION_ID=9C62D3D4
RSTUDIO_SESSION_PORT=13494
RSTUDIO_TERM=2BD6BB88
RSTUDIO_USER_IDENTITY=edd
RSTUDIO_WINUTILS=bin/winutils
$
Similarly, from within the R session:
R> se <- Sys.getenv()
R> se[grepl("rstudio",se,ignore.case=TRUE)]
GIO_LAUNCHED_DESKTOP_FILE /usr/share/applications/rstudio.desktop
PATH [...also redacted...]
RMARKDOWN_MATHJAX_PATH /usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH /usr/lib/rstudio/bin/rpostback
RSTUDIO_PANDOC /usr/lib/rstudio/bin/pandoc
R>
Edit in Aug 2021 or nine years later As all the answers listed here in the different answer may still be too much for people, you can also install package rstudioapi from CRAN and then ask it via rstudioapi::isAvailable() which comes back TRUE for me inside RStudio and FALSE in ESS / standard R.
When I start RStudio it seems to have tools:rstudio in position 2 on the search path. This has a function "RStudio.version" which is undocumented but seems to return the RStudio version string:
> RStudio.version()
[1] "0.96.316"
So you can define:
is.RStudio <- function(){
if(!exists("RStudio.version"))return(FALSE)
if(!is.function(RStudio.version))return(FALSE)
return(TRUE)
}
and maybe use that.
As of today, there are a few packages which include functions to check whether RStudio is running:
rstudioapi::isAvailable()
assertive::is_rstudio()
(list is non-exhaustive)
The assertive and assertive.reflections packages, resp., do include additional functions to check for other IDEs, desktop/server versions of RStudio, and various R releases (e.g., alpha, beta, devel, release, patched, etc.)
To add to the number of nice guesses, here is a message from 2011 (Ice Age)
http://support.rstudio.org/help/discussions/problems/413-location-of-installed-packages
if (Sys.getenv("RSTUDIO_USER_IDENTITY")!= ""){
.libPaths(.Library) # Avoid additional libraries } else { # not rstudio ...
I find the following works for me
checkRstudio <- function () {
return ("tools:rstudio" %in% search())
}
I am sort of new to R myself, but I believe Rstudio necessarily loads the
package "tools:rstudio" in order to run.
Neat solution is now available through the startup package via the is_rstudio_console function:
startup:::is_rstudio_console()
[1] TRUE
It may be worth adding that this function checks for two environment variables, RSTUDIO, which was mentioned in the answer by #krlmr and RSTUDIO_TERM that doesn't seem to be mentioned across the preceding answers at the moment.
function ()
{
(Sys.getenv("RSTUDIO") == "1") && !nzchar(Sys.getenv("RSTUDIO_TERM"))
}
The most reliable way to detect whether code is running in the main RStudio R session (without relying on the rstudioapi package) is:
commandArgs()[[1L]] == "RStudio"
Some comments on other answers:
Checking environment variables may be insufficient, as the "RSTUDIO" environment variable will also be inherited by child R processes launched from the main R session.
RStudio does not initialize its R infrastructure until after R profile scripts are run (e.g. ~/.Rprofile), so checking things like .Platform$GUI == "RStudio" or "tools:rstudio" %in% search() or rstudioapi::isAvailable() won't give what you expect in that context.
On a Mac only the Sys.getenv answer works
platform x86_64-apple-darwin10.8.0
version.string R version 3.1.0 (2014-04-10)
Sys.getenv("RSTUDIO")=="1"
[1] TRUE
RStudio.version()
Error: could not find function "RStudio.version"
any(grepl("RStudio", .libPaths()))
[1] FALSE
.libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library"

Resources