Reputation: 5819
Code chunk #1 and #2 are identical, except for line number 14. Code chunk #1 uses a print()
call and code chunk #2 uses the View()
call. Code chunk #1 works fine. Code chunk #2 gives the error "Error in FUN(X[[i]], ...) : object 'cal.date' not found"
. Why?
library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- df %>% mutate(mutation = 2 * !!variable, horizontal.line = hor.line)
}
parent_function <- function(df, date, variable, hor.line = 6) {
date <- enquo(date)
variable <- enquo(variable)
df <- df %>% child_function(!!variable, hor.line) %>% print() # LINE 14
p <- ggplot(df, aes(!!date, mutation)) +
geom_point() +
geom_hline(aes(yintercept = hor.line))
p
}
parent_function(graph.data, date = cal.date, variable = random_num, hor.line = 8)
library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- df %>% mutate(mutation = 2 * !!variable, horizontal.line = hor.line)
}
parent_function <- function(df, date, variable, hor.line = 6) {
date <- enquo(date)
variable <- enquo(variable)
df <- df %>% child_function(!!variable, hor.line) %>% View() # LINE 14
p <- ggplot(df, aes(!!date, mutation)) +
geom_point() +
geom_hline(aes(yintercept = hor.line))
p
}
parent_function(graph.data, date = cal.date, variable = random_num, hor.line = 8)
Upvotes: 1
Views: 38
Reputation: 1321
View()
is a side effect function and does not return anything.
Use %T>%
from the magrittr
package instead of %>%
for your second case.
View()
ends the pipe such that you will want to have a T pipe
instead. I think you can see it more clearly like this
df %>% child_function(!!variable, hor.line) %>% View() -> df
vs.
df %>% child_function(!!variable, hor.line) %T>% View() -> df
Upvotes: 5