Reputation: 2356
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))
Upvotes: 0
Views: 794
Reputation: 206401
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) {
[email protected]$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.
Upvotes: 1