简单的问题但如果将数据框的名称保存为函数中的excel文件名,该如何保存?
export_origin <- function(df){
df1 <- unite(df, variable, c(Reaction.Type, Trial, Actual.Total.Seconds))
df2<- dcast(df1, X.nm.~variable, value.var = "X.A.")
fname= paste(df, "xls", sep = ".")
write.xlsx2(df2, file = fname, col.names = TRUE)}
我希望fname = df.xls与我输入的df名称无关,但它将其保存为数据帧中的观察值作为名称。
答案 0 :(得分:2)
我们可以使用deparse(substitute
export_origin <- function(df){
v1 <- deparse(substitute(df))
df1 <- unite(df, variable, c(Reaction.Type, Trial, Actual.Total.Seconds))
df2<- dcast(df1, X.nm.~variable, value.var = "X.A.")
fname= paste(v1, "xls", sep = ".")
write.xlsx2(df2, file = fname, col.names = TRUE)}
}
为了使这个可重复,
export_origin <- function(df){
v1 <- deparse(substitute(df))
paste(v1, "xls", sep=".")
}
export_origin(df)
#[1] "df.xls"