0

I have a Seurat single-cell gene expression object, which has slots.

One of the slots is @meta.data, which is a matrix.

I'd like to create a column $orig.ident by assigning it the value of meta$brain.region as a factor. meta is my original table of metadata.

I'm doing this for a bunch of datasets and would like to make it generalizable.

The idea is that the user would only have to enter the name of the original object, and everything from then on would be called accordingly.

User prompt:

> dataset <- "path/to/gw14.RData"

> seurat.obj <- "gw14"

The workspace is then loaded, which includes the Seurat object gw14.

> load(dataset)

> seurat.obj.new <- paste0(seurat.obj, ".", 2)

I don't understand why using get here returns the error below:

> get(seurat.obj.new)@meta.data$orig.ident <- factor(meta$brain.region)

Error in get(seurat.obj.new)@meta.data$orig.ident = factor(meta$brain.region) : 
  could not find function "get<-"

Whereas using it here works as expected:

> assign(seurat.obj.new, CreateSeuratObject(raw.data = get(seurat.obj)@raw.data, 
                                          min.cells = 0, min.genes = 0, project=age))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
  • 3
    When asking for help, you should 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. The special `@<-` and `%<-` functions are going to choke with `get()`. Yet another reason [get/assign](https://stackoverflow.com/questions/17559390/why-is-using-assign-bad) should be avoided. Why are you using get? Do you have a bunch of objects? It would be easier to work with if they were in a named list. – MrFlick Aug 30 '18 at 21:31
  • Thank you @MrFlick. I'm doing this for a bunch of datasets and would like to make it generalizable. The idea is that the user would only have to enter the name of the original object, and everything from then on would be called accordingly. – Carmen Sandoval Aug 30 '18 at 22:04
  • They have to enter the name of the object as a string? Why not write a proper function that they can pass their data too. Then it would be much easier to generalize. – MrFlick Aug 30 '18 at 22:10
  • The object (gw14) is contained in an .RData file, which is the first thing the user will enter. After that, the prompt for name of the object is to tell R which object from the workspace to take. – Carmen Sandoval Aug 30 '18 at 22:15

1 Answers1

1

First, just write a function that assumes you pass in the actual data object and returns an updated data object. For example

my_fun <- function(x) {
  x@meta.data$orig.ident <- factor(meta$brain.region)
  x
}

Then normally you would call it like this

gw14.2 <- my_fun(gw14)

Note functions in R should return take a value and return an updated value. They should not have side effects like creating variables. That should be in the user''s control.

If you did want to work with the data objects as strings, you could do

seurat.obj <- "gw14"
seurat.obj.new <- paste0(seurat.obj, ".", 2)
assign(seurat.obj.new, my_fun(get(seurat.obj)))

But this type of behavior is not consistent with how most R functions operate.

MrFlick
  • 195,160
  • 17
  • 277
  • 295