[R] Accessing an object using a string

William Dunlap wdunlap at tibco.com
Mon Aug 15 21:10:15 CEST 2016


'names' is an S3-generic function.  E.g., in R-3.3.0:

  > names.unnamed <- function(x) sprintf("#%0*d",
ceiling(log10(length(x))), seq_along(x))
  > u <- structure(letters, class="unnamed")
  > names(u)
   [1] "#01" "#02" "#03" "#04" "#05" "#06" "#07"
   [8] "#08" "#09" "#10" "#11" "#12" "#13" "#14"
  [15] "#15" "#16" "#17" "#18" "#19" "#20" "#21"
  [22] "#22" "#23" "#24" "#25" "#26"

There are a lot of C/C++ based functions that use an internal function to
get
the names of vectors and they may not use this method, but R code will use
it.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Aug 15, 2016 at 11:33 AM, Greg Snow <538280 at gmail.com> wrote:

> The names function is a primitive, which means that if it does not
> already do what you want, it is generally not going to be easy to
> coerce it to do it.
>
> However, the names of an object are generally stored as an attribute
> of that object, which can be accessed using the attr or attributes
> functions.  If you change your code to not use the names function and
> instead use attr or attributes to access the names then it should work
> for you.
>
>
> You may also want to consider changing your workflow to have your data
> objects read into a list rather than global variables, then process
> using lapply/sapply (this would require a change in how your data is
> saved from your example, but if you can change that then everything
> after can be cleaner/simpler/easier/more fool proof/etc.)
>
>
> On Mon, Aug 15, 2016 at 2:49 AM,  <G.Maubach at weinwolf.de> wrote:
> > Hi All,
> >
> > I would like to access an object using a sting.
> >
> > # Create example dataset
> > var1 <- c(1, 2, 3)
> > var2 <- c(4, 5, 6)
> > data1 <- data.frame(var1, var2)
> >
> > var3 <- c(7, 8, 9)
> > var4 <- c(10, 11, 12)
> > data2 <- data.frame(var3, var4)
> >
> > save(file = "c:/temp/test.RData", list = c("data1", "data2"))
> >
> > # Define function
> > t_load_dataset <- function(file_path,
> >                            file_name) {
> >   file_location <- file.path(file_path, file_name)
> >
> >   print(paste0('Loading ', file_location, " ..."))
> >   cat("\n")
> >
> >   object_list <- load(file = file_location,
> >                       envir = .GlobalEnv)
> >
> >   print(paste(length(object_list), "dataset(s) loaded from",
> > file_location))
> >   cat("\n")
> >
> >   print("The following objects were loaded:")
> >   print(object_list)
> >   cat("\n")
> >
> >   for (i in object_list) {
> >     print(paste0("Object '", i, "' in '", file_name, "' contains:"))
> >     str(i)
> >     names(i)  # does not work
> >   }
> > }
> >
> > I have only the character vector object_list containing the names of the
> > objects as strings. I would like to access the objects in object_list to
> > be able to print the names of the variables within the object (usuallly a
> > data frame).
> >
> > Is it possible to do this? How is it done?
> >
> > Kind regards
> >
> > Georg
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538280 at gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list