Pass a column name as a variable

Hey guys :slight_smile:
I've written the following function:

prog_f <- function(d_list,colName,caption,gr_color,t_CA,w_t_CA,p_names,group){
  
  maxh <- c()
  minh <- c()
  for(i in 1:length(d_list)){maxh[i] <- max(d_list[[i]]$colName,na.rm=TRUE)}
  for(i in 1:length(d_list)){minh[i] <- -max(-(d_list[[i]]$colName),na.rm=TRUE)}
  
  ymax <- max(maxh)
  ymin <- min(minh)
  
  inter <- abs(ymax-ymin)
  ylim1 <- ymin-0.105*inter
  ylim2 <- ymax+0.1*inter
  
  x.lab <- c("0'","5'","5'","10'","15'","20'")
  
  h <- c(d_list[[1]]$colName[(w_t_CA[1]-6):(w_t_CA[1]+312)])
  for(i in 2:9){h <- rbind(h,c(d_list[[i]]$colName[(w_t_CA[i]-6):(w_t_CA[i]+312)]))}
  mean_bol <- colMeans(h,na.rm=TRUE)
  
  h <- c(d_list[[10]]$colName[(w_t_CA[10]-6):(w_t_CA[10]+312)])
  for(i in 11:19){h <- rbind(h,c(d_list[[i]]$colName[(w_t_CA[i]-6):(w_t_CA[i]+312)]))}
  mean_kon <- colMeans(h,na.rm=TRUE)

  p1 <- plot_ly(x=d_list[[1]]$Time-t_CA[1],y=d_list[[1]]$colName, type="scatter", mode="lines", name = p_names[1], line = list(width = 0.5), color = I(gr_color[1]), legendgroup = p_names[1])%>%
    add_segments(x = 300, xend = 300, y = 0, yend = ylim2, color=I("black"), name= "5 min CA", legendgroup = "5 min CA",showlegend = FALSE)%>%
    add_segments(x = 0, xend = 0, y = ylim1, yend = ylim2, color=I("black"), name= "Start CA", legendgroup = "Start CA",showlegend = FALSE)
  for(k in 2:length(d_list)){
    p1 <- add_trace(p1, x=d_list[[k]]$Time-t_CA[k],y=d_list[[k]]$colName,name = p_names[k], color = I(gr_color[k]), legendgroup = p_names[k])
  }
  p1 <- add_trace(p1,x=seq(from=-30, to=1560, by=5),y=mean_bol, name="Mean b",color = I("blue"), line=list(width=2), legendgroup = "Mean b")
  p1 <- add_trace(p1,x=seq(from=-30, to=1560, by=5), y=mean_kon, name="Mean c",color = I("red"), line=list(width=2), legendgroup = "Mean c")
  p1 <-  layout(p1,
                xaxis = list(autotick = FALSE,ticks = "outside",tickvals=list(0,300,600,900,1200,1500), ticktext=x.lab,tickmode = "array",tickangle = -45,showgrid = T, title = "", range = c(-30,1560)),
                yaxis = list(showgrid = T, range=c(ylim1,ylim2), title=caption)
                #shapes = list(vline(t_CA[1]))
  )
  p1 <- add_annotations(p1, x=-25,yref = 'paper',y=1.05,text="bl",showarrow = FALSE,font = list(size = 12),color=I("grey"),opacity = 1)
  p1 <- add_annotations(p1,x=150,yref = 'paper',y=1.05,text="ca",showarrow = FALSE,font = list(size = 12),color=I("grey"),opacity = 1)
  p1 <- add_annotations(p1, x=930,yref = 'paper',y=1.05,text="ec",showarrow = FALSE,font = list(size = 12),color=I("grey"),opacity = 1)
  
  return(p1)
}

The function works well, as long as I insert the correct column name instead of the variable "colName" everywhere. As soon as I put the variable "colName" into the function (just like above), i get this error-message: Error in -(d_list[[i]]$colName): invalid argument to unary operator. Does anybody know what the problem with this code is? TIA :slight_smile:

Does it work if you try [[colName]] instead of $colName? (The $ operator may require a string rather than a name.)

Thank you very much for your advise! Unfortunatly it doesn't work. I've also tried the following version: d_list[[i]][(w_t_CA[i]-6):(w_t_CA[i]+312), c(colName)], where colName is a string, which doesn't work either. Also paste0('d_list[[i]]$',colName,'[(w_t_CA[i]-6):(w_t_CA[i]+312)]') is not working :frowning:

Could you post the error it gives with my previous suggestion?

Yes of course :slight_smile: I always got the error message "Error in [[.tbl_df(d_list[[i]], colName) : object 'H2' not found" first but then I changed the variable input from H2 to "H2" and now it works! Thank you so much for your help, I would have never solved it by my own. Im so happy right now, thank you so much! :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.