Within a function I'm assigning dynamic variable names and values to them using the "assign" function. I want to pass back the results but am uncertain how to do this. Basically, my function reads a number of data files and uses the filename of each file as the variable name for a list-to-become-dataframe. I want then to pass all these lists back, but again, the names of the lists to pass back have been "assign-ed" to the filename. See code snippet below, especially the "*** WHAT DO I PUT HERE ***" part! Thanks in advance for any help!!! -Scott CODE SNIPPET BELOW: loadFiles <- function() { fullFileNames <- choose.files(filters = c("Fluor file (*.data,*.Dat)", "*.dat;*.Dat")) numFiles <- length(fullFileNames) fileNames <- basename(fullFileNames) # removes the all the paths from the full filenames splitNames <- strsplit(fileNames,"_") # create a "list" of strings separated by the "_" character dfNames <- sapply(splitNames,"[",1) # or I could use "sapply(splitNames, function (x) x[1])" OR EVEN "unlist(lapply(splitNames, function(x)x[1]))" -- [Thanks to Andy Liaw, Simon Blomberg, Gabor Grothendieck, #James Holtman and Robert Keefe for their helpful responses on this question - See posting on returning the first elements of a list of vectors] # for (i in 1:numFiles) { assign(dfNames[[i]],ReadFileCreateDF(fullFileNames[[i]])) # we use "assign" since we want the quoted string in dfNames to be assigned the value of the function call # the ReadFileCreateList is a function that reads an individual file and sends back a number of parameters in a list } return(list( *** WHAT DO I PUT HERE***)) } Scott Norton, Ph.D. Engineering Manager Nanoplex Technologies, Inc. 2375 Garcia Ave. Mountain View, CA 94043 www.nanoplextech.com [[alternative HTML version deleted]]
One thing you could do the assign() directly into the global workspace, i.e. assign(dfNames[[i]],ReadFileCreateDF(fullFileNames[[i]]), envir = globalenv()) -roger Scott Norton wrote:>Within a function I'm assigning dynamic variable names and values to them >using the "assign" function. I want to pass back the results but am >uncertain how to do this. > > > >Basically, my function reads a number of data files and uses the filename of >each file as the variable name for a list-to-become-dataframe. I want then >to pass all these lists back, but again, the names of the lists to pass back >have been "assign-ed" to the filename. See code snippet below, especially >the "*** WHAT DO I PUT HERE ***" part! > > > >Thanks in advance for any help!!! > > > >-Scott > > > > > >CODE SNIPPET BELOW: > > > >loadFiles <- function() > >{ > > > > fullFileNames <- choose.files(filters = c("Fluor file (*.data,*.Dat)", >"*.dat;*.Dat")) > > numFiles <- length(fullFileNames) > > fileNames <- basename(fullFileNames) # removes the all the paths from the >full filenames > > splitNames <- strsplit(fileNames,"_") # create a "list" of strings >separated by the "_" character > > > > dfNames <- sapply(splitNames,"[",1) > > # or I could use "sapply(splitNames, function (x) x[1])" OR EVEN >"unlist(lapply(splitNames, function(x)x[1]))" -- [Thanks to Andy Liaw, >Simon Blomberg, Gabor Grothendieck, > > #James Holtman and Robert Keefe for their helpful responses on this >question - See posting on returning the first elements of a list of vectors] > > # > > > > for (i in 1:numFiles) > > { > > assign(dfNames[[i]],ReadFileCreateDF(fullFileNames[[i]])) ># we use "assign" since we want the quoted string in dfNames to be assigned >the value of the function call > > # the ReadFileCreateList is a function that reads an >individual file and sends back a number of parameters in a list > > } > > return(list( *** WHAT DO I PUT HERE***)) > > } > > > > > >Scott Norton, Ph.D. > >Engineering Manager > >Nanoplex Technologies, Inc. > >2375 Garcia Ave. > >Mountain View, CA 94043 > >www.nanoplextech.com > > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > >
On Thu, 16 Oct 2003, Scott Norton wrote:> Within a function I'm assigning dynamic variable names and values to them > using the "assign" function. I want to pass back the results but am > uncertain how to do this. > > > > Basically, my function reads a number of data files and uses the filename of > each file as the variable name for a list-to-become-dataframe. I want then > to pass all these lists back, but again, the names of the lists to pass back > have been "assign-ed" to the filename. See code snippet below, especially > the "*** WHAT DO I PUT HERE ***" part!This looks like a compromise between two strategies, either of which would work on its own 1/ return a list of dataframes with tags based on the filenames 2/ create new variables with names based on the filenames. The former approach is easy. The latter requires assigning into a non-local environment, which is normally considered harmful but here is the whole point of the command (as with R's load() function). You might want to specify where the variables are to be created (as load() does). I've written it below so that the default location is the calling environment, and you would use loadFiles2(where=.GlobalEnv) to load into the global environment. So either loadFiles1 <- function() { fullFileNames <- choose.files(filters = c("Fluor file (*.data,*.Dat)", "*.dat;*.Dat")) numFiles <- length(fullFileNames) fileNames <- basename(fullFileNames) splitNames <- strsplit(fileNames,"_") dfNames <- sapply(splitNames,"[",1) rval <-lapply(fullFileNames, ReadFileCreateDF) names(rval) <- dfNames return(rval) } or loadFiles2 <- function(where=parent.frame()) { fullFileNames <- choose.files(filters = c("Fluor file (*.data,*.Dat)", "*.dat;*.Dat")) numFiles <- length(fullFileNames) fileNames <- basename(fullFileNames) splitNames <- strsplit(fileNames,"_") dfNames <- sapply(splitNames,"[",1) for (i in 1:numFiles) assign(dfNames[i], ReadFileCreateDF[i], envir=where) invisible(NULL) }