Problems with `invoke_map_dbl`'s argument

I read a post on the R-blogger.

The writer use purrr's function in this way .

library(tidyverse)
spread <- list(sd = sd, iqr = IQR, mad = mad)
x <- rnorm(100)
invoke_map_dbl(spread, x = x)
#>       sd      iqr      mad 
#> 1.040133 1.393010 1.051002

Created on 2018-11-26 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                                              
#>  version  R version 3.5.1 (2018-07-02)                       
#>  os       Windows 7 x64 SP 1                                 
#>  system   x86_64, mingw32                                    
#>  ui       RTerm                                              
#>  language (EN)                                               
#>  collate  Chinese (Simplified)_People's Republic of China.936
#>  ctype    Chinese (Simplified)_People's Republic of China.936
#>  tz       Asia/Taipei                                        
#>  date     2018-11-26                                         
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version    date       lib source                        
#>  assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.1)                
#>  backports     1.1.2      2017-12-13 [1] CRAN (R 3.5.0)                
#>  base64enc     0.1-3      2015-07-28 [1] CRAN (R 3.5.0)                
#>  bindr         0.1.1      2018-03-13 [1] CRAN (R 3.5.1)                
#>  bindrcpp      0.2.2      2018-03-29 [1] CRAN (R 3.5.1)                
#>  broom         0.5.0      2018-07-17 [1] CRAN (R 3.5.1)                
#>  callr         3.0.0      2018-08-24 [1] CRAN (R 3.5.1)                
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.5.1)                
#>  cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.1)                
#>  colorspace    1.3-2      2016-12-14 [1] CRAN (R 3.5.1)                
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.1)                
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.1)                
#>  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.1)                
#>  digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.1)                
#>  dplyr       * 0.7.8      2018-11-10 [1] CRAN (R 3.5.1)                
#>  evaluate      0.12       2018-10-09 [1] CRAN (R 3.5.1)                
#>  forcats     * 0.3.0      2018-02-19 [1] CRAN (R 3.5.1)                
#>  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.1)                
#>  ggplot2     * 3.1.0      2018-10-25 [1] CRAN (R 3.5.1)                
#>  glue          1.3.0      2018-07-17 [1] CRAN (R 3.5.1)                
#>  gtable        0.2.0      2016-02-26 [1] CRAN (R 3.5.1)                
#>  haven         1.1.2      2018-06-27 [1] CRAN (R 3.5.1)                
#>  hms           0.4.2      2018-03-10 [1] CRAN (R 3.5.1)                
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.5.1)                
#>  httr          1.3.1      2017-08-20 [1] CRAN (R 3.5.1)                
#>  jsonlite      1.5        2017-06-01 [1] CRAN (R 3.5.1)                
#>  knitr         1.20       2018-02-20 [1] CRAN (R 3.5.1)                
#>  lattice       0.20-35    2017-03-25 [1] CRAN (R 3.5.1)                
#>  lazyeval      0.2.1      2017-10-29 [1] CRAN (R 3.5.1)                
#>  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.1)                
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.1)                
#>  memoise       1.1.0.9000 2018-11-15 [1] Github (r-lib/memoise@1650ad7)
#>  modelr        0.1.2      2018-05-11 [1] CRAN (R 3.5.1)                
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.5.1)                
#>  nlme          3.1-137    2018-04-07 [1] CRAN (R 3.5.1)                
#>  pillar        1.3.0      2018-07-14 [1] CRAN (R 3.5.1)                
#>  pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.1)                
#>  pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.5.1)                
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.1)                
#>  plyr          1.8.4      2016-06-08 [1] CRAN (R 3.5.1)                
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.1)                
#>  processx      3.2.0      2018-08-16 [1] CRAN (R 3.5.1)                
#>  ps            1.2.0      2018-10-16 [1] CRAN (R 3.5.1)                
#>  purrr       * 0.2.5      2018-05-29 [1] CRAN (R 3.5.1)                
#>  R6            2.3.0      2018-10-04 [1] CRAN (R 3.5.1)                
#>  Rcpp          0.12.19    2018-10-01 [1] CRAN (R 3.5.1)                
#>  readr       * 1.1.1      2017-05-16 [1] CRAN (R 3.5.1)                
#>  readxl        1.1.0      2018-04-20 [1] CRAN (R 3.5.1)                
#>  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.1)                
#>  rlang         0.3.0.1    2018-10-25 [1] CRAN (R 3.5.1)                
#>  rmarkdown     1.10       2018-06-11 [1] CRAN (R 3.5.1)                
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.1)                
#>  rvest         0.3.2      2016-06-17 [1] CRAN (R 3.5.1)                
#>  scales        1.0.0      2018-08-09 [1] CRAN (R 3.5.1)                
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.1)                
#>  stringi       1.2.4      2018-07-20 [1] CRAN (R 3.5.1)                
#>  stringr     * 1.3.1      2018-05-10 [1] CRAN (R 3.5.1)                
#>  tibble      * 1.4.2      2018-01-22 [1] CRAN (R 3.5.1)                
#>  tidyr       * 0.8.2      2018-10-28 [1] CRAN (R 3.5.1)                
#>  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.5.1)                
#>  tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.5.1)                
#>  usethis       1.4.0      2018-08-14 [1] CRAN (R 3.5.1)                
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.1)                
#>  xml2          1.2.0      2018-01-24 [1] CRAN (R 3.5.1)                
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.5.1)                
#> 
#> [1] D:/software/R-3.5.1/library

I read the help document, there is no argument named by x in it.

invoke_map_dbl(.f, .x = list(NULL), ..., .env = NULL)

I wonder is there any special meanin of x = x here?

1 Like

That particular usage is a bit of a clever shorthand (it’s the same thing that’s going on in examples 6 and 7 on the doc page, which are equivalent). I think it might be clearer written out more verbosely:

invoke_map_dbl(.f = spread, .x = list(NULL), x = x)

Combine that with this condensed excerpt from the docs:

.f …for invoke_map a list of functions.
.x …for invoke_map a list of argument-lists the same length as .f (or length 1). The default argument, list(NULL) , will be recycled to the same length as .f , and will call each function with no arguments (apart from any supplied in ... ).
... Additional arguments passed to each function.

All of the functions in the list spread take an argument x. Here, x is supplied as one of the “additional arguments” passed via .... It happens to refer to the object x, a vector of 100 values. Since nothing is passed to invoke_map_dbl()’s argument .x, nothing other than the ... argument is passed to the functions in spread, so each function is called with x = x.

As in example 6, the same thing could also be accomplished with:

invoke_map_dbl(spread, list(list(x = x)))

But you can maybe see why that’s an awkward syntax in this case!

3 Likes

@jcblum Thanks, that is a very clear answer !!!

And, by the way, I want to know how to type a table in this way you do.

1 Like
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.