Solution see comments, "After updating my version of R, and restarting the sever, this issue is no longer present. May it never return. There was no issue with scope or local/global environments, just some bug. Thanks @MrFlick for pointing me in the right direction."
I am curious if folk can explain (or point to good, new-programmer-friendly documentation or text) to help me probably incorporate tidyr (and plyr, dplyr, and other Hadleyverse packages) into custom functions. The issue below clearly has a scope/local environment issue I don't fully grasp.
Thanks in advance!
Reproducible Examples
messy <- data.frame(
name = c("Wilbur", "Petunia", "Gregory"),
a = c(67, 80, 64),
b = c(56, 90, 50)
)
Outside of a custom function, here is a use of tidyr:
library(tidyr); library(dplyr)
messy %>%
gather(drug, heartrate, a:b) %>%
dplyr::arrange(desc(drug))
messy %>%
gather(drug, heartrate, 2:do.call("ncol",list(messy))) %>%
dplyr::arrange(desc(drug))
Both return:
name drug heartrate 1 Wilbur b 56 2 Petunia b 90 3 Gregory b 50 4 Wilbur a 67 5 Petunia a 80 6 Gregory a 64
These types of operations are obviously quite useful in larger more complex custom functions, but I encounter the following issue:
foo1 <- function(DF){
DF %>%
gather(drug, heartrate, 2:ncol(DF)) %>%
dplyr::arrange(desc(drug))
}
foo2 <- function(DF){
DF %>%
gather(drug, heartrate, 2:do.call(ncol,list(DF))) %>%
dplyr::arrange(desc(drug))
}
foo1(messy)
Error in ncol(DF) : object 'DF' not found
foo2(messy)
Error in do.call(ncol, list(DF)) : object 'DF' not found
The do.call version in foo2 is motivated by a similar recent stackoverflow post that wasn't all that helpful in the end.
Bad Workarounds
One horrible solution that I've been using is to create a global variable
fooLISH <- function(DF){
NCOLS <<- ncol(DF)
DF %>%
gather(drug, heartrate, 2:NCOLS) %>%
dplyr::arrange(desc(drug))
}
Or to pass an additional argument into the custom function.
But I would love to use this question as a teaching moment for me and others who have run up to this problem.
Results from sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C LC_IDENTIFICATION=C
attached base packages:
[1] splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] tnet_3.0.11 survival_2.37-7 igraph_0.7.1 tidyr_0.2.0 RODBC_1.3-10 ggplot2_1.0.0 plyr_1.8.1 reshape2_1.4
[9] data.table_1.9.2 dplyr_0.2
loaded via a namespace (and not attached):
[1] MASS_7.3-34 Rcpp_0.11.2 assertthat_0.1 colorspace_1.2-4 digest_0.6.4 grid_3.1.1 gtable_0.1.2 labeling_0.2
[9] magrittr_1.0.1 munsell_0.4.2 parallel_3.1.1 proto_0.3-10 scales_0.2.4 stringi_0.4-1 stringr_0.6.2 tools_3.1.1