Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Export variable that is named via another variable out of a function

Given this sample code,

gene_expression<-function(gene) {
assign((paste0(gene,"_graph")),5)
}

an input of "ACTB" should create a variable that is named "ACTB_graph" and have a value of 5. I would like to be able to export this variable and value out of the function to store for later analysis.

I’ve tried various permutations of the global operator (<<-) but can’t seem to figure it out!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When using assign in a user-defined function, you need to add envr = .GlobalEnv. (Also, need to adjust your parentheses)

gene_expression <- function(gene) {
  assign(paste0(gene,"_graph"), 5, envir = .GlobalEnv)
}

gene_expression("ACTB")

This will store it as an object in the global environment, just like ACTB_graph <- 5 would do.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading