Tidyeval in own functions in own functions inside own functions with the pipe 管道

Tidyeval in own functions inside own functions with the pipe

所以我正在尝试打包(我没有在下面包含我的 roxygen2 headers):

我有这个功能:

date_from_text <- function(df, x){
  x <- rlang::enquo(x)
  name <- rlang::quo_name(x)

  df %>%
    dplyr::mutate(!!name := lubridate::ymd_hms(!!x))
}

当日期时间列正确时 class 然后我用它来提取所有组件。

date_columns <- function(df, x){

  x <- rlang::enquo(x)

  df %>%
      dplyr::mutate(year=lubridate::year(!!x),
           ydag=lubridate::yday(!!x),
           weekday=lubridate::wday(!!x, label=FALSE),
           hour = lubridate::hour(!!x),
           hour_min= hms::as.hms(lubridate::force_tz(!!x)),
           week_num = lubridate::week(!!x),
           month = lubridate::month(!!x),
           date = lubridate::date(!!x))
}

我不希望 date_from_text 函数包含在 NAMESPACE 中,我想以某种方式将它包含在 date_columns 函数中。比如检查时间戳是否正确 class 如果不正确则更改 class.. 然后创建所有日期时间组件。

我不知道如何在另一个函数中调用第一个函数。

测试数据:

df <- structure(list(time = c("2018-01-30 20:08:18", "2018-02-01 21:01:25", 
"2018-01-31 23:25:12", "2018-01-28 23:45:34", "2018-01-31 12:00:55", 
"2018-02-04 09:15:31", "2018-01-27 21:08:02", "2018-02-08 01:50:31", 
"2018-02-01 03:15:43", "2018-02-04 01:04:52"), type = c("A", 
"D", "B", "B", "B", "D", "C", "C", "C", "A")), .Names = c("time", 
"type"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

更新:所以我现在将 date_from_text 包含在 date_columns

date_columns <- function(df, x){
  x <- rlang::enquo(x)

  out <-  df %>%
    date_from_text(!!x) %>%
      dplyr::mutate(year=lubridate::year(!!x),
           ydag=lubridate::yday(!!x),
           weekday=lubridate::wday(!!x, label=FALSE),
           hour = lubridate::hour(!!x),
           hour_min= hms::as.hms(lubridate::force_tz(!!x)),
           week_num = lubridate::week(!!x),
           month = lubridate::month(!!x),
           date = lubridate::date(!!x))
 out

  }

所以我不明白为什么我必须在date_columns里面再次使用!!x??它已包含在 date_from_text 中。我正在调用函数而不是创建它...

正如在评论和聊天中所讨论的那样,问题既不是关于包开发和命名空间,也不是关于管道。问题是如何在可能嵌套的包装函数中使用

答案是用户传递给函数的表达式需要是 quoted and unquoted,就像下面通过 enquo(x)!!xdate_from_text() 中一样。

date_from_text <- function(df, x) {
        x <- rlang::enquo(x)                                  # quote via enquo()
        name <- rlang::quo_name(x) 
        dplyr::mutate(df, !!name := lubridate::ymd_hms(!!x))  # unquote via !!
}

然后,可以在"interactive"模式下将表达式传递给此类函数,就像dplyr函数一样:

date_from_text(df, time)
select(df, time)

请记住,函数内部的函数 不采用表达式,而是带引号和不带引号的表达式,就像在 mutate() 调用 enquo(x) 中一样和上面的 !!x

这意味着,在下面的date_from_text()中,调用date_from_text()mutate()都需要接收!!x

date_columns <- function(df, x) {
        x <- rlang::enquo(x)

        out <- date_from_text(df, !!x)
        out <- dplyr::mutate(out, year = lubridate::year(!!x))
        return(out)
}

除此之外,在包开发中,您可以使用所有函数,无论它们是否为 exported(就像我在 date_columns() 中使用 date_from_text() 所做的那样)。导出的函数需要文档化,通过library(pkg)pkg::fun()安装后才能使用,而未导出的函数只能通过pkg:::fun().

安装后使用

我修改了@David 的函数以便专注于相关部分