Can you turn an input into a variable name?

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

Can you turn an input into a variable name?

April Ettington
Let's say I am parsing a file with a list of parameters followed by an
equal sign, and their corresponding values, eg:

color=green
shape=circle

and I want to use this information to create a variable called color with
the value 'green' and a variable shape with the value 'circle'.  However, I
also want my code to be able to do this *when it doesn't know up front what
the parameter names will be.  *So, if the file also included "age=7", it
should still make a variable called age with the value '7', even though
'age' doesn't specifically appear anywhere in my code.  Is there a way to
do this?

Thank you,
April

        [[alternative HTML version deleted]]

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

Re: Can you turn an input into a variable name?

Jeff Newmiller
Yes. But you should be careful.

"source" is the best way, especially if you put the symbols in a dedicated environment instead of the global environment to avoid your program getting stomped on by your input file.

On October 3, 2019 11:58:51 PM PDT, April Ettington <[hidden email]> wrote:

>Let's say I am parsing a file with a list of parameters followed by an
>equal sign, and their corresponding values, eg:
>
>color=green
>shape=circle
>
>and I want to use this information to create a variable called color
>with
>the value 'green' and a variable shape with the value 'circle'.
>However, I
>also want my code to be able to do this *when it doesn't know up front
>what
>the parameter names will be.  *So, if the file also included "age=7",
>it
>should still make a variable called age with the value '7', even though
>'age' doesn't specifically appear anywhere in my code.  Is there a way
>to
>do this?
>
>Thank you,
>April
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

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

Re: Can you turn an input into a variable name?

R help mailing list-2
In reply to this post by April Ettington
Hi April,
Try this:

# this could be done from a file
textlines<-read.table(text="color=green
 shape=circle
 age=17
 name=Jim",
 stringsAsFactors=FALSE)
for(i in 1:length(textlines)) {
 nextline<-unlist(strsplit(textlines[i,1],"="))
 assign(nextline[1],nextline[2])
}
color
[1] "green"
shape
[1] "circle"
age
[1] "17"
name
[1] "Jim"

Jim

On Fri, Oct 4, 2019 at 4:59 PM April Ettington <[hidden email]> wrote:

>
> Let's say I am parsing a file with a list of parameters followed by an
> equal sign, and their corresponding values, eg:
>
> color=green
> shape=circle
>
> and I want to use this information to create a variable called color with
> the value 'green' and a variable shape with the value 'circle'.  However, I
> also want my code to be able to do this *when it doesn't know up front what
> the parameter names will be.  *So, if the file also included "age=7", it
> should still make a variable called age with the value '7', even though
> 'age' doesn't specifically appear anywhere in my code.  Is there a way to
> do this?
>
> Thank you,
> April
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

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

Re: Can you turn an input into a variable name?

Bert Gunter-2
Well, OK, but do note that strsplit() is vectorized, so

z <- strplit(textlines)  ## provides a list of splits for each line

would be faster for large files.

However, to add to what Jeff said, it is hard for me to see how your
approach will not lead to problems. For example, what if there are several
"age = xxx" lines in the file; or variable number of spaces between the "="
and the left and right sides; etc., which would mean that your "split"
string might need to be a regex that can handle variable numbers of spaces.
In general, keeping such information in a single suitable data structure
rather than having separate symbols (variable names) for each line seems to
me -- in my vast ignorance of your situation!!, so caveat emptor -- to be a
more robust approach.

But anyway ....

Cheers,
Bert





Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Oct 4, 2019 at 4:11 AM Jim Lemon via R-help <[hidden email]>
wrote:

> Hi April,
> Try this:
>
> # this could be done from a file
> textlines<-read.table(text="color=green
>  shape=circle
>  age=17
>  name=Jim",
>  stringsAsFactors=FALSE)
> for(i in 1:length(textlines)) {
>  nextline<-unlist(strsplit(textlines[i,1],"="))
>  assign(nextline[1],nextline[2])
> }
> color
> [1] "green"
> shape
> [1] "circle"
> age
> [1] "17"
> name
> [1] "Jim"
>
> Jim
>
> On Fri, Oct 4, 2019 at 4:59 PM April Ettington <[hidden email]>
> wrote:
> >
> > Let's say I am parsing a file with a list of parameters followed by an
> > equal sign, and their corresponding values, eg:
> >
> > color=green
> > shape=circle
> >
> > and I want to use this information to create a variable called color with
> > the value 'green' and a variable shape with the value 'circle'.
> However, I
> > also want my code to be able to do this *when it doesn't know up front
> what
> > the parameter names will be.  *So, if the file also included "age=7", it
> > should still make a variable called age with the value '7', even though
> > 'age' doesn't specifically appear anywhere in my code.  Is there a way to
> > do this?
> >
> > Thank you,
> > April
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

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

Re: Can you turn an input into a variable name?

glsnow
In reply to this post by April Ettington
This is in part answered by FAQ 7.21.

The most important part of that answer is at the bottom where it says
that it is usually better to use a list.

It may be safer to use a list for your case so that other important
variables do not become masked (hidden by the global variables you
just created).

Here is one way to do it:

txtcon <- textConnection("color=green
shape=circle")

textlines<-readLines(txtcon)
tmp <- strsplit(textlines, "=")

mylist <- list()
for(i in tmp) {
  mylist[[ i[1] ]] <- i[2]
}

or another option:

mylist[ sapply(tmp, `[`, 1)] <- sapply(tmp, `[`, 2)

If you know that you will not need a regular expression then you can
just use the `sep` argument to `read.table`:

txtcon <- textConnection("color=green
shape=circle")

tmp2 <- read.table(txtcon, sep='=', stringsAsFactors = FALSE)
mylist <- list()
mylist[ tmp2[,1] ] <- tmp2[,2]

Now you can use functions like `with`, `within`, `evalq`, etc. to work
with the elements of the list as if they were variables:

> with(mylist, ls() )
[1] "color" "shape"
> with(mylist, color)
[1] "green"
> mylist$shape
[1] "circle"
> vname <- 'color'
> mylist[[vname]]
[1] "green"
> evalq(paste('it is a', color, shape), mylist)
[1] "it is a green circle"

If you need regular expressions to find your names and values (extra
spaces, lines without '=', pairs embedded in longer sentences, etc.)
then look at the `regmatches` function or possibly the gsubfn package
(or other tools).


On Fri, Oct 4, 2019 at 12:59 AM April Ettington
<[hidden email]> wrote:

>
> Let's say I am parsing a file with a list of parameters followed by an
> equal sign, and their corresponding values, eg:
>
> color=green
> shape=circle
>
> and I want to use this information to create a variable called color with
> the value 'green' and a variable shape with the value 'circle'.  However, I
> also want my code to be able to do this *when it doesn't know up front what
> the parameter names will be.  *So, if the file also included "age=7", it
> should still make a variable called age with the value '7', even though
> 'age' doesn't specifically appear anywhere in my code.  Is there a way to
> do this?
>
> Thank you,
> April
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
[hidden email]

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