在管道%>%之后插入if语句,以在自定义函数中返回错误消息 [英] insert if statement after pipe %>% to return error message within custom function
问题描述
一个非常简单的问题,但是找不到解决方案,如果变量不是 dplyr
class 的变量,我想做的就是从函数返回错误消息>管道.可以说我有:
Very simple problem but cant find the solution, all I want to do is return an error message from a function if a variable is not of a certain class
within a dplyr
pipe. Lets say I have:
library(tidyverse)
library(scales)
library(ggplot2)
data1 <- data.frame(date1 = sample(seq(as.Date("2005-01-01"), as.Date('2018-01-01'), by="day"), 5),
num1 = 1:5)
data1
date1 num1
1 2008-10-20 1
2 2005-01-17 2
3 2014-03-19 3
4 2005-01-24 4
5 2014-01-21 5
和一个用于绘制日期直方图的小函数:
and a small function to plot a histogram of dates:
hist_date_fun <- function(df, date_varaible) {
date_varaible = enquo(date_varaible)
df %>%
group_by(month = floor_date(!!date_varaible, "month")) %>%
dplyr::summarize(freq = n()) %>%
ggplot(aes(x = month, y = freq)) +
geom_bar(stat="identity") +
scale_x_date(labels = date_format("%m-%Y"), breaks = date_breaks ("1 month")) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
}
如果我有一个日期变量,这可以很好地工作:
this works fine if I have a date variable:
hist_date_fun(data1, date1)
我只想在函数中间插入 if
语句并 return
错误,以告诉用户变量不是日期,例如:
I just want to insert an if
statement and return
an error in the middle of the function to tell the user the variable is not a date, something like:
test <- function(x) {
if(!is.Date(x)) {
stop('this function only works for date input!\n',
'You have provided an object of class: ', class(x)[1])
}
else ....#rest of code
}
,以便 hist_date_fun(data1,num1)
将返回错误.
我的尝试:
hist_date_fun <- function(df, date_varaible) {
date_varaible = enquo(date_varaible)
df %>%
if (!is.Date(date_varaible)) {
stop("Stop variable is of class", class(date_varaible)[1])
#or return()?
}
else {
group_by(month = floor_date(!!date_varaible, "month")) %>%
dplyr::summarize(freq = n()) %>%
ggplot(aes(x = month, y = freq)) +
geom_bar(stat="identity") +
scale_x_date(labels = date_format("%m-%Y"), breaks = date_breaks ("1 month")) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
}}
有什么建议吗?
参考: https://community.rstudio.com/t/return-on-conditional-from-the-middle-of-a-pipe/5513/2
推荐答案
这是使用卷曲的一种方法-来自 rlang
Here is one way using curly - curly operator from rlang
library(tidyverse)
library(rlang)
library(scales)
hist_date_fun <- function(df, date_varaible) {
class_date <- df %>% pull({{date_varaible}}) %>% class
if (class_date != "Date")
stop("Stop variable is of class ", class_date)
else {
df %>%
group_by(month = floor_date({{date_varaible}}, "month")) %>%
dplyr::summarize(freq = n()) %>%
ggplot(aes(x = month, y = freq)) +
geom_bar(stat="identity") +
scale_x_date(labels = date_format("%m-%Y"),
breaks = date_breaks ("1 month")) +
theme_bw() +
(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
}
}
hist_date_fun(data1, num1)
hist_date_fun(data1,num1)中的错误:Stop变量属于整数类
Error in hist_date_fun(data1, num1) : Stop variable is of class integer
这篇关于在管道%>%之后插入if语句,以在自定义函数中返回错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!