decomposing a string representing a valid mathematical expression?

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

decomposing a string representing a valid mathematical expression?

Witold E Wolski
Hello,

I would like to decompose a string i.e.

"a - b"
or "a +b"

into an array or list
["a","-","b"]

Since R knows how to parse expressions these type of expressions I
would like to reuse some existing functions and not to use gsub or
strsplit etc.

Thank you
Witek

--
Witold Eryk Wolski

______________________________________________
[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: decomposing a string representing a valid mathematical expression?

Bert Gunter-2
Not sure what you're looking for, but see ?deparse for deparsing
**expressions.**

-- 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 Tue, Oct 29, 2019 at 5:56 AM Witold E Wolski <[hidden email]> wrote:

> Hello,
>
> I would like to decompose a string i.e.
>
> "a - b"
> or "a +b"
>
> into an array or list
> ["a","-","b"]
>
> Since R knows how to parse expressions these type of expressions I
> would like to reuse some existing functions and not to use gsub or
> strsplit etc.
>
> Thank you
> Witek
>
> --
> Witold Eryk Wolski
>
> ______________________________________________
> [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: decomposing a string representing a valid mathematical expression?

Ivan Krylov
In reply to this post by Witold E Wolski
On Tue, 29 Oct 2019 13:55:27 +0100
Witold E Wolski <[hidden email]> wrote:

> Since R knows how to parse expressions these type of expressions I
> would like to reuse some existing functions and not to use gsub or
> strsplit etc.

You might be interested in the `parse` function:

x <- "(a+b) * c/(d * (e - f))"
str(parse(text = x))
# length 1 expression((a + b) * c/(d * (e - f)))
#  - attr(*, "srcref")=List of 1
#   ..$ : 'srcref' int [1:8] 1 1 1 23 1 23 1 1
#   .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x55ebecdac210>
#  - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment:0x55ebecdac210>
#  - attr(*, "wholeSrcref")= 'srcref' int [1:8] 1 0 2 0 0 0 1 2
#   ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'<environment:0x55ebecdac210>
parse(text = x)[[1]]
# (a + b) * c/(d * (e - f))
parse(text = x)[[1]][1]
# `/`()
parse(text = x)[[1]][2]
# ((a + b) * c)()
parse(text = x)[[1]][3]
# (d * (e - f))()

Quoting ?expression,

>> As an object of mode ‘"expression"’ is a list, it can be subsetted
>> by ‘[’, ‘[[’ or ‘$’, the latter two extracting individual calls etc.

--
Best regards,
Ivan

______________________________________________
[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: decomposing a string representing a valid mathematical expression?

Bert Gunter-2
Does something like the following make any sense for your context?

> z <- "(xy+ s)/ (ab +log(4.13))"
>
> lapply(parse(text = z), FUN = function(x)strsplit(deparse(x), split =
"[[:space:]]+"))
[[1]]
[[1]][[1]]
[1] "(xy"        "+"          "s)/(ab"     "+"          "log(4.13))"


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 Tue, Oct 29, 2019 at 9:23 AM Ivan Krylov <[hidden email]> wrote:

> On Tue, 29 Oct 2019 13:55:27 +0100
> Witold E Wolski <[hidden email]> wrote:
>
> > Since R knows how to parse expressions these type of expressions I
> > would like to reuse some existing functions and not to use gsub or
> > strsplit etc.
>
> You might be interested in the `parse` function:
>
> x <- "(a+b) * c/(d * (e - f))"
> str(parse(text = x))
> # length 1 expression((a + b) * c/(d * (e - f)))
> #  - attr(*, "srcref")=List of 1
> #   ..$ : 'srcref' int [1:8] 1 1 1 23 1 23 1 1
> #   .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
> <environment: 0x55ebecdac210>
> #  - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
> <environment:0x55ebecdac210>
> #  - attr(*, "wholeSrcref")= 'srcref' int [1:8] 1 0 2 0 0 0 1 2
> #   ..- attr(*, "srcfile")=Classes 'srcfilecopy',
> 'srcfile'<environment:0x55ebecdac210>
> parse(text = x)[[1]]
> # (a + b) * c/(d * (e - f))
> parse(text = x)[[1]][1]
> # `/`()
> parse(text = x)[[1]][2]
> # ((a + b) * c)()
> parse(text = x)[[1]][3]
> # (d * (e - f))()
>
> Quoting ?expression,
>
> >> As an object of mode ‘"expression"’ is a list, it can be subsetted
> >> by ‘[’, ‘[[’ or ‘$’, the latter two extracting individual calls etc.
>
> --
> Best regards,
> Ivan
>
> ______________________________________________
> [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: decomposing a string representing a valid mathematical expression?

Duncan Murdoch-2
On 29/10/2019 12:46 p.m., Bert Gunter wrote:
> Does something like the following make any sense for your context?
>
>> z <- "(xy+ s)/ (ab +log(4.13))"
>>
>> lapply(parse(text = z), FUN = function(x)strsplit(deparse(x), split =
> "[[:space:]]+"))
> [[1]]
> [[1]][[1]]
> [1] "(xy"        "+"          "s)/(ab"     "+"          "log(4.13))"

That's the wrong way to do it.  Notice the 3rd element,  "s)/(ab", which
combines two partial operands and an operator.  Deparsing doesn't always
introduce spaces, as your reprex assumes.

I think Witold's request is quite hard to do.  The right way to handle
expressions is as hierachical objects, like those returned from
parse(text = z).  Turning those into character vectors of lexical tokens
doesn't make sense.

Perhaps if Witold explained the context of what he is trying to do
someone could give a suggestion about a better approach.

Duncan Murdoch

>
>
> 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 Tue, Oct 29, 2019 at 9:23 AM Ivan Krylov <[hidden email]> wrote:
>
>> On Tue, 29 Oct 2019 13:55:27 +0100
>> Witold E Wolski <[hidden email]> wrote:
>>
>>> Since R knows how to parse expressions these type of expressions I
>>> would like to reuse some existing functions and not to use gsub or
>>> strsplit etc.
>>
>> You might be interested in the `parse` function:
>>
>> x <- "(a+b) * c/(d * (e - f))"
>> str(parse(text = x))
>> # length 1 expression((a + b) * c/(d * (e - f)))
>> #  - attr(*, "srcref")=List of 1
>> #   ..$ : 'srcref' int [1:8] 1 1 1 23 1 23 1 1
>> #   .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
>> <environment: 0x55ebecdac210>
>> #  - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
>> <environment:0x55ebecdac210>
>> #  - attr(*, "wholeSrcref")= 'srcref' int [1:8] 1 0 2 0 0 0 1 2
>> #   ..- attr(*, "srcfile")=Classes 'srcfilecopy',
>> 'srcfile'<environment:0x55ebecdac210>
>> parse(text = x)[[1]]
>> # (a + b) * c/(d * (e - f))
>> parse(text = x)[[1]][1]
>> # `/`()
>> parse(text = x)[[1]][2]
>> # ((a + b) * c)()
>> parse(text = x)[[1]][3]
>> # (d * (e - f))()
>>
>> Quoting ?expression,
>>
>>>> As an object of mode ‘"expression"’ is a list, it can be subsetted
>>>> by ‘[’, ‘[[’ or ‘$’, the latter two extracting individual calls etc.
>>
>> --
>> Best regards,
>> Ivan
>>
>> ______________________________________________
>> [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.
>

______________________________________________
[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: decomposing a string representing a valid mathematical expression?

Bert Gunter-2
Yes, I agree. Maybe the clumsiness of my example might help clarify the
importance of clarifying the context/understanding the difficulty.

Cheers,
Bert


On Tue, Oct 29, 2019 at 5:22 PM Duncan Murdoch <[hidden email]>
wrote:

> On 29/10/2019 12:46 p.m., Bert Gunter wrote:
> > Does something like the following make any sense for your context?
> >
> >> z <- "(xy+ s)/ (ab +log(4.13))"
> >>
> >> lapply(parse(text = z), FUN = function(x)strsplit(deparse(x), split =
> > "[[:space:]]+"))
> > [[1]]
> > [[1]][[1]]
> > [1] "(xy"        "+"          "s)/(ab"     "+"          "log(4.13))"
>
> That's the wrong way to do it.  Notice the 3rd element,  "s)/(ab", which
> combines two partial operands and an operator.  Deparsing doesn't always
> introduce spaces, as your reprex assumes.
>
> I think Witold's request is quite hard to do.  The right way to handle
> expressions is as hierachical objects, like those returned from
> parse(text = z).  Turning those into character vectors of lexical tokens
> doesn't make sense.
>
> Perhaps if Witold explained the context of what he is trying to do
> someone could give a suggestion about a better approach.
>
> Duncan Murdoch
>
> >
> >
> > 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 Tue, Oct 29, 2019 at 9:23 AM Ivan Krylov <[hidden email]>
> wrote:
> >
> >> On Tue, 29 Oct 2019 13:55:27 +0100
> >> Witold E Wolski <[hidden email]> wrote:
> >>
> >>> Since R knows how to parse expressions these type of expressions I
> >>> would like to reuse some existing functions and not to use gsub or
> >>> strsplit etc.
> >>
> >> You might be interested in the `parse` function:
> >>
> >> x <- "(a+b) * c/(d * (e - f))"
> >> str(parse(text = x))
> >> # length 1 expression((a + b) * c/(d * (e - f)))
> >> #  - attr(*, "srcref")=List of 1
> >> #   ..$ : 'srcref' int [1:8] 1 1 1 23 1 23 1 1
> >> #   .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
> >> <environment: 0x55ebecdac210>
> >> #  - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
> >> <environment:0x55ebecdac210>
> >> #  - attr(*, "wholeSrcref")= 'srcref' int [1:8] 1 0 2 0 0 0 1 2
> >> #   ..- attr(*, "srcfile")=Classes 'srcfilecopy',
> >> 'srcfile'<environment:0x55ebecdac210>
> >> parse(text = x)[[1]]
> >> # (a + b) * c/(d * (e - f))
> >> parse(text = x)[[1]][1]
> >> # `/`()
> >> parse(text = x)[[1]][2]
> >> # ((a + b) * c)()
> >> parse(text = x)[[1]][3]
> >> # (d * (e - f))()
> >>
> >> Quoting ?expression,
> >>
> >>>> As an object of mode ‘"expression"’ is a list, it can be subsetted
> >>>> by ‘[’, ‘[[’ or ‘$’, the latter two extracting individual calls etc.
> >>
> >> --
> >> Best regards,
> >> Ivan
> >>
> >> ______________________________________________
> >> [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.
> >
>
>

        [[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.