rlang

Remove columns the tidyeval way

杀马特。学长 韩版系。学妹 提交于 2019-11-30 14:19:17
I would like to remove a vector of columns using dplyr >= 0.7 library(dplyr) data(mtcars) rem_cols <- c("wt", "qsec", "vs", "am", "gear", "carb") head(select(mtcars, !!paste0("-", rem_cols))) Error: Strings must match column names. Unknown columns: -wt, -qsec, -vs, -am, -gear, -carb dplyr < 0.7 worked as follows: head(select_(mtcars, .dots = paste0("-", rem_cols))) # mpg cyl disp hp drat # Mazda RX4 21.0 6 160 110 3.90 # Mazda RX4 Wag 21.0 6 160 110 3.90 # Datsun 710 22.8 4 108 93 3.85 # Hornet 4 Drive 21.4 6 258 110 3.08 # Hornet Sportabout 18.7 8 360 175 3.15 # Valiant 18.1 6 225 105 2.76 I

Custom pipe to silence warnings

半世苍凉 提交于 2019-11-30 11:38:58
Related to this question . I'd like to build a custom pipe %W>% that would silence warnings for one operation library(magrittr) data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos will be equivalent to : w <- options()$warn data.frame(a= c(1,-1)) %T>% {options(warn=-1)} %>% mutate(a=sqrt(a)) %T>% {options(warn=w)} %>% cos These two tries don't work : `%W>%` <- function(lhs,rhs){ w <- options()$warn on.exit(options(warn=w)) options(warn=-1) lhs %>% rhs } `%W>%` <- function(lhs,rhs){ lhs <- quo(lhs) rhs <- quo(rhs) w <- options()$warn on.exit(options(warn=w)) options(warn=-1) (!!lhs) %>% (!

How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2

回眸只為那壹抹淺笑 提交于 2019-11-30 06:49:14
library(dplyr) #Devel version, soon-to-be-released 0.6.0 library(tidyr) library(ggplot2) library(forcats) #for gss_cat data I'm attempting to write a function that combines quosures from the soon-to-be-released dplyr devel version together with tidyr::gather and ggplot2 . So far it seems to work with tidyr , but I'm having trouble with the plotting. The below function seems to work with tidyr's gather : GatherFun<-function(gath){ gath<-enquo(gath) gss_cat%>%select(relig,marital,race,partyid)%>% gather(key,value,-!!gath)%>% count(!!gath,key,value)%>% mutate(perc=n/sum(n)) } But I can't figure

Defunct as of rlang 0.3.0 and mutate_impl

别等时光非礼了梦想. 提交于 2019-11-30 00:35:26
问题 I am trying to use the following function but every time I do, I receive the error below. I tried installing an older version of rlang as it works on a different R Studio but I was unable to do that. It seems the error is due to the 0.3.0 version. Any suggestions on how to fix this error would be appreciated. details2 <- details %>% mutate(rownames=rownames(.)) %>% filter(isdir==FALSE) %>% arrange(desc(ctime)) Error in mutate_impl(.data, dots) : Evaluation error: `as_dictionary()` is defunct

Rename in dplyr 0.7+ function

不问归期 提交于 2019-11-29 14:26:39
I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting variables in custom dplyr function ). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names. Here is a simple example, which works except for the commented-out lines. library(tidyverse) myfunc <- function(df, groupvar, colvar, grouplab, collab) { groupvar

Supplying multiple groups of variables to a function for dplyr arguments in the body

狂风中的少年 提交于 2019-11-29 14:20:10
Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", "k", 7L, "c", "f", "i", "hello", 3L, "c", "f", "i", "hello", 4L ) and the vectors, I want to use: filter_var <- c("hello") groupby_vars1 <- c("var1", "var2", "var3") groupby_vars2 <- c("var1", "var2") joinby_vars1 <- c("var1", "var2") joinby_vars2 <- c("var1", "var2", "var3") 2nd & 5th, and 3rd & 4th vectors are same, but please assume they are different and retain them as different vectors. Now I want to

dplyr 0.7 equivalent for deprecated mutate_

拈花ヽ惹草 提交于 2019-11-29 10:57:57
I cannot find in dplyr 0.7 a way to replace the mutate_ function which is going to be deprecated. The mutate_ function is useful in my use case : I store in a database (string format) many instructions (that can be filtered if needed) and apply these instructions to one or several data frames. For example : dplyr::tibble(test = "test@test") %>% dplyr::mutate_(.dots = list("test2" = "substr(test, 1, 5)", "test3" = "substr(test, 5, 5)")) Is there a way to do this with dplyr 0.7 keeping variables and instructions as character? To expand a little bit on MrFlick 's example, let's assume you have a

How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 03:05:58
问题 library(dplyr) #Devel version, soon-to-be-released 0.6.0 library(tidyr) library(ggplot2) library(forcats) #for gss_cat data I'm attempting to write a function that combines quosures from the soon-to-be-released dplyr devel version together with tidyr::gather and ggplot2 . So far it seems to work with tidyr , but I'm having trouble with the plotting. The below function seems to work with tidyr's gather : GatherFun<-function(gath){ gath<-enquo(gath) gss_cat%>%select(relig,marital,race,partyid)%

How to pass strings denoting expressions to dplyr 0.7 verbs?

老子叫甜甜 提交于 2019-11-28 19:02:05
I would like to understand how to pass strings representing expressions into dplyr, so that the variables mentioned in the string are evaluated as expressions on columns in the dataframe. The main vignette on this topic covers passing in quosures, and doesn't discuss strings at all. It's clear that quosures are safer and clearer than strings when representing expressions, so of course we should avoid strings when quosures can be used instead. However, when working with tools outside the R ecosystem, such as javascript or YAML config files, one will often have to work with strings instead of

How to pass a named vector to dplyr::select using quosures?

非 Y 不嫁゛ 提交于 2019-11-28 11:17:33
Using the old select_() function, I could pass a named vector into select and change position and column names at once: my_data <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30) my_newnames <- c("newbar" = "bar", "newfoo" = "foo") move_stuff <- function(df, newnames) { select_(df, .dots = newnames) } move_stuff(my_data, newnames = my_newnames) ) # this is the desired output # A tibble: 4 x 2 newbar newfoo <int> <int> 1 10 0 2 11 1 3 12 2 4 13 3 I tried doing something similar using quosures and splicing--selecting columns works great, but the names of the vectors (and thus renaming columns