Name of data.frame as a text string?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Name of data.frame as a text string?

Mark Knecht
I've passed a data.frame as an input into a function which does some
plotting on a 4x4 matrix of certain parameters within in the
data.frame. I'd like to add a small header on top of each plot with
the name of the data.frame so that it's clear as I compare these 16
things where each on came from.

So far I haven't found the right way to get the name of the data.frame
as a string which I can use in something like mtext. Is there one? If
I put dummy text in, or take the time to pass in the name by hand,
then I do get titles just as I'd like, but I'd far rather let the name
of the data.frame speak for itself.

Thanks,
Mark

______________________________________________
[hidden email] mailing list
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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Ted.Harding-2
On 02-Jul-09 19:00:44, Mark Knecht wrote:

> I've passed a data.frame as an input into a function which does some
> plotting on a 4x4 matrix of certain parameters within in the
> data.frame. I'd like to add a small header on top of each plot with
> the name of the data.frame so that it's clear as I compare these 16
> things where each on came from.
>
> So far I haven't found the right way to get the name of the data.frame
> as a string which I can use in something like mtext. Is there one? If
> I put dummy text in, or take the time to pass in the name by hand,
> then I do get titles just as I'd like, but I'd far rather let the name
> of the data.frame speak for itself.
>
> Thanks,
> Mark

One way to do this (which is how I usually do it) is to set up the
dataframe name as a string variable, and then use this as required.

For instance:

  datafr <- "My1stDF"
  DF <- read.csv(paste(datafr,".csv",sep="")
  {<do things>}
  plot(whatever,main=paste("Data from", datafr),...)

which will read the CSV file whose name corresponds to what you have
set 'datafr' to, and then put a corresponding header into the plot.

This is a particularly useful technique if you want to loop through
several datasets, on the lines of

  DataFrs <- c("My1stDF", "My2ndDFD", "My3rdDF", "My4thDF")
  for( datafr in DataFrs ) {
    {<stuff like the above>}
  }

Several variants of this kind of approach are possible!

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 02-Jul-09                                       Time: 20:21:43
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Don MacQueen
In reply to this post by Mark Knecht
Use
   deparse(substitute(df))

Example from ?deparse:

     function(x, y) {
          plot(x, y, xlab=deparse(substitute(x)),
              ylab=deparse(substitute(y)))
      }

-Don

At 12:00 PM -0700 7/2/09, Mark Knecht wrote:

>I've passed a data.frame as an input into a function which does some
>plotting on a 4x4 matrix of certain parameters within in the
>data.frame. I'd like to add a small header on top of each plot with
>the name of the data.frame so that it's clear as I compare these 16
>things where each on came from.
>
>So far I haven't found the right way to get the name of the data.frame
>as a string which I can use in something like mtext. Is there one? If
>I put dummy text in, or take the time to pass in the name by hand,
>then I do get titles just as I'd like, but I'd far rather let the name
>of the data.frame speak for itself.
>
>Thanks,
>Mark
>
>______________________________________________
>[hidden email] mailing list
>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.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Mark Knecht
In reply to this post by Mark Knecht
Perfect! Thanks!

Cheers,
Mark

On 7/2/09, Phil Spector <[hidden email]> wrote:

> Mark -
>    Take a look at the help file for substitute.
>  For example:
>
>
> > zz = function(x)print(substitute(x))
> > zz(variablename)
> >
>  variablename
>
>                                         - Phil
>
>
>
>  On Thu, 2 Jul 2009, Mark Knecht wrote:
>
>
> >
> > I've passed a data.frame as an input into a function which does some
> > plotting on a 4x4 matrix of certain parameters within in the
> > data.frame. I'd like to add a small header on top of each plot with
> > the name of the data.frame so that it's clear as I compare these 16
> > things where each on came from.
> >
> > So far I haven't found the right way to get the name of the data.frame
> > as a string which I can use in something like mtext. Is there one? If
> > I put dummy text in, or take the time to pass in the name by hand,
> > then I do get titles just as I'd like, but I'd far rather let the name
> > of the data.frame speak for itself.
> >
> > Thanks,
> > Mark
> >
> > ______________________________________________
> > [hidden email] mailing list
> > 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.
> >
> >
>

______________________________________________
[hidden email] mailing list
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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Mark Knecht
In reply to this post by Ted.Harding-2
On 7/2/09, Ted Harding <[hidden email]> wrote:

> On 02-Jul-09 19:00:44, Mark Knecht wrote:
>  > I've passed a data.frame as an input into a function which does some
>  > plotting on a 4x4 matrix of certain parameters within in the
>  > data.frame. I'd like to add a small header on top of each plot with
>  > the name of the data.frame so that it's clear as I compare these 16
>  > things where each on came from.
>  >
>  > So far I haven't found the right way to get the name of the data.frame
>  > as a string which I can use in something like mtext. Is there one? If
>  > I put dummy text in, or take the time to pass in the name by hand,
>  > then I do get titles just as I'd like, but I'd far rather let the name
>  > of the data.frame speak for itself.
>  >
>  > Thanks,
>  > Mark
>
>
> One way to do this (which is how I usually do it) is to set up the
>  dataframe name as a string variable, and then use this as required.
>
>  For instance:
>
>   datafr <- "My1stDF"
>   DF <- read.csv(paste(datafr,".csv",sep="")
>   {<do things>}
>   plot(whatever,main=paste("Data from", datafr),...)
>
>  which will read the CSV file whose name corresponds to what you have
>  set 'datafr' to, and then put a corresponding header into the plot.
>
>  This is a particularly useful technique if you want to loop through
>  several datasets, on the lines of
>
>   DataFrs <- c("My1stDF", "My2ndDFD", "My3rdDF", "My4thDF")
>   for( datafr in DataFrs ) {
>     {<stuff like the above>}
>   }
>
>  Several variants of this kind of approach are possible!
>
>  Hoping this helps,
>  Ted.
>

Yep, doing it that way is very sensible. I've got the name in my hand
at the point I read the file in. Easy to use this way and probably a
good long-term solution.

Cheers,
Mark

______________________________________________
[hidden email] mailing list
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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Mark Knecht
In reply to this post by Mark Knecht
On 7/2/09, Don MacQueen <[hidden email]> wrote:

> Use
>   deparse(substitute(df))
>
>  Example from ?deparse:
>
>     function(x, y) {
>          plot(x, y, xlab=deparse(substitute(x)),
>              ylab=deparse(substitute(y)))
>      }
>
>  -Don
>

Now that's interesting... deparsed means ?? de-analyze ?? I'm not
clear what that's doing for me beyond using only substitute but your
example and the substitute example both use it so I suppose I
shouldn't tempt the Fates...

Thanks,
Mark

______________________________________________
[hidden email] mailing list
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.
markknecht@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Name of data.frame as a text string?

Duncan Murdoch
On 7/2/2009 3:33 PM, Mark Knecht wrote:

> On 7/2/09, Don MacQueen <[hidden email]> wrote:
>> Use
>>   deparse(substitute(df))
>>
>>  Example from ?deparse:
>>
>>     function(x, y) {
>>          plot(x, y, xlab=deparse(substitute(x)),
>>              ylab=deparse(substitute(y)))
>>      }
>>
>>  -Don
>>
>
> Now that's interesting... deparsed means ?? de-analyze ?? I'm not
> clear what that's doing for me beyond using only substitute but your
> example and the substitute example both use it so I suppose I
> shouldn't tempt the Fates...

substitute() returns an expression.  deparse() turns an expression into
a text string.  (So named because parse() turns a text string or strings
into an expression.)

Duncan Murdoch

______________________________________________
[hidden email] mailing list
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.