2

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  
Community
  • 1
  • 1
EconomiCurtis
  • 2,107
  • 4
  • 23
  • 33
  • 3
    Are you sure that's exactly the code you are running? Are you sure the data.frame name you use in the function matches the parameter name? I can not replicate those errors. Can you show the output of `sessionInfo()` so we can see what versions of tidyr, dplyr, and magrittr you are using? – MrFlick Apr 13 '15 at 19:03
  • 1
    Well, you have an older version of `magrittr`. I might try `update.packages("magrittr")`. But i think it's more likely you have a typo in what you've posted here as a reproducible example. Have you tried to copy/paste this code into a new R session? – MrFlick Apr 13 '15 at 19:08
  • I have taken both your suggestions (update 'magrittr' and reset my session) getting the same errors – EconomiCurtis Apr 13 '15 at 19:12
  • You know what.... I tried it on another machine that had R installed more recently, without issue. Perhaps the problem is resolved updating `R version 3.1.1 (2014-07-10)' – EconomiCurtis Apr 13 '15 at 19:14
  • It worked for me in "R version 3.1.0". When you restarted R you only ran the code above? Did it restore a workspace when you restarted? Something isn't matching up here. – MrFlick Apr 13 '15 at 19:15
  • I restarted the machine (and have been updating my version of R). Works now. Though I regret this wasn't a good teaching moment for me about Scope, happy you helped me resolve the issue @MrFlick! – EconomiCurtis Apr 13 '15 at 21:46
  • 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. – EconomiCurtis Apr 14 '15 at 19:07

0 Answers0