'what' must be a function or character string in R error message

1k views Asked by At

I am plotting different umaps. I have a part of the code that worked yesterday, however today I get the error message: Error in do.call(c, lapply(2:ncol(nn_idx), function(i) as.vector(rbind(nn_idx[, : 'what' must be a function or character string"

My code is the following:

read.csv (Tabula, row.names=1)%>% as.matrix()-> counts0
t(t(counts0)/colSums(counts0))-> fracs
log(fracs+ 10^-5) -> lfracs
irlba::prcomp_irlba(t(lfracs), n=30)-> pca
colnames (lfracs) -> rownames (pca$x)
rownames (lfracs) -> rownames (pca$rotation)
pca$x %>% head()
set.seed(12345678)
uwot::umap(pca$x, ret_nn=TRUE, spread=5)-> ump
rownames(pca$x)-> rownames(ump$embedding)
as_tibble(ump$embedding)%>%
  ggplot + geom_point (aes(x=V1, y=V2), size= .5)+ coord_equal()

Now here is the part where I get the error message:

library(igraph)
louvain_clustering <- function (nn_idx){
  do.call (c,lapply (2:ncol(nn_idx), function (i)
    as.vector (rbind (nn_idx[,1], nn_idx[,i]))))->edge_list
  igraph::make_undirected_graph(edge_list)-> graph
  igraph::cluster_louvain(graph)-> clustering
  factor(igraph::membership(clustering))
}

louvain_clustering(ump$nn$euclidean$idx)->louvain

as_tibble(ump$embedding)%>%
  mutate(cluster=louvain)-> Maus
  ggplot(Maus, aes(x=V1, y=V2, col=cluster))+
    geom_point(size=.3)+
    geom_text(aes(label=cluster), col="black",
              data= (Maus %>% group_by (cluster)%>% summarise_all(mean)))+
    coord_equal()

The error message appears on the line

louvain_clustering(ump$nn$euclidean$idx)->louvain

and states: Error in do.call(c, lapply(2:ncol(nn_idx), function(i) as.vector(rbind(nn_idx[, : 'what' must be a function or character string"

I really cannot figure out where my mistake lays, hopefully someone can help. Thank you

1

There are 1 answers

0
Love Eklund On BEST ANSWER

Maybe you have overwritten the primitive function c ? R lets you do that, I was able to replicate your error bellow, and to fix it you can just remove c and it will revert back to the primitive function, so you can try that, please let me know if it fixes your problem.

do.call(c,list(1,2,3))
>> 1 2 3 
c <- 123
do.call(c, list(1,2,3))
>> Error in do.call(c, list(1, 2, 3)) : 
'what' must be a function or character string
rm(c)
do.call(c,list(1,2,3))
>> 1 2 3