在R中创建函数时添加参数

时间:2021-10-12 16:04:55

I am a beginner in R. For example i have a function called w whose code is listed below:

我是R的初学者。例如,我有一个名为w的函数,其代码如下所示:

    w<-function(x){
       a<-x+2
       plot(a)
      }

How can i add these arguments in the function so that

我如何在函数中添加这些参数,以便

  1. Export: a number equal to 0 if result should be allowed on screen and to 1 if the result should be printed in a text file.

    导出:如果屏幕上允许结果,则数字等于0;如果结果应打印在文本文件中,则输出为1。

  2. Tfile: name of the text file where the results will be written to;

    Tfile:将写入结果的文本文件的名称;

  3. Gfile:name of the pdf file where the graph will be written to.

    Gfile:将写入图形的pdf文件的名称。

2 个解决方案

#1


To include further arguments in a function, simply list them in function().

要在函数中包含更多参数,只需在function()中列出它们。

w <- function(x, export, tfile, gfile) {
    a <- x + 2
    if (export == 1) {
        write.csv(a, file = tfile)
    } else if (export == 0) {
        pdf(file = gfile)
        plot(a)
        dev.off()
    }
}

For more information on writing and debugging functions in R, see this article.

有关在R中编写和调试函数的更多信息,请参阅此文章。

#2


As the others have pointed out, just separate additional arguments with commas. You can have as many as you want.

正如其他人所指出的那样,只需用逗号分隔其他参数即可。你可以拥有任意数量的东西。

w <- function(x, export, tfile, gfile)

Assigning values within the function definition allows them to have default arguments, so you can choose not to include them

在函数定义中分配值允许它们具有默认参数,因此您可以选择不包含它们

w <- function(x, export = 0, tfile = "w.csv", gfile = "w.pdf")

I will add that a handy thing for plot functions (and many other functions) is the ellipsis ... construct, which basically means "any other relevant arguments". For instance, doing something like this allows you to optionally pass further graphical parameters to your plot (e.g. label names, title).

我将补充说,绘图函数(和许多其他函数)的一个方便的东西是省略号...构造,它基本上意味着“任何其他相关的参数”。例如,执行此类操作允许您选择将更多图形参数传递到绘图(例如标签名称,标题)。

w <- function(x, ...){
    a <- x + 2
    plot(a, ...)
}
w(10, main="Main title")
w(15, type='l', col='red')

#1


To include further arguments in a function, simply list them in function().

要在函数中包含更多参数,只需在function()中列出它们。

w <- function(x, export, tfile, gfile) {
    a <- x + 2
    if (export == 1) {
        write.csv(a, file = tfile)
    } else if (export == 0) {
        pdf(file = gfile)
        plot(a)
        dev.off()
    }
}

For more information on writing and debugging functions in R, see this article.

有关在R中编写和调试函数的更多信息,请参阅此文章。

#2


As the others have pointed out, just separate additional arguments with commas. You can have as many as you want.

正如其他人所指出的那样,只需用逗号分隔其他参数即可。你可以拥有任意数量的东西。

w <- function(x, export, tfile, gfile)

Assigning values within the function definition allows them to have default arguments, so you can choose not to include them

在函数定义中分配值允许它们具有默认参数,因此您可以选择不包含它们

w <- function(x, export = 0, tfile = "w.csv", gfile = "w.pdf")

I will add that a handy thing for plot functions (and many other functions) is the ellipsis ... construct, which basically means "any other relevant arguments". For instance, doing something like this allows you to optionally pass further graphical parameters to your plot (e.g. label names, title).

我将补充说,绘图函数(和许多其他函数)的一个方便的东西是省略号...构造,它基本上意味着“任何其他相关的参数”。例如,执行此类操作允许您选择将更多图形参数传递到绘图(例如标签名称,标题)。

w <- function(x, ...){
    a <- x + 2
    plot(a, ...)
}
w(10, main="Main title")
w(15, type='l', col='red')