Skip to content

Instantly share code, notes, and snippets.

@stephlocke
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephlocke/12ebfbcf8f945001aa54 to your computer and use it in GitHub Desktop.
Save stephlocke/12ebfbcf8f945001aa54 to your computer and use it in GitHub Desktop.
Example of named argument not working (as I'd expect) in magrittr
> mytestfunction(iris)
Functional sequence with the following components:
1. data.table(.)
2. { if (y) rbind(head(., 1), tail(., 1)) else .}
Use 'functions' to extract the individual functions.
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.9.4 magrittr_1.5
loaded via a namespace (and not attached):
[1] chron_2.3-45 plyr_1.8.1 Rcpp_0.11.4 reshape2_1.4.1 stringr_0.6.2 tools_3.1.2
library(magrittr)
library(data.table)
mytestfunction<-function(x,y = TRUE) {
x %>%
data.table %>%
{
if(y) {
.[1:50]
} else {
.[1:75]
}
} %>%
nrow
}
mytestfunction(iris)
@smbache
Copy link

smbache commented Feb 9, 2015

library(magrittr)
library(data.table)

mytestfunction(x,y = TRUE) {
  x %>%
    data.table %>%
    { 
       if(y) {
          .[1:50]
       } else {
          .[1:75]
       } 
    } %>%
    nrow
  }

mytestfunction(iris)

Would this work? On phone, so excuse typos :)

@stephlocke
Copy link
Author

Ah - so you have to fully name the argument at the beginning of the pipe instead of it allowing the first NULL to receive the value

@smbache
Copy link

smbache commented Feb 9, 2015

The first NULL? What should the dot refer to? I am not sure I get what you want? What is the purpose of x in your (original) version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment