2

我有以下功能:

example_Foo <- function( ...,FigureFolder){

  # check what variables are passed through the function  
  v_names <- as.list(match.call())
  variable_list <- v_names[2:(length(v_names)-2)] 

  # create file to store figures
  subDir <- c(paste(FigureFolder,"SavedData",sep = "\\"))

}

显然这只是函数的开始,但我已经遇到了一些问题。在这里,我试图定义我最终希望保存结果的目录。使用该函数的一个例子是:

weight <- c(102,20,30,04,022,01,220,10)
height <- c(102,20,30,04,022,01,220,10)

catg <- c(102,20,30,04,022,01,220,10)
catg <- matrix(height,nrow = 2)

FigureFolder <- "C:\\exampleDat"

# this is the function
example_Foo(catg,FigureFolder)

这会产生以下错误:

Error in paste(FigureFolder, "SavedData", sep = "\\") : 
  argument "FigureFolder" is missing, with no default

我猜这是由于函数不知道'FigureFolder'是什么,我的问题是如何通过函数传递这个字符串?

4

2 回答 2

5

因为您不使用命名参数,所以该FigureFolder参数被放入.... 只需使用:

example_Foo(catg, FigureFolder = FigureFolder)

此外:

example_Foo <- function( ...,FigureFolder){

  # check what variables are passed through the function  
  v_names <- as.list(match.call())
  variable_list <- v_names[2:(length(v_names)-2)] 

  # create file to store figures
  subDir <- c(paste(FigureFolder,"SavedData",sep = "\\"))

}

也可以替换为:

example_Foo <- function( ...,FigureFolder){

  # check what variables are passed through the function  
  variable_list = list(...)

  # create file to store figures
  subDir <- c(paste(FigureFolder,"SavedData",sep = "\\"))

}

或者更简单:

example_Foo <- function(variable_list, FigureFolder){
  # create file to store figures
  subDir <- c(paste(FigureFolder,"SavedData",sep = "\\"))

}

保持你的代码简单可以让你更容易阅读(也为你自己)并且更容易使用和维护。

于 2013-04-24T07:07:40.487 回答
2

您需要为 FigureFolder 提供一个值,例如

example_Foo(catg,FigureFolder="FigureFolder")
于 2013-04-24T07:07:33.517 回答