Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在管道%>;%后插入if语句在自定义函数中返回错误消息的步骤_R_Function_If Statement_Dplyr_Return - Fatal编程技术网

R 在管道%>;%后插入if语句在自定义函数中返回错误消息的步骤

R 在管道%>;%后插入if语句在自定义函数中返回错误消息的步骤,r,function,if-statement,dplyr,return,R,Function,If Statement,Dplyr,Return,非常简单的问题,但找不到解决方案,如果变量不是dplyr管道中的某个class,我只想从函数返回一条错误消息。假设我有: 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),

非常简单的问题,但找不到解决方案,如果变量不是
dplyr
管道中的某个
class
,我只想从函数返回一条错误消息。假设我有:

    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
< P> >我只想插入一个<代码>如果语句和<代码>返回<代码>函数中间的一个错误,告诉用户变量不是一个日期,比如:

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
}
有什么建议吗


Ref:

这里有一种方法可以使用
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)
库(tidyverse)
图书馆(rlang)
图书馆(比例尺)
历史日期乐趣%class
如果(类别日期!=“日期”)
停止(“停止变量属于类别”,类别\日期)
否则{
df%>%
分组依据(月=楼层日期({{date\u varaible},“月”))%>%
dplyr::summary(freq=n())%>%
ggplot(aes(x=月,y=频率))+
几何图形栏(stat=“identity”)+
比例x日期(标签=日期格式(“%m-%Y”),
休息时间=休息时间(“1个月”)+
主题_bw()+
(axis.text.x=element_text(角度=90,vjust=0.5,hjust=1))
}
}
历史日期(数据1,num1)
hist_date_fun(数据1,num1)中出错:停止变量为整数类

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 <- 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))
      }}
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)