【问题标题】:How to convert a function into pipe-friendly functions?如何将函数转换为管道友好的函数?
【发布时间】:2020-12-25 02:56:11
【问题描述】:

我正在尝试将以下函数转换为对管道友好的函数。但它是由字符串组成的。我不知道从哪里开始。

library(MplusAutomation)

pathmodel <- mplusObject(
   TITLE = "MplusAutomation Example - Path Model;",
   MODEL = "
     mpg ON hp;
     wt by disp drat;",
   OUTPUT = "CINTERVAL;",
   rdata = mtcars)

我已经尝试过这种格式,但我不确定哪个不起作用,我不确定如何创建它以便它与管道一起使用。

mplus <- function(data, title, on, by, output) {
  mplusObject(TITLE = as.character(title),
              MODEL = paste(on, "/n", by),
              OUTPUT = as.character(output),
              rdata = data)
  
}

这就是我最终要达到的目标。

mplus %>%
  data(mtcars) %>%
  title("example - path model") %>%
  predictors("mpg on hp") %>%
  latentvars("wt by disp drat") %>%
  output(cinterval)

【问题讨论】:

  • 键入 pathmodel 返回 &gt;Error in summary.mplusObject(x) : isFALSE(is.null(object$results)) is not TRUE。管道用于以有序的方式链接多个功能。没有名为onoutput 等的函数供您使用。这些只是 1 个函数的参数,即mplusObject。您不能通过管道传递参数。
  • 我明白了。我编辑了预期的代码以允许直接引用。对于管道参数,我是否应该创建可以一起使用的新函数。如果是这样,那会是什么样子。即使不是整个解决方案,即使是一个简单的建议也会有所帮助。

标签: r tidyverse magrittr


【解决方案1】:

如果你想拥有 pipable 功能,那么你需要一个 pipeble 对象。在这里,我们只是将值存储在 a 列表中。类似的东西

new_mplus <- function(data=NA) {
  x <- list(TITLE=NA, MODEL=NA, OUTPUT=NA, predictors=NA, latent=NA, rdata=data)
  class(x) <- "mplus"
  x
}

is_mplus <- function(x) {
  "mplus" %in% class(x)
}

mplus <- function(data) {
  stopifnot(is.data.frame(data))
  new_mplus(data)
}

title <- function(x, title) {
  stopifnot(is_mplus(x))
  x$TITLE <- title
  x
}

predictors <- function(x, predictors) {
  stopifnot(is_mplus(x))
  x$predictors <- predictors
  x
}

latentvars <- function(x, latent) {
  stopifnot(is_mplus(x))
  x$latent <- latent
  x
}

output <- function(x, output) {
  stopifnot(is_mplus(x))
  x$OUTOUT <- output
  x
}

然后你调用它

mtcars %>%
  mplus() %>%
  title("example - path model") %>%
  predictors("mpg on hp") %>%
  latentvars("wt by disp drat") %>%
  output("cinterval")

该列表将记录您的所有价值观。然后你只需要一个函数来执行它

execute <- function(x) {
  mplusObject(TITLE = x$TITLE
              MODEL = paste(x$predictors, "/n", x$latent),
              OUTPUT = x$OUTPUT,
              rdata = x$data)
}

mtcars %>%
  mplus() %>%
  title("example - path model") %>%
  predictors("mpg on hp") %>%
  latentvars("wt by disp drat") %>%
  output("cinterval") %>% 
  execute()

管道都是关于将对象从一个函数传递到下一个函数,因此您需要传递某种对象来存储所有值。使用dplyr,您正在传递一个tibble,使用ggplot2,您正在创建一个ggplot 对象。

【讨论】:

    【解决方案2】:

    这是你可以做的让这个函数动态化:

    mplus <- function(data, title, on, by, output) {
      mplusObject(TITLE = title,
                  MODEL = paste(on, "/n", by),
                  OUTPUT = output,
                  rdata = data)
    }
    

    然后将其称为:

    mtcars %>%
      mplus("example - path model", "mpg on hp", "wt by disp drat", "CINTERVAL")
    

    【讨论】:

    • 太棒了。是否可以将mplusObject 函数拆分为多个函数以将其用作管道?
    • 你需要为每个函数创建不同的函数。我认为弗利克先生的回答向您展示了如何。
    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2017-01-17
    • 2020-07-02
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多