【问题标题】:retaining entered variable names inside a custom function in r在 r 中的自定义函数中保留输入的变量名称
【发布时间】:2018-02-19 16:11:19
【问题描述】:

我试图弄清楚如何保留在自定义函数中作为参数输入的变量名。我想允许用户以(data = df, x = x, y = y)(data = NULL, x = df$x, y = df$y) 两种不同的方式向函数输入参数,并尝试在任何一种情况下准备标签。

# libraries needed
library(dplyr)
library(rlang)
library(datasets)

# defining the custom function
# allow two ways to enter the arguments to the function-
# (data = df, x = x, y = y) or (data = NULL, x = df$x, y = df$y)

prac.fn <- function(data = NULL, x, y) {
  #===================== creating labels out of entered variables ===============
  if (!is.null(data)) {
    # if dataframe is provided
    lab.df <- colnames(dplyr::select(.data = data,
                                     !!rlang::enquo(x),
                                     !!rlang::enquo(y)))
    xlab <- lab.df[1]
    ylab <- lab.df[2]
  } else {
    # if vectors were provided
    # split the name of the entered variable by `$` sign and 
    # select the second part of the split string
    xlab <- strsplit(quote(x), "$", fixed = TRUE)[[1]][[2]]
    ylab <- strsplit(quote(y), "$", fixed = TRUE)[[1]][[2]]
  }
  print(xlab)
  print(ylab)
}

# checking if the code works
# using the `data` argument (this works!)
prac.fn(data = iris, x = Species, y = Sepal.Length)
#> [1] "Species"
#> [1] "Sepal.Length"

# without using the `data` argument (this doesn't work)
prac.fn(x = iris$Species, y = iris$Sepal.Length)
#> Error in strsplit(quote(x), "$", fixed = TRUE): non-character argument

# desired output
# xlab should be assigned the character 'Species'
# xlab should be assigned the character 'Sepal.Length'

reprex package (v0.2.0) 于 2018 年 2 月 19 日创建。

【问题讨论】:

    标签: r eval rlang


    【解决方案1】:

    编辑

    你要找的是deparse(substitute(x)),你的函数可以这样写:

    prac.fn <- function(data = NULL, x, y, xlab=deparse(substitute(x)), ylab=deparse(substitute(y))) {
    
      if (!is.null(data)) {
        # if dataframe was provided
        df <- data.frame(x=data[,xlab], y=data[,ylab])
    
      } else {
        # if vectors were provided
        df <- data.frame(x = x, y = y)
        xlab <- gsub(".*\\$", "", xlab)
        ylab <- gsub(".*\\$", "", ylab)
      }
      print(df)
      print(xlab)
      print(ylab)
    }
    

    那么,prac.fn(x = iris[1:5,]$Species, y = iris[1:5,]$Sepal.Length)prac.fn(data = iris[1:5,], x = Species, y = Sepal.Length) 给:

           x   y
    1 setosa 5.1
    2 setosa 4.9
    3 setosa 4.7
    4 setosa 4.6
    5 setosa 5.0
    [1] "Species"
    [1] "Sepal.Length"
    

    老答案

    我不确定是否正确理解了您的问题,但这里有一个非常简化的代码版本,可能会对您有所帮助:

    prac.fn <- function(data = NULL, xlab="x", ylab="y") {
    
      df <- data[, c(xlab, ylab)]
      colnames(df) <- c("x", "y")
    
      print(df)
      print(xlab)
      print(ylab)
    }
    
    prac.fn(data = iris[1:5,], xlab = "Species", ylab = "Sepal.Length")
    

    【讨论】:

    • 是的,这有效,但仅在用户可以以(data = df, x = "x", y = "y") 格式输入参数的上下文中。这是我试图避免的格式,而是希望用户以(data = df, x = x, y = y)(x = df$x, y = df$y) 格式输入参数。
    • 我稍微简化了我的问题。看看有没有帮助。
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2021-12-10
    • 2022-01-13
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多