Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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
将函数结果追加到R中的列表_R_List_Function_Return_Append - Fatal编程技术网

将函数结果追加到R中的列表

将函数结果追加到R中的列表,r,list,function,return,append,R,List,Function,Return,Append,我创建了一些生成绘图的函数。目标是将函数结果存储在一个analysisObjects列表中。analysisObjects是一个列表列表。下面的代码完全按照预期生成结果 createPlot1 <- function(data = mtcars){ plot1 <- ggplot(data, aes(factor(cyl))) + geom_boxplot() + coord_flip() plot2 <- ggplot(data, aes(factor(

我创建了一些生成绘图的函数。目标是将函数结果存储在一个analysisObjects列表中。analysisObjects是一个列表列表。下面的代码完全按照预期生成结果

createPlot1 <- function(data = mtcars){
  
 plot1 <- ggplot(data, aes(factor(cyl))) +
    geom_boxplot() + coord_flip()
 
 plot2 <- ggplot(data, aes(factor(cyl))) +
    geom_boxplot() 
  
  res = list("mycars" = list(list("objType" = "plot",
                                                   "object" =  plot1),
                                              list("objType" = "plot",
                                                   "object" =  plot2)))
  
  analysisObjects <<- append(analysisObjects, res)
  
}


createPlot2 <- function(data = iris){
  
 plot1 <-  ggplot(data, aes(x = Sepal.Length, y = Sepal.Width)) +
   geom_point(aes(color=Species, shape=Species)) 
 
 plot2 <- ggplot(data, aes(x=Species, y=Sepal.Length)) +
   geom_boxplot(aes(fill=Species)) 
  
  res <- list("myflowers" = list(list("objType" = "plot",
                                            "object" =  plot1),
                                       list("objType" = "plot",
                                            "object" =  plot2)))
  
 analysisObjects <<- append(analysisObjects, res)
  
}

此函数将替换line analysisObjects我相信您处理问题的前提是错误的

你为什么不试试这样的东西:

library(ggplot2)

# generic for methods dispatch
createPlot <- function(data){
 
 UseMethod("createPlot")
 
}

# method for class "someclass"
createPlot.someclass <- function(data){
 
 plot1 <- ggplot(data, aes(factor(cyl))) +
  geom_boxplot() + coord_flip()
 
 plot2 <- ggplot(data, aes(factor(cyl))) +
  geom_boxplot() 
 
 list(list("objType" = "plot", "object" =  plot1),
      list("objType" = "plot", "object" =  plot2))
 
}

# method for class "someotherclass"
createPlot.someotherclass <- function(data){
 
 plot1 <-  ggplot(data, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color=Species, shape=Species)) 
 
 plot2 <- ggplot(data, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(fill=Species)) 
 
 list(list("objType" = "plot", "object" =  plot1),
      list("objType" = "plot", "object" =  plot2))

}

# prepare your data and assign the right class. It will be needed for dispatch
class(mtcars) <- c("someclass"     , class(mtcars))
class(iris)   <- c("someotherclass", class(iris))

# make a list of your dataframes and give it a name to each one (if you want to)
mylist <- list(mycars = mtcars, myflowers = iris)

# create your list
analysisObjects <- lapply(mylist, createPlot)
正如您所见,R是优化的,因此即使您创建了另一个对象,它实际上也会调用RAM内存中的相同空间,并且不会无缘无故地复制所有内容

如果编辑i2的一列,那么它将只复制该列

因此,如果您需要分配不同的类并复制数据集,这不是问题

但是,如果对于一个特定的数据集,您需要调用两种不同的方法,您可以这样做:

createPlot.someotherclassyet <- function(data){

  out1 <- createPlot.someclass(data)
  out2 <- createPlot.someotherclass(data)

  c(out1, out2)

}

class(iris) <- c("someotherclassyet", class(iris))
createPlot(iris)
这项工作完成了,但还不是很干净

最好有一个将一个类转换为另一个类的函数,以防将来需要创建检查或额外的转换来生成这样的类

### someclass
as_someclass <- function(x){

  UseMethod("as_someclass")

}

as_someclass.someotherclassyet <- function(x){
  
  class(x) <- setdiff(class(x), "someotherclassyet")
  as_someclass(x)

}

as_someclass.data.frame <- function(x){
  
  class(x) <- c("someclass", class(x))
  x

}



### someotherclass

as_someotherclass <- function(x){

  UseMethod("as_someotherclass")

}

as_someotherclass.someotherclassyet <- function(x){
  
  class(x) <- setdiff(class(x), "someotherclassyet")
  as_someotherclass(x)

}

as_someotherclass.data.frame <- function(x){
  
  class(x) <- c("someotherclass", class(x))
  x

}


### someotherclassyet
as_someotherclassyet <- function(x){

  UseMethod("as_someotherclassyet")

}

as_someotherclassyet.data.frame <- function(x){
  
  class(x) <- c("someotherclassyet", class(x))
  x

}


createPlot.someotherclassyet <- function(data){

  out1 <- createPlot(as_someclass(data))
  out2 <- createPlot(as_someotherclass(data))

  c(out1, out2)

}


lapply(list(myflowers      = as_someclass(iris),
            myotherflowers = as_someotherclass(iris),
            allmyflowers   = as_someotherclassyet(iris),
            mycars         = as_someotherclass(mtcars)),
       createPlot)

如果要在对象列表上执行createPlot,可以创建列表并指定一个类。

我认为您的问题的前提是错误的

你为什么不试试这样的东西:

library(ggplot2)

# generic for methods dispatch
createPlot <- function(data){
 
 UseMethod("createPlot")
 
}

# method for class "someclass"
createPlot.someclass <- function(data){
 
 plot1 <- ggplot(data, aes(factor(cyl))) +
  geom_boxplot() + coord_flip()
 
 plot2 <- ggplot(data, aes(factor(cyl))) +
  geom_boxplot() 
 
 list(list("objType" = "plot", "object" =  plot1),
      list("objType" = "plot", "object" =  plot2))
 
}

# method for class "someotherclass"
createPlot.someotherclass <- function(data){
 
 plot1 <-  ggplot(data, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color=Species, shape=Species)) 
 
 plot2 <- ggplot(data, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(fill=Species)) 
 
 list(list("objType" = "plot", "object" =  plot1),
      list("objType" = "plot", "object" =  plot2))

}

# prepare your data and assign the right class. It will be needed for dispatch
class(mtcars) <- c("someclass"     , class(mtcars))
class(iris)   <- c("someotherclass", class(iris))

# make a list of your dataframes and give it a name to each one (if you want to)
mylist <- list(mycars = mtcars, myflowers = iris)

# create your list
analysisObjects <- lapply(mylist, createPlot)
正如您所见,R是优化的,因此即使您创建了另一个对象,它实际上也会调用RAM内存中的相同空间,并且不会无缘无故地复制所有内容

如果编辑i2的一列,那么它将只复制该列

因此,如果您需要分配不同的类并复制数据集,这不是问题

但是,如果对于一个特定的数据集,您需要调用两种不同的方法,您可以这样做:

createPlot.someotherclassyet <- function(data){

  out1 <- createPlot.someclass(data)
  out2 <- createPlot.someotherclass(data)

  c(out1, out2)

}

class(iris) <- c("someotherclassyet", class(iris))
createPlot(iris)
这项工作完成了,但还不是很干净

最好有一个将一个类转换为另一个类的函数,以防将来需要创建检查或额外的转换来生成这样的类

### someclass
as_someclass <- function(x){

  UseMethod("as_someclass")

}

as_someclass.someotherclassyet <- function(x){
  
  class(x) <- setdiff(class(x), "someotherclassyet")
  as_someclass(x)

}

as_someclass.data.frame <- function(x){
  
  class(x) <- c("someclass", class(x))
  x

}



### someotherclass

as_someotherclass <- function(x){

  UseMethod("as_someotherclass")

}

as_someotherclass.someotherclassyet <- function(x){
  
  class(x) <- setdiff(class(x), "someotherclassyet")
  as_someotherclass(x)

}

as_someotherclass.data.frame <- function(x){
  
  class(x) <- c("someotherclass", class(x))
  x

}


### someotherclassyet
as_someotherclassyet <- function(x){

  UseMethod("as_someotherclassyet")

}

as_someotherclassyet.data.frame <- function(x){
  
  class(x) <- c("someotherclassyet", class(x))
  x

}


createPlot.someotherclassyet <- function(data){

  out1 <- createPlot(as_someclass(data))
  out2 <- createPlot(as_someotherclass(data))

  c(out1, out2)

}


lapply(list(myflowers      = as_someclass(iris),
            myotherflowers = as_someotherclass(iris),
            allmyflowers   = as_someotherclassyet(iris),
            mycars         = as_someotherclass(mtcars)),
       createPlot)

如果你想在对象列表上执行createPlot,你可以创建你的列表并分配一个类。

fortunes::fortune 174运算符这是我怀疑的,也是我寻找其他对象的原因fortunes::fortune 174运算符这是我怀疑的,也是我寻找其他对象的原因我必须研究使用方法来理解您在这里做了什么。结果正如所期望的,这种方法唯一的缺点是classmtcars“它创建了两个新对象”是什么意思?它覆盖了现有的。我的错,我误解了这一点。我想知道如何处理同一数据集在多个实例中使用的情况?假设我还有一个函数createPlot.someotherclassyet,它也使用iris数据集。但是,我希望功能是分开的。这意味着我将在运行lappy之前覆盖该类。我如何处理这个问题?或者如果一个函数需要两个数据集怎么办?iris和mtcars?看看我的编辑。它应该能回答你的问题。我必须研究UseMethod是如何工作的,才能理解你在这里做了什么。结果正如所期望的,这种方法唯一的缺点是classmtcars“它创建了两个新对象”是什么意思?它覆盖了现有的。我的错,我误解了这一点。我想知道如何处理同一数据集在多个实例中使用的情况?假设我还有一个函数createPlot.someotherclassyet,它也使用iris数据集。但是,我希望功能是分开的。这意味着我将在运行lappy之前覆盖该类。我如何处理这个问题?或者如果一个函数需要两个数据集怎么办?iris和mtcars?看看我的编辑。它应该能回答你的问题。