我有以下功能:
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'是什么,我的问题是如何通过函数传递这个字符串?
答案 0 :(得分:4)
因为您不使用命名参数,所以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 = "\\"))
}
保持代码简单易于阅读(也适合自己),更易于使用和维护。
答案 1 :(得分:2)
您需要为图文件夹ep>提供值
example_Foo(catg,FigureFolder="FigureFolder")