简体   繁体   English

如何在引号中引用 function arguments?

[英]How to refer to the function arguments in quotes?

I'm specifying a function to plot graphs like this:我将 function 指定为 plot 图形,如下所示:

func.plot <- function(z){
  df %>%
    ggplot(aes(z)) + 
    geom_histogram(aes(y =..density..), 
                   binwidth = 0.004,
                   col="red", 
                   fill="green", 
                   alpha=.2) + 
    geom_density(col=2) + 
    labs(title="title", x="z", y="vertical axis")
}

The purpose is to produce histogram plots for a few variables in the data set, hence in this function I also want to use the variable names for the titles of the x axis so as to make the plots differentiated.目的是为数据集中的一些变量生成直方图,因此在这个 function 中,我还想使用变量名称作为x轴的标题,以便使图区分开来。 However, when calling the function with a variable such as func.plot(var) there's an error saying:但是,当使用诸如func.plot(var)之类的变量调用 function 时,会出现错误提示:

Don't know how to automatically pick scale for object of type tbl_ts/tbl_df/tbl/data.frame.不知道如何为 tbl_ts/tbl_df/tbl/data.frame 类型的 object 自动选择比例。 Defaulting to continuous.默认为连续。

Error: Aesthetics must be either length 1 or the same as the data (883): x错误:美学长度必须为 1 或与数据 (883) 相同:x

I've got a few questions:我有几个问题:

  1. For this specific function, how to fix it?对于这个特定的 function,如何解决?

  2. More generally, at times I want to write functions that refer to the argument in quotes, such as the x title above.更一般地说,有时我想编写引用引号中的参数的函数,例如上面的 x 标题。 Another simple example will be to read or write data like this:另一个简单的例子是像这样读取或写入数据:

func.write <- function(x){
  write.csv(x, file="x.csv", row.names=FALSE)
}

This function is also not implemented properly when called out with func.write(df) .当使用func.write(df)调用时,此 function 也未正确实现。 It will write the data but under the name of "x.csv" .它将写入数据,但名称为"x.csv"

Depending on how you want to pass the input arguments you can use one of the below function.根据您想要传递输入 arguments 的方式,您可以使用以下 function 之一。

To pass quoted arguments:要通过引用的 arguments:

library(ggplot2)
library(rlang)

func.plot_quoted <- function(df, z){
   df %>%
     ggplot(aes(!!sym(z))) + 
     geom_histogram(aes(y =..density..), 
                        binwidth = 0.004,
                        col="red", 
                        fill="green", 
                        alpha=.2) + 
     geom_density(col=2) + 
     labs(title="title", x=z, y="vertical axis")
}

which can be used as可以用作

func.plot_quoted(mtcars, "cyl")

and to pass unquoted arguments并通过未引用的 arguments

func.plot_unquoted <- function(df, z){
    df %>%
      ggplot(aes({{z}})) + 
      geom_histogram(aes(y =..density..), 
                     binwidth = 0.004,
                     col="red", 
                     fill="green", 
                     alpha=.2) + 
      geom_density(col=2) + 
      labs(title="title", x={{z}}, y="vertical axis")
} 

which can be used as可以用作

func.plot_unquoted(mtcars, cyl)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM