magrittr

suppressWarnings() doesn't work with pipe operator

随声附和 提交于 2019-12-08 15:18:49
问题 I am trying to suppress warnings by using the suppressWarnings() function. Surprisingly, it removes warnings when used normally, but it fails to do so when you use the pipe %>% operator. Here is some example code : library(magrittr) c("1", "2", "ABC") %>% as.numeric() # [1] 1 2 NA # Warning message: # In function_list[[k]](value) : NAs introduced by coercion c("1", "2", "ABC") %>% as.numeric() %>% suppressWarnings # [1] 1 2 NA # Warning message: # In function_list[[i]](value) : NAs introduced

Referencing piped dataset in ggplot layers for subsetting

限于喜欢 提交于 2019-12-06 10:09:22
Trying to find a way to reference different parts of the dataset for different ggplot2 geom layers without having to save the dataset first in the global environment. Ex non working solution. read_excel("Book1.xlsx",sheet = "Sheet2") %>% ggplot(aes(x,y)) + geom_col() + geom_point(data=subset($ID == "1"),colour="red") Above does not seem to work since i am not referencing the piped(magrittr) dataset in a way that R can recognize it. I have searched but the only solutions i could see are based of the one where i first save the dataset in the global environment as such Ex working solution with

dplyr . and _no visible binding for global variable '.'_ Note in package check

╄→гoц情女王★ 提交于 2019-12-05 18:26:37
问题 In dplyr one can write code like e.g. using the '.' to refer to the data in the pipe x <- data.frame(x = 2:4) y <- data.frame(y = 1:3) y %>% dplyr::bind_cols(x,.) but when using it in a function and running the package check it produces the no visible binding for global variable '.'. What is the best practice to handle the NOTE? 回答1: Best practice now is to probably use quosures. This other SO post has a good summary: How to evaluate a constructed string with non-standard evaluation using

dplyr + magrittr + qplot = no plot?

笑着哭i 提交于 2019-12-04 10:17:45
I want to use qplot (ggplot2) and then forward the data with magrittr : This works: mtcars %>% qplot(mpg, cyl, data=.) This produces an error: mtcars %>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg)) And those produce only summary statistics: mtcars %T>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg)) mtcars %>% {qplot(mpg, cyl, data=.); .} %>% summarise(mean(mpg)) mtcars %T>% {qplot(mpg, cyl, data=.)} %>% summarise(mean(mpg)) What is the problem? I already found this solution, but it does not help, as you see from the code attached. All ggplot2 functions return an object that represents a

compatibility issue of magrittr and arima in R

你离开我真会死。 提交于 2019-12-04 09:31:10
consider the following example: library(tidyverse) set.seed(1) forecast::forecast x <- cumsum(rnorm(10)) y1 <- arima(x, order = c(1, 0, 0)) y2 <- x %>% arima(order = c(1, 0, 0)) length(fitted(y1)) [1] 10 length(fitted(y2)) [1] 0 The objects y1 and y2 are almost identical, the only exceptions being the slots call and series . So I guess that is where the fitted functions starts its magic. I would really like to work with y1 instead of y2 . Does anyone know an alternative function to fitted which produces the same result? EDIT2: The above "bug" does not appear if the forecast package is not

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

拟墨画扇 提交于 2019-12-04 04:49:51
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)) layers <- list(geom_point(), geom_line(), xlab("Quarter Mile Time"), ylab("Fuel Efficiency")) x +

magrittr and date objects

南楼画角 提交于 2019-12-04 04:41:45
问题 I just noticed that when I subtract two dates from one another and then try to cast to to a numeric value with magrittr ( %>% ) I get a date, where as if I simply wrap the date difference in as.numeric() I get the number of days difference as a numeric value. See the simple example below: ## version 1 as.Date("2014-10-10") - as.Date("2014-10-1") %>% as.numeric ## return value --> "1970-01-10" ## version 2 (returning the value that I actually want) as.numeric(as.Date("2014-10-10") - as.Date(

Multiple ggplots with magrittr tee operator

拈花ヽ惹草 提交于 2019-12-04 03:03:27
I am trying to figure out why the tee operator, %T>%, does not work when I pass the data to a ggplot command. This works fine library(ggplot2) library(dplyr) library(magrittr) mtcars %T>% qplot(x = cyl, y = mpg, data = ., geom = "point") %>% qplot(x = mpg, y = cyl, data = ., geom = "point") And this also works fine mtcars %>% {ggplot() + geom_point(aes(cyl, mpg)) ; . } %>% ggplot() + geom_point(aes(mpg, cyl)) But when I use the tee operator, as below, it throws "Error: ggplot2 doesn't know how to deal with data of class protoenvironment". mtcars %T>% ggplot() + geom_point(aes(cyl, mpg)) %>%

dplyr . and _no visible binding for global variable '.'_ Note in package check

混江龙づ霸主 提交于 2019-12-04 02:55:41
In dplyr one can write code like e.g. using the '.' to refer to the data in the pipe x <- data.frame(x = 2:4) y <- data.frame(y = 1:3) y %>% dplyr::bind_cols(x,.) but when using it in a function and running the package check it produces the no visible binding for global variable '.'. What is the best practice to handle the NOTE? Best practice now is to probably use quosures. This other SO post has a good summary: How to evaluate a constructed string with non-standard evaluation using dplyr? In practice, I've just included . = NULL at the top of my functions. EDIT As @MrFlick pointed out,

Easily finding and replacing every match in a nested list

流过昼夜 提交于 2019-12-04 01:24:39
问题 Take this object as an example: expr <- substitute(mean(exp(sqrt(.)), .)) It is a nested list. I want to find every element that matches quote(.) . For example, magrittr 's solution matches only the first level of the call: dots <- c(FALSE, vapply(expr[-1], identical, quote(.), FUN.VALUE = logical(1))) dots [1] FALSE FALSE TRUE But I wanted to find every "." in an arbitrary nested list. In this particular case this would be these two dots: expr[[3]] expr[[2]][[2]][[2]] And then these dots