1

I have written code below : I am sure it enters error part but it didn't assign to vector_a. anyone can help me? thanks a lot

vector_a <- c()
for (satr in 1:10) {
  my_gene <- input[satr,]$gene
  tryCatch({
      orgg.inf <- select(org.Hs.eg.db,
              keys = as.vector(my_gene),
              columns=c("SYMBOL","ENSEMBL"),
              keytype="ENSEMBL")
      vector_a <- c(vector_a, orgg.inf$SYMBOL)
},
error = function(e){
  vector_a <- c(vector_a, "NO")
    }
  )
}
MMRA
  • 337
  • 1
  • 3
  • 11
  • 1
    https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – s_baldur Jun 26 '19 at 15:33
  • 2
    Variables you assign inside a function (such as your error function) do not exist outside of that function. This is how R scoping rules work. It's unclear exactly what you are trying to accomplish since we can't run your code. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 26 '19 at 15:37
  • @MrFlick outside a loop there is one line: vector_a <- c() – MMRA Jun 26 '19 at 15:41
  • @Mahshid that line is inside of a function. it is bad practice but you can try ```vector_a <<- c(vector_a, "NO")``` – M-- Jun 26 '19 at 15:45

1 Answers1

1

You can use <<- assignment to access parent environment variables. In your case from error block, it will assign to the parent environment which contains vector_a. But not sure if it is a good practice.

error = function(e){
    vector_a <<- c(vector_a, "NO")
}