Re: [R] evaluating expressions as R console

2016-09-05 Thread Bert Gunter
I'm sharing this with r-help, as your detailed response might help
others help you.

-- 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 Sun, Sep 4, 2016 at 11:45 PM, Adrian Dușa  wrote:
> On Mon, Sep 5, 2016 at 2:52 AM, Bert Gunter  wrote:
>>
>> You might want to look at the "evaluate" package.
>
>
> Of course, forgot to mention. I did try it, but got:
>
>> bar <- readLines("foo.R", warn = FALSE)
>> bar <- paste(bar, collapse = "\n")
>> evaluate::evaluate(input = bar)
> [[1]]
> $src
> [1] "foo <- function(x) {\nprint(x)\n}\nprint)foo)\nfoo(2)"
>
> attr(,"class")
> [1] "source"
>
> [[2]]
> :4:6: unexpected ')'
> 3: }
> 4: print)
> ^>
>
> I ran into the same problem as source(): it works only if it doesn't have
> any errors. In addition, the error message is also different because it
> evaluates the entire chunk, whereas the R console evaluates one command
> (line) at a time.
>
> Fixing the command:
> bar2 <- "foo <- function(x) {\nprint(x)\n}\nprint(foo)\nfoo(2)"
>
> will fix the workflow in evaluate():
>
> evaluate::evaluate(input = bar2)
>
>
> But it will also fix it with source():
>
>> source("foo2.R", echo = TRUE, keep.source = TRUE)
>
>> foo <- function(x) {
> + print(x)
> + }
>
>> print(foo)
> function(x) {
> print(x)
> }
>
>> foo(2)
> [1] 2
>
>
> So evaluate() has more detail than source(), but essentially they do the
> same thing in evaluating the whole chunk. From the help of source():
> "Since the complete file is parsed before any of it is run, syntax errors
> result in none of the code being run."
>
> So far, it seems that only R CMD BATCH is able to run one command at a time:
>
> $ R CMD BATCH -q foo.R
> $ cat foo.Rout
>> foo <- function(x) {
> + print(x)
> + }
>> print)foo)
> Error: unexpected ')' in "print)"
> Execution halted
>
> This error is exactly the same as in the R console, and the function foo()
> is created before the error occurs. One possible solution is to get the
> workspace saved, and run R CMD BATCH again on the rest of commands after the
> error.
>
> But it still needs additional objects if the chunk is to be evaluated in a
> specific environment. This is the point where I've got, and I don't know if
> this is the best approach.
>
> Thank you,
> Adrian
>
> --
> Adrian Dusa
> University of Bucharest
> Romanian Social Data Archive
> Soseaua Panduri nr.90
> 050663 Bucharest sector 5
> Romania

__
R-help@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.

Re: [R] evaluating expressions as R console

2016-09-05 Thread Adrian Dușa
I think I have found a working solution. Rather ugly, but working and will
keep looking for better alternatives, though.

The procedure involves:

- parsing one line at a time
- if incomplete, parse as many lines as necessary to form an expression
- determine all expressions in the original input
- evaluate each expression, one at a time
  (which shows individual errors and warnings)
- print each pair of:
- expression
- error, or result with possible warning(s)

It works reasonably quick for small chunks, but that is ok for my purposes.

I hope it helps anyone. Should there be better alternatives, I would be
more than grateful for a hint.

Best,
Adrian

On Mon, Sep 5, 2016 at 5:33 PM, Bert Gunter  wrote:

> I'm sharing this with r-help, as your detailed response might help
> others help you.
>
> -- 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 Sun, Sep 4, 2016 at 11:45 PM, Adrian Dușa 
> wrote:
> > On Mon, Sep 5, 2016 at 2:52 AM, Bert Gunter 
> wrote:
> >>
> >> You might want to look at the "evaluate" package.
> >
> >
> > Of course, forgot to mention. I did try it, but got:
> >
> >> bar <- readLines("foo.R", warn = FALSE)
> >> bar <- paste(bar, collapse = "\n")
> >> evaluate::evaluate(input = bar)
> > [[1]]
> > $src
> > [1] "foo <- function(x) {\nprint(x)\n}\nprint)foo)\nfoo(2)"
> >
> > attr(,"class")
> > [1] "source"
> >
> > [[2]]
> > :4:6: unexpected ')'
> > 3: }
> > 4: print)
> > ^>
> >
> > I ran into the same problem as source(): it works only if it doesn't have
> > any errors. In addition, the error message is also different because it
> > evaluates the entire chunk, whereas the R console evaluates one command
> > (line) at a time.
> >
> > Fixing the command:
> > bar2 <- "foo <- function(x) {\nprint(x)\n}\nprint(foo)\nfoo(2)"
> >
> > will fix the workflow in evaluate():
> >
> > evaluate::evaluate(input = bar2)
> >
> >
> > But it will also fix it with source():
> >
> >> source("foo2.R", echo = TRUE, keep.source = TRUE)
> >
> >> foo <- function(x) {
> > + print(x)
> > + }
> >
> >> print(foo)
> > function(x) {
> > print(x)
> > }
> >
> >> foo(2)
> > [1] 2
> >
> >
> > So evaluate() has more detail than source(), but essentially they do the
> > same thing in evaluating the whole chunk. From the help of source():
> > "Since the complete file is parsed before any of it is run, syntax errors
> > result in none of the code being run."
> >
> > So far, it seems that only R CMD BATCH is able to run one command at a
> time:
> >
> > $ R CMD BATCH -q foo.R
> > $ cat foo.Rout
> >> foo <- function(x) {
> > + print(x)
> > + }
> >> print)foo)
> > Error: unexpected ')' in "print)"
> > Execution halted
> >
> > This error is exactly the same as in the R console, and the function
> foo()
> > is created before the error occurs. One possible solution is to get the
> > workspace saved, and run R CMD BATCH again on the rest of commands after
> the
> > error.
> >
> > But it still needs additional objects if the chunk is to be evaluated in
> a
> > specific environment. This is the point where I've got, and I don't know
> if
> > this is the best approach.
> >
> > Thank you,
> > Adrian
>


-- 
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[alternative HTML version deleted]]

__
R-help@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.

Re: [R] evaluating expressions as R console

2016-09-05 Thread Adrian Dușa
On Mon, Sep 5, 2016 at 5:33 PM, Bert Gunter  wrote:
>
> I'm sharing this with r-help, as your detailed response might help
> others help you.


Oh, my bad (thought I had replied to all).
I wanted to add anyways the intended result seems to be possible. If
pasting the code here...:
http://www.tutorialspoint.com/r_terminal_online.php

... the result is exactly as in the R console.

Maybe this is a different technology (direct websocket?), but if they are
evaluating the text they're doing a very good job.

Best,
Adrian

--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] evaluating expressions as R console

2016-09-04 Thread Bert Gunter
You might want to look at the "evaluate" package.

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 Sun, Sep 4, 2016 at 4:47 PM, Adrian Dușa  wrote:
> Dear R users,
>
> I am trying to simulate a pseudo R console, evaluating commands. This is a
> simple example of a series of command lines which get evaluated:
>
> foo <- function(x) {
> print(x)
> }
> print)foo)
> foo(2)
>
>
> If I copied and pasted this example in an R console, this is the result to
> replicate (and the benchmark to compare everything else):
>
>> foo <- function(x) {
> + print(x)
> + }
>> print)foo)
> Error: unexpected ')' in "print)"
>> foo(2)
> [1] 2
>
>
> The trouble I'm having is to reproduce this exact output, by evaluating the
> chunk of code. I tried both Rscript and littler, but I am unable to
> reproduce this.
>
> I had some success via:
>
> R CMD BATCH -q foo.R
>
> (saving the chunk to a file called foo.R), which only works until the first
> error appears. I can run it again on the subsequent command(s) after the
> error, but the function foo(), which in a normal R console gets created
> without any error, doesn't get preserved on the next run.
>
> I also had some limited success with:
>
> source("foo.R", echo = TRUE, keep.source = TRUE)
>
> But the error message is different, and the first 4 lines are not echo-ed.
>
> source() works pretty well if there are no errors, but otherwise I get the
> same result as R CMD BATCH, namely only the error is displayed and the
> function foo() doesn't get created in the current environment.
>
> I would rather use source() than R CMD BATCH, due to specific environments
> where the chunk should be evaluated in (not impossible, but inconvenient to
> save environments and load them back in the R CMD BATCH).
>
> Was also curious about knitr and pander, but again unable to replicate the
> results in the real R console.
>
> After many hours of searching, reading and testing, I would be grateful for
> any hint.
>
> Thank you in advance,
> Adrian
>
> --
> Adrian Dusa
> University of Bucharest
> Romanian Social Data Archive
> Soseaua Panduri nr.90
> 050663 Bucharest sector 5
> Romania
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.