- operator with assembly using a non-standard estimate

I would like to write a function that takes quosure as an argument, adds -to quosure and passes this value to gather, for example:

library(tidyverse)
my_gather <- function(d, not.columns) {
  dg <- tidyr::gather(d, key = k, value = v, .dots = -!!!not.columns)
  dg
}

de <- my_gather(mtcars, not.columns = quos(mpg, cyl, disp))

> Error in `-`(~mpg, ~cyl, ~disp) : operator needs one or two arguments

This is clear because I need to add every quosure element with -, rather than add all quosure with -. But in my work, creating this quosure will not be easy in form quos(-mpg, -cyl, -disp)- so how can I change quos(mpg, cyl, disp)to add -?

I would like to see a result that is identical gather(mtcars, key = k, value = v, -mpg, -cyl, -disp), the first 3 lines of which are equal

   mpg cyl disp  k   v
1 21.0   6  160 hp 110
2 21.0   6  160 hp 110
3 22.8   4  108 hp  93

There is a similar question here, but it does not answer and does not seem to address the problem quos(), not quo().

+4
source share
2

my_gather <- function(d, not.columns) {
  tidyr::gather(d, key = k, value = v, .dots =  -c(UQS(not.columns)))
  #or use !!! instead of UQS
  #tidyr::gather(d, key = k, value = v, .dots =  -c(!!!(not.columns)))

}
de <- my_gather(mtcars, not.columns = quos(mpg, cyl, disp))
head(de, 3)
#   mpg cyl disp  k   v
#1 21.0   6  160 hp 110
#2 21.0   6  160 hp 110
#3 22.8   4  108 hp  93

de1 <- gather(mtcars, key = k, value = v, -mpg, -cyl, -disp)
identical(de, de1)
#[1] TRUE
+3

" ". , . :

library(tidyverse)

negate_columns <- function(.tbl, not.columns) {
  not_columns <- colnames(select(.tbl, !!!not.columns))

  setdiff(colnames(.tbl), not_columns)
}

my_gather <- function(d, not.columns) {
  columns <- negate_columns(d, not.columns)

  tidyr::gather(d, key = k, value = v, !!!syms(columns))
}

, , :

my_gather(mtcars, not.columns = quos(mpg, cyl, disp)) %>%
    head(3)
#>    mpg cyl disp  k   v
#> 1 21.0   6  160 hp 110
#> 2 21.0   6  160 hp 110
#> 3 22.8   4  108 hp  93
+2

Source: https://habr.com/ru/post/1688365/


All Articles