Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无效';envir';类型为'的参数;字符';——在具有格直方图的自定义函数中_R_Error Handling_Lattice - Fatal编程技术网

无效';envir';类型为'的参数;字符';——在具有格直方图的自定义函数中

无效';envir';类型为'的参数;字符';——在具有格直方图的自定义函数中,r,error-handling,lattice,R,Error Handling,Lattice,我想要一个带有参数的函数,比如数据名(dat)、因子(myfactor)、变量名(myvar),以便动态生成直方图(必须使用lattice) 以IRIS为例: data(iris) my_histogram <- function(myvar,myfactor,dat){ listofparam <- c(myvar,myfactor) myf <- as.formula(paste("~",paste(listofparam,collapse="|"))

我想要一个带有参数的函数,比如数据名(dat)、因子(myfactor)、变量名(myvar),以便动态生成直方图(必须使用lattice)

以IRIS为例:

data(iris)



my_histogram <- function(myvar,myfactor,dat){
    listofparam <- c(myvar,myfactor)
    myf <- as.formula(paste("~",paste(listofparam,collapse="|")))
    histogram(myf,
              data=dat,
              main=bquote(paste(.(myvar),"distribution by",.(myfactor),seq=" ")))}



my_histogram("Sepal.Length","Species","iris") 
数据(iris)

我的_直方图读者应该意识到,这个问题已经从早期版本完全变异,不再与这个答案匹配。新问题的答案出现在评论中

没有名为
萼片.Length
的对象。(因此,R甚至在调用
my_函数
之前就创建了一个错误。)只有一个列名,需要引用它才能将其传递给函数。(无法创建数据对象,因为该URL无法传递数据。为什么不使用iris数据对象的内置副本


您还需要从
myvar
fac
构建一个公式。公式是表达式,可以在不计算其标记的情况下进行解析。您需要在函数中构建一个类似于:
~Sepal.Length | Species
的公式,然后将其传递给直方图调用。请参阅
?as.formula
histogram(myf,data=dat)
我现在有效地回答了关于两个不同错误的两个不同问题。
my_histogram <- function(myvar,myfactor,dat){
listofparam <- c(myvar,myfactor)
myf <- as.formula(paste("~",paste(listofparam,collapse="|")))
p <- do.call("histogram",
   args = list(myf,
   data=dat))
print(p)

}

my_histogram("Sepal.Length","Species","iris")