rlang

Functional programming with dplyr

和自甴很熟 提交于 2019-12-18 15:52:22
问题 Looking for a more efficient / elegant way to pass multiple arguments to a group-by using non-standard evaluation in a function using dplyr. I don't want to use the ... operator, but to specify the functions individually. My specific use case is a function which takes a data frame and creates a ggplot object with simpler syntax. Here is an example of the code I want to automate with my function: # create data frame my_df <- data.frame(month = sample(1:12, 1000, replace = T), category = sample

How to pass strings denoting expressions to dplyr 0.7 verbs?

谁说胖子不能爱 提交于 2019-12-17 22:25:53
问题 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

R package updates

一世执手 提交于 2019-12-17 21:17:58
问题 I have been having some issues accessing an updated version of a package in R. On running a workflow for data analysis I got this error message: library(dplyr) Error: package or namespace load failed for ‘dplyr’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): namespace ‘rlang’ 0.2.1 is already loaded, but >= 0.2.2 is required I therefore tried to update both the "dplyr" and "rlang" packages. On updating I get the messages: Installing package into ‘C:/Users/tomsp/OneDrive

using `rlang` for conditional labelling in `ggplot` using `ggrepel`

﹥>﹥吖頭↗ 提交于 2019-12-13 19:10:07
问题 I am writing a custom function to create a scatterplot with labels attached to points. Here is a minimal rendition of the same. # needed libraries library(tidyverse) library(ggplot2) library(ggrepel) # custom function label_adder <- function(data, x, y, label.var) { # basic plot plot <- ggplot(data = data, mapping = aes( x = !!rlang::enquo(x), y = !!rlang::enquo(y) )) + geom_point() + geom_smooth(method = "lm") # adding label plot <- plot + geom_label_repel(mapping = aes(label = !!rlang:

How to wrap css and xpath arguments in html_nodes in a generic function

两盒软妹~` 提交于 2019-12-13 03:14:53
问题 I want to create a wrapper around html_node that is capable of reading CSS and XPATH arguments. I want to create a quoted expression that can be supplied to html_node and be evaluated at the spot. I figured out how to create the path argument for css and xpath respectively, but when I supply this expression to html_node it does not work. Why not? page_parser <- function(dat_list, path = NULL, css = FALSE, attr = "") { library(rlang) # make css or path argument for html_nodes if (css == TRUE)

Using the pipe in selfmade function with tidyeval (quo_name)

我是研究僧i 提交于 2019-12-11 15:17:32
问题 I have two functions: date_diff and group_stat. So I have read this article tidyverse and I try so create simple functions and use the pipe. The first function creates a difftime and names them timex_minus_timey but when I pipe this result into the next function I have to look at the name so I can fill in summary_var. Is there a better way to do this? library(tidyverse) # set.seed(42) data <- dplyr::bind_rows( tibble::tibble(Hosp = rep("A", 1000), drg = sample(letters[1:5], 1000, replace =

dplyr 0.7 - Specify grouping variable as string

与世无争的帅哥 提交于 2019-12-11 07:58:01
问题 I have some variable names specified as string (e.g. input from Shiny app) and I would like to use them in my dplyr and ggplot2 code as if they were variables. I got it to work by trial and error, but I feel like there must be a better way. What is a better way to perform these operations? library(rlang) library(ggplot2) library(dplyr) someString <- "g1" df <- tibble( g1 = c(1, 1, 2, 2, 2), g2 = c(1, 2, 1, 2, 1), a = sample(5), b = sample(5) ) my_summarise <- function(df, group_var) { print

if-statement in a function using purrr and dplyr (List Column Workflow) in R

萝らか妹 提交于 2019-12-11 05:59:07
问题 I am trying to estimate differences in means for two hospitals, A and B. Every hospital has different "groups" and I have given them group 1 and 2 in the simulated data set. That is I want to test difference in means between hospital A and B within group 1 and group 2 and in addition I have more than one variable (e.g. value1 and value2). So I have to test value1 between hospital A and B within the groups 1 and 2. Even though I specify method=1 in the call at the end I get the third method

dplyr::mutate unquote RHS

自作多情 提交于 2019-12-11 05:09:07
问题 I am wondering how to properly UQ string created variable names on the RHS in dplyr methods like mutate . See the error messages I got in comments in the wilcox.test part of this MWE: require(dplyr) dfMain <- data.frame( base = c(rep('A', 5), rep('B', 5)), id = letters[1:10], q0 = rnorm(10) ) backgs <- list( A = rnorm(13), B = rnorm(11) ) fun <- function(dfMain, i = 0){ pcol <- sprintf('p%i', i) qcol <- sprintf('q%i', i) ( dfMain %>% group_by(id) %>% mutate( !!pcol := ifelse( !is.nan(!!qcol)

Constructing lists using tidyeval tools (like `!!` and `:=`)

谁说我不能喝 提交于 2019-12-11 04:38:55
问题 I am looking a way for easy list constructing based on R 's tidyeval framework as defined in the rlang package. Below is what I want to achieve: a <- "item_name" b <- "item_value" identical( list(!!a := !!b), # list(!!a := b) is of course also fine list(item_name = "item_value") ) What I can obtain at the moment is: list(!!a := !!b) # output [[1]] `:=`(!(!a), !(!b) Alternatively it can get perhaps a little bit better when adding quosure : quo(list(!!a := !!b)) # output <quosure: global> ~list