R error: Task 1 failed - "argument "x" is missing, with no default"

331 Views Asked by At

I am adapting a model in R for my masters thesis but I get an error that with the help of this forum and google I still could not solve.

This is the line of code that gives the error. It says that it cannot find the argument 'fibre'. This argument is used in previous lines of code and is present in the global environment.

I edited my question based on the result of debugging.

'''

    Compute_env_concentrations_v2_fibre <- function(pts,HL,fibre){
    for(i in 1:ncol(pts)) assign(paste('pts.',colnames(pts)[i],sep=''),pts[,i])
    for(i in 1:ncol(HL)) assign(paste('HL.',colnames(HL)[i],sep=''),HL[,i])

    break.vec1<-c();

    while (any(pts.fin==0)){
    print(paste('# points in pts:',sum(pts.fin == 0),sep=' '))
    print(paste('# points in HL:',ifelse(nrow(HL)!=0,sum(HL.fin==0),0),sep=' '))

    break.vec1<-c(break.vec1,sum(pts.fin == 0));

    if(length(break.vec1)-length(unique(break.vec1))>10) break

    for (j in 1:nrow(pts)) {
    if(pts.fin[j]==0 & pts.upcount[j]==0) {
    if (!is.na(match(pts.basin_id[j], HL.basin_id)) & (pts.lake_out[j] == 1)) { 
      E_total <- HL.E_in[which(HL.Hylak_id == pts.Hylak_id[j])] + pts.E_w[j] + pts.E_up[j] 
      con <<- Compute_env_concentration_fibre_equations(pts, HL, fibre)
      V <- HL.Vol_total[which(HL.Hylak_id == pts.Hylak_id[j])] * 1e6  
      k <- HL.k[which(HL.Hylak_id == pts.Hylak_id[j])]         
      pts.C_w[j] <- E_total / (pts.Q[j] + k * V) * 1e6 / (365*24*3600) 
      
      chem_exchange <- HL.v_bur[which(HL.Hylak_id == pts.Hylak_id[j])]/ HL.v_res[which(HL.Hylak_id == pts.Hylak_id[j])]
     
      pts.C_sd[j] <- pts.C_w[j] * chem_exchange
      
      HL.C_w[which(HL.Hylak_id == pts.Hylak_id[j])] <- pts.C_w[j]
      HL.C_sd[which(HL.Hylak_id == pts.Hylak_id[j])] <- pts.C_sd[j]
      
      HL.fin[which(HL.Hylak_id == pts.Hylak_id[j])] <- 1
      
      pts.E_w_NXT[j] <- pts.C_w[j] * pts.Q[j] * 365 * 24 * 3600 / 1e6 * exp(-pts.k_NXT[j] * pts.dist_nxt[j] / pts.V_NXT[j]) 
      pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] + pts.E_w_NXT[j]  
      pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] - 1
      
    } else if ((pts.Hylak_id[j] == 0) | (pts.lake_out[j] == 1)) { 
      E_total <- pts.E_w[j] + pts.E_up[j] )
      pts.C_w[j] <- E_total / pts.Q[j] * 1e6 / (365*24*3600) 
      
      chem_exchange <- pts.v_bur[j] / pts.v_res[j]
  
      pts.C_sd[j] <- pts.C_w[j] * chem_exchange  
      
      pts.E_w_NXT[j] <- E_total * exp(-pts.k_NXT[j] * pts.dist_nxt[j] / pts.V_NXT[j])  
      pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] + pts.E_w_NXT[j]  
      pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] - 1
      
    } else {
      E_total <- pts.E_w[j] + pts.E_up[j] 
      pts.C_w[j] <- NA
      pts.C_sd[j] <- NA
      
      pts.E_w_NXT[j] <- E_total
      pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.E_up[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] + pts.E_w_NXT[j]  
      pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] <- pts.upcount[which(pts.ID == pts.ID_nxt[j] & pts.basin_id == pts.basin_id[j])] - 1
    }
    
    pts.fin[j] <- 1
    
   }
  
   }
   } 
   if (nrow(HL)!=0 {return(list(pts=data.frame(ID=pts.ID,Pt_type=pts.Pt_type,ID_nxt=pts.ID_nxt,basin_ID=pts.basin_id,x=pts.x,y=pts.y,Q=pts.Q,C_w=pts.C_w,C_sd=pts.C_sd,WWTPremoval=pts.f_rem_WWTP),
     HL=data.frame(Hylak_id=HL.Hylak_id,C_w=HL.C_w,C_sd=HL.C_sd)))
   } else {return(list(pts=data.frame(ID=pts.ID,Pt_type=pts.Pt_type,ID_nxt=pts.ID_nxt,basin_ID=pts.basin_id,x=pts.x,y=pts.y,Q=pts.Q,C_w=pts.C_w,C_sd=pts.C_sd,WWTPremoval=pts.f_rem_WWTP)))
   }
   }

'''

The error occurs when I insert the new equation (con <<- Compute_env_conc_fibre_equations(pts,HL,fibre) into the for-loop. I need to do this because I need the results of E_total to calculate some parameters. These calculations are done within the new function.

I cannot find the solution or the exact problem. I am pretty new to programming. Hope this question is clear and you guys can help me.

Jolien

0

There are 0 best solutions below