tidyeval

Why is enquo + !! preferable to substitute + eval

送分小仙女□ 提交于 2019-11-27 19:43:26
In the following example, why should we favour using f1 over f2 ? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option. library(dplyr) d = data.frame(x = 1:5, y = rnorm(5)) # using enquo + !! f1 = function(mydata, myvar) { m = enquo(myvar) mydata %>% mutate(two_y = 2 * !!m) } # using substitute + eval f2 = function(mydata, myvar) { m = substitute(myvar) mydata %>% mutate(two_y = 2 * eval(m)) } all.equal(d %>% f1(y), d %>% f2(y)) # TRUE In other words, and beyond this particular example, my question is: can I get get away

How to combine ggplot and dplyr into a function?

本小妞迷上赌 提交于 2019-11-27 14:24:53
Consider this simple example library(dplyr) library(ggplot2) dataframe <- data_frame(id = c(1,2,3,4), group = c('a','b','c','c'), value = c(200,400,120,300)) # A tibble: 4 x 3 id group value <dbl> <chr> <dbl> 1 1 a 200 2 2 b 400 3 3 c 120 4 4 c 300 Here I want to write a function that takes the dataframe and the grouping variable as input. Ideally, after grouping and aggregating I would like to print a ggpplot chart. This works: get_charts2 <- function(data, mygroup){ quo_var <- enquo(mygroup) df_agg <- data %>% group_by(!!quo_var) %>% summarize(mean = mean(value, na.rm = TRUE), count = n()) %

pass function arguments to both dplyr and ggplot

大城市里の小女人 提交于 2019-11-27 14:23:57
I'm confused about how to pass function argument into dplyr and ggplot codes. I'm using the newest version of dplyr and ggplot2 Here is my code to produce a barplot (clarity vs mean price) diamond.plot<- function (data, group, metric) { group<- quo(group) metric<- quo(metric) data() %>% group_by(!! group) %>% summarise(price=mean(!! metric)) %>% ggplot(aes(x=!! group,y=price))+ geom_bar(stat='identity') } diamond.plot(diamonds, group='clarity', metric='price') error: Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "packageIQR" For the newest

Scale value inside of aes_string()

雨燕双飞 提交于 2019-11-27 07:38:55
问题 I want to scale my y-var by multiplying it by a number, say 10, in ggplot. The problem is this is in a Shiny app and the variable must be passed as a character string, i.e. input$variable . How can I multiply one of the variables in aes_string() the same way I would in aes() ? Here is an example of when it fails: library(ggplot2) ggplot(data = subset(mtcars, cyl == 4), aes_string(x = "wt", y = "mpg")) + geom_line(size = 1.5, color = "#00868B") + geom_line(data = subset(mtcars, cyl == 8), aes

Error when using dplyr inside of a function

陌路散爱 提交于 2019-11-27 02:13:08
问题 I'm trying to put together a function that creates a subset from my original data frame, and then uses dplyr's SELECT and MUTATE to give me the number of large/small entries, based on the sum of the width and length of sepals/petals. filter <- function (spp, LENGTH, WIDTH) { d <- subset (iris, subset=iris$Species == spp) # This part seems to work just fine large <- d %>% select (LENGTH, WIDTH) %>% # This is where the problem arises. mutate (sum = LENGTH + WIDTH) big_samples <- which(large$sum

pass function arguments to both dplyr and ggplot

好久不见. 提交于 2019-11-26 16:43:50
问题 I'm confused about how to pass function argument into dplyr and ggplot codes. I'm using the newest version of dplyr and ggplot2 Here is my code to produce a barplot (clarity vs mean price) diamond.plot<- function (data, group, metric) { group<- quo(group) metric<- quo(metric) data() %>% group_by(!! group) %>% summarise(price=mean(!! metric)) %>% ggplot(aes(x=!! group,y=price))+ geom_bar(stat='identity') } diamond.plot(diamonds, group='clarity', metric='price') error: Error in UseMethod("group

Creating multiple graphs based upon the column names

断了今生、忘了曾经 提交于 2019-11-26 06:08:23
问题 This is my first question on stackoverlow, please correct me if I am not following correct question protocols. I am trying to create some graphs for data that has been collected over three time points (time 1, time 2, time 3) which equates to X1..., X2... and X3... at the beginning of column names. The graphs are also separated by the column $Group from the data frame. I have no problem creating the graphs, I just have many variables (~170) and am wanting to compare time 1 vs time 2, time 2

dplyr: How to use group_by inside a function?

喜你入骨 提交于 2019-11-26 03:56:04
问题 I want to use use the dplyr::group_by function inside another function, but I do not know how to pass the arguments to this function. Can someone provide a working example? library(dplyr) data(iris) iris %.% group_by(Species) %.% summarise(n = n()) # ## Source: local data frame [3 x 2] ## Species n ## 1 virginica 50 ## 2 versicolor 50 ## 3 setosa 50 mytable0 <- function(x, ...) x %.% group_by(...) %.% summarise(n = n()) mytable0(iris, \"Species\") # OK ## Source: local data frame [3 x 2] ##

Multiple plots in for loop ignoring par

隐身守侯 提交于 2019-11-26 02:02:55
问题 I am trying to generate 10 pairs of plots with a few pairs per page of plots, and am using a for loop to construct the pairs. However, the plots are sent to the device as separate plots instead of pages. The MWE below has identical constructions for base graphics and ggplot versions, but the base graphics works and ggplot does not. What do I need to do to get the pagination correct in the second version? library(ggplot2) attach(mtcars) # correct configuration par(mfrow=c(2,2)) for (ii in 1:3)