magrittr

R package fails devtools::check, because “could not find function” even though the function is imported in NAMESPACE

╄→尐↘猪︶ㄣ 提交于 2020-07-21 05:25:11
问题 Trying to build my first R package using roxygen2 and devtools . I have added a function that uses %>% and mutate in the @examples section. When I run check() it fails, because it cannot find the function %>% or mutate . Based on this, this, and this I have tried the following: I have #' importFrom magrittr %>% and #' importFrom dplyr mutate in the function's .R file. I also have magrittr and dplyr under Imports: in the DESCRIPTION file. After running document() , my NAMESPACE file contains

Use argument place holder within ggplot

强颜欢笑 提交于 2020-05-26 04:41:21
问题 I am trying to use an argument place holder . within a ggplot() . But it doesn't work for some reason I am not entirely sure of. What I am doing is this (using the sample data from ggplot2 /the tidyverse ): library(tidyverse) library(magrittr) corr_eqn <- function(x, y, digits = 2) { corr_coef <- round(cor(x, y, use = "pairwise.complete.obs"), digits = digits) paste("r = ", corr_coef) } economics %>% filter(date >= "1990-11-01") %>% ggplot(aes(pop, unemploy)) + geom_point()+ annotate(geom =

piping a vector into all() to test equality

那年仲夏 提交于 2020-04-30 10:38:08
问题 I'm trying to pipe a vector into an all() statement to check if all elements are equal to a certain value. I figure I need to use the exposition pipe %$% since all() does not have a built-in data argument. My attempt leads to an error: library(tidyverse) library(magrittr) vec <- c("a", "b", "a") vec %>% keep(!grepl("b", .)) %$% all(. == "a") #> Error in eval(substitute(expr), data, enclos = parent.frame()): invalid 'envir' argument of type 'character' If I break the pipe before all() and

Get expression that evaluated to dot in function called by `magrittr`

删除回忆录丶 提交于 2020-03-18 12:43:25
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

Get expression that evaluated to dot in function called by `magrittr`

回眸只為那壹抹淺笑 提交于 2020-03-18 12:43:19
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

R: Using piping to pass a single argument to multiple locations in a function

蓝咒 提交于 2020-01-24 12:04:33
问题 I am attempting to exclusively use piping to rewrite the following code (using babynames data from babynames package: library(babynames) library(dplyr) myDF <- babynames %>% group_by(year) %>% summarise(totalBirthsPerYear = sum(n)) slice(myDF, seq(1, nrow(myDF), by = 20)) The closest I have gotten is this code (not working): myDF <- babyNames %>% group_by(year) %>% summarise(totalBirthsPerYear = sum(n)) %>% slice( XXX, seq(1, nrow(XXX), by = 20)) where XXX is meant to be passed via pipes to

Is there a way to `pipe through a list'?

好久不见. 提交于 2020-01-12 14:46:10
问题 One really cool feature from the ggplot2 package that I never really exploited enough was adding lists of layers to a plot. The fun thing about this was that I could pass a list of layers as an argument to a function and have them added to the plot. I could then get the desired appearance of the plot without necessarily returning the plot from the function (whether or not this is a good idea is another matter, but it was possible). library(ggplot2) x <- ggplot(mtcars, aes(x = qsec, y = mpg))

Use of ! (or any logical operator) with %>% (magrittr) produces unexpected output

本秂侑毒 提交于 2020-01-11 08:46:06
问题 I have run across a situation where %>% produces very surprising output when combined with ! . Consider the following code: x <- c(1:20) y <- !is.na(x) > y [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE > sum(Y) [1] 20 Ok, nothing surprising there. But if I try to shorten it using %>% weird stuff happens: !is.na(x) %>% sum [1] TRUE TRUE ?? Not what I expected - it should be 20 . If I remove the ! it gives me 0 as expected: > is.na(x) %>

Custom pipe to silence warnings

ε祈祈猫儿з 提交于 2020-01-10 17:46:09
问题 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 <-

Use pipe without feeding first argument

旧时模样 提交于 2020-01-09 10:18:07
问题 Is the %>% pipe operator always feeding the left-hand side (LHS) to the first argument of the right-hand side (RHS)? Even if the first argument is specified again in the RHS call? Say I want to specify which variable to use in cor() : library(magrittr) iris %>% cor(x=.$Sepal.Length, y=.$Sepal.Width) But this fails, it looks like it call something like cor(., x=.$Sepal.Length, y=.$Sepal.Width) ? I know I could use instead iris %$% cor(x=Sepal.Length, y=Sepal.Width) But wanted to find a