vallenderlab / MicrobiomeR

A comprehensive and customizable R package for microbiome analysis.
https://vallenderlab.github.io/MicrobiomeR/
MIT License
20 stars 4 forks source link

Make a better way to access our "private variables". #8

Closed grabear closed 5 years ago

grabear commented 5 years ago

@grabear commented on Fri Dec 07 2018

Our current way of doing this is using a function and adding the data to the global environment with:

# A function for accessing variables.
get_static_data <- function(...) {
  data <- list()
  # Create a list for validating metacoder observation data
  basic_names <- c("otu_abundance", "otu_annotations")
  analyzed_names <- c("taxa_abundance", "otu_proportions", "taxa_proportions")
  all_names <- c(basic_names, analyzed_names)
  valid_list <- list(basic = basic_names, analyzed = analyzed_names, all = all_names)
  # Create various rank based lists
  ranks <- list(c("Kingdom"), c("Phylum"), c("Class"), c("Order"), c("Family"), c("Genus"), c("Species"))
  rank_index <- list("Kingdom" = 1, "Phylum" = 2, "Class" = 3, "Order" = 4, "Family" = 5, "Genus" = 6, "Species" = 7)
  mc_df_rank_list <- list("Kingdom" = 4, "Phylum" = 5, "Class" = 6, "Order" = 7, "Family" = 8, "Genus" = 9, "Species" = 10)
  format_list <- list("phyloseq_format" = 0, "raw_format" = 1, "basic_format" = 2, "analyzed_format" = 3)
  data <- list(valid_list = valid_list, ranks = ranks, rank_index = rank_index, mc_df_rank_list = mc_df_rank_list)
  list2env(data[...], envir = .GlobalEnv)
}

However a more elegant solution might be to create a "package environment" (link so that this data does not easily disrupt the end users experience.

sdhutchins commented 5 years ago

What's your overall vision for this? To just include our data in the package?

grabear commented 5 years ago

No, I just wanted a way to access "private variables". So that they can be used in our functions without being accessible outside of our package.

What I'm doing now:

pkg.env <- new.env(parent = emptyenv())

pkg.env$format_level_list <- list(unknown_format = -1, mixed_format = -1,
                                  phyloseq_format = 0, raw_format = 1,
                                  basic_format = 2, analyzed_format = 3)

format_table_list <- list(
  raw_format =  c("otu_abundance", "otu_annotations"),
  basic_format = c(raw_names, "taxa_abundance", "otu_proportions", "taxa_proportions"),
  analyzed_format = c(basic_names, "statistical_data", "stats_tax_data"),
  phyloseq_format = c("otu_table", "tax_data", "sample_data", "phy_tree")
)
# etc. etc.

# and then
random_func <- function() {
  print(pkg.env$format_table_list$analyzed_format)
}
sdhutchins commented 5 years ago

Funny enough, I found myself looking for similar yesterday. I thought I'd read a while back about how to access private variables, but I've been drawing a blank on it.

I think the above method is a good workaround though.

grabear commented 5 years ago

https://stackoverflow.com/questions/41582136/r-what-do-you-call-the-and-operators-and-how-do-they-differ?answertab=active#tab-top

grabear commented 5 years ago

Implemented with: 2869a438e82cadf342c2730b5a7d08358e9f324a