Error in sum(X) : invalid 'type' (list) of argument

148 views Asked by At

I am in need of help with this problem.

I have been using this script for about 2 years with no issues. However, I recently had to format my PC and therefore reinstall R and RStudio.

The code is the following:

hill<-function(x,y,z) {
  require(ggplot2)
  require(reshape2)
  require(SpadeR)
  comm<-x[which(x$Localidade==paste(y)),]
  comm<-dcast(comm,Ponto~Sp)
  comm<-comm[!rowSums(comm[,-1])<10,]
  hill2<-data.frame()
  for (i in 1:nrow(comm)) {
    hill_new<-cbind(data.frame(Diversity(comm[i,c(3:ncol(comm))],datatype=c("abundance"))$Hill_numbers[,c(1,5)]),rep(comm$Ponto[i],13))
    colnames(hill_new)[3]<-"Ponto"
    hill2<-rbind(hill2,hill_new)
  }
  ggplot()+
    geom_line(aes(x=hill2$q,y=hill2$Empirical,color=hill2$Ponto),size=2.0)+
    xlab("q")+
    ylab("Hill")+
    ggtitle(paste(z))+
    theme(panel.background = element_rect(fill = "white", colour = "black", size = 0.5), # opcoes graficas
          panel.grid.major = element_line(colour = NA),
          panel.grid.minor = element_line(colour = NA),
          legend.position = c(0.90, 0.75),
          plot.title=element_text(hjust=0.5,vjust=3,margin=margin(t=60,b=-50),
                                  colour = "black", size = 30, face = "bold"),
          axis.text = element_text(colour = "black", size = 30),
          axis.title = element_text(colour = "black", size = 28, face = "bold"),
          legend.title = element_blank(),
          legend.background = element_rect(fill = "white"),
          legend.key.size = unit(3,"line"),
          legend.text = element_text(colour = "black", size = 26))
  ggsave(filename=paste(paste(z),"-output.tiff", sep=""),dpi=200,width=50,height=45,units="cm")
  write.table(hill,file=paste(paste(z),"-output.txt",sep=""))
}

I have ran the code line by line to find where the error falls, and it seems to be related to this specific function: Diversity(comm[i,c(3:ncol(comm))],datatype=c("abundance")

And then the error "Error in sum(X) : invalid 'type' (list) of argument" shows up.

From my limited knowledge on this, it seems that the coding is not reading my data, and not able to create objects and run the analysis. However, the data is in the R enviroment. I have tried all I could think of (reinstalling the software with last and older versions, checked the packages versions), but no luck. I have tried with other datasets where I've successfully ran the analisys (same script), and this error is there. First I thought it might be something wrong with my PC, but tried to run on another machine (both Windows 10 OS) and the same error appears.

Package versions: ggplot2 3.4.2 reshape2 1.4.4 SpadeR 0.1.1 R versions 4.2.3 ; 4.3.0 ; 4.3.1 (tried all of them) RStudio versions 2022.07.2-576 ; 2023.06.0-421

Any guesses what might be wrong? Kind regards.

I have ran the code line by line to find where the error falls, and it seems to be related to this specific function: Diversity(comm[i,c(3:ncol(comm))],datatype=c("abundance")

And then the error "Error in sum(X) : invalid 'type' (list) of argument" shows up.

Expecting the analysis to run as it always did, creating the plot and output.

0

There are 0 answers