Executing an R script and show the plots.

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

Executing an R script and show the plots.

Felix Blind
Dear R users,

i am a python user trying to get my statistical knowledge up to speed
and R is the language for that.
I would like to run R scripts like that: R -f script.r and still get the
plots that pop up.

When I type the following code in the R REPL I get a nice plot:

library(ggplot2)                                                                                                                                                                          

data("midwest",
package="ggplot2")                                                                                                                                                        

ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()

But when I put the same code in a file script.r and run it with R -f
script.r or if I source it in the REPL with source("script.r") I do not
get any plots shown.

Can you guys tell me what I do wrong. (Apart from either using RStudio
or the REPL exclusively)

Kind regards,
Felix

______________________________________________
[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: Executing an R script and show the plots.

Bert Gunter-2
Google is your friend!

https://stackoverflow.com/questions/26643852/ggplot-plots-in-scripts-do-not-display-in-rstudio

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, Jan 26, 2020 at 7:30 AM Felix Blind <[hidden email]> wrote:

> Dear R users,
>
> i am a python user trying to get my statistical knowledge up to speed
> and R is the language for that.
> I would like to run R scripts like that: R -f script.r and still get the
> plots that pop up.
>
> When I type the following code in the R REPL I get a nice plot:
>
>
> library(ggplot2)
>
> data("midwest",
>
> package="ggplot2")
>
> ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()
>
> But when I put the same code in a file script.r and run it with R -f
> script.r or if I source it in the REPL with source("script.r") I do not
> get any plots shown.
>
> Can you guys tell me what I do wrong. (Apart from either using RStudio
> or the REPL exclusively)
>
> Kind regards,
> Felix
>
> ______________________________________________
> [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: Executing an R script and show the plots.

Jeff Newmiller
OP does not want to depend on RStudio. Bert.

Need to open a graphics device. Which one you use is highly dependent on your needs and OS configuration. https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html

On January 26, 2020 8:06:24 AM PST, Bert Gunter <[hidden email]> wrote:

>Google is your friend!
>
>https://stackoverflow.com/questions/26643852/ggplot-plots-in-scripts-do-not-display-in-rstudio
>
>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, Jan 26, 2020 at 7:30 AM Felix Blind <[hidden email]>
>wrote:
>
>> Dear R users,
>>
>> i am a python user trying to get my statistical knowledge up to speed
>> and R is the language for that.
>> I would like to run R scripts like that: R -f script.r and still get
>the
>> plots that pop up.
>>
>> When I type the following code in the R REPL I get a nice plot:
>>
>>
>> library(ggplot2)
>>
>> data("midwest",
>>
>> package="ggplot2")
>>
>> ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()
>>
>> But when I put the same code in a file script.r and run it with R -f
>> script.r or if I source it in the REPL with source("script.r") I do
>not
>> get any plots shown.
>>
>> Can you guys tell me what I do wrong. (Apart from either using
>RStudio
>> or the REPL exclusively)
>>
>> Kind regards,
>> Felix
>>
>> ______________________________________________
>> [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.

--
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: Executing an R script and show the plots.

Rui Barradas
In reply to this post by Felix Blind
Hello,

Have you tried

source("script.r", print.eval = TRUE)

?

Also, you should print() the graph explicitly:


library(ggplot2)
data(midwest, package = "ggplot2")

g <- ggplot(midwest, aes(area, poptotal)) + geom_point()
print(g)


Hope this helps,

Rui Barradas

Às 16:32 de 24/01/20, Felix Blind escreveu:

> Dear R users,
>
> i am a python user trying to get my statistical knowledge up to speed
> and R is the language for that.
> I would like to run R scripts like that: R -f script.r and still get the
> plots that pop up.
>
> When I type the following code in the R REPL I get a nice plot:
>
> library(ggplot2)
>
> data("midwest",
> package="ggplot2")
>
> ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()
>
> But when I put the same code in a file script.r and run it with R -f
> script.r or if I source it in the REPL with source("script.r") I do not
> get any plots shown.
>
> Can you guys tell me what I do wrong. (Apart from either using RStudio
> or the REPL exclusively)
>
> Kind regards,
> Felix
>
> ______________________________________________
> [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: Executing an R script and show the plots.

radmuzom .
In reply to this post by Felix Blind
Dear Felix,

> I would like to run R scripts like that: R -f script.r and still get the
> plots that pop up.

Since you are using ggplot2, you can use the ggsave function from the package to save the plot to a location of your choice. I tested the following script from the command line and it worked.

library(ggplot2)                                                                                                                                                                          
data("midwest",
     package="ggplot2")                                                                                                                                                        
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()
ggsave(paste0(getwd(), "/midwest.png"), device = "png")

Regards,
Anindya


______________________________________________
[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: Executing an R script and show the plots.

David Winsemius
In reply to this post by Felix Blind
You need to understand that the R interpreter does not have an
interactive plot device when run as a script from an OS command line.
You need to open an output device, `print` the output of the ggplot
call, and then  _after_ closing the device appropriately, open the
output in an appropriate viewer supplied by your unstated OS.


?Devices # brings up a list of possible devices and has a "See Also"
section that says:

===========================

The individual help files for further information on any of the devices
listed here;

on Windows:
windows.options,

on a Unix-alike:
X11.options, quartz.options,

ps.options and pdf.options for how to customize devices.

dev.interactive, dev.cur, dev.print, graphics.off, image, dev2bitmap.

On Unix-alikes only:
capabilities to see if X11, jpeg, png, tiff, quartz and the cairo-based
devices are available.
==============================


So do some further reading to educate yourself.


--

David.

On 1/24/20 8:32 AM, Felix Blind wrote:

> Dear R users,
>
> i am a python user trying to get my statistical knowledge up to speed
> and R is the language for that.
> I would like to run R scripts like that: R -f script.r and still get the
> plots that pop up.
>
> When I type the following code in the R REPL I get a nice plot:
>
> library(ggplot2)
>
> data("midwest",
> package="ggplot2")
>
> ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()
>
> But when I put the same code in a file script.r and run it with R -f
> script.r or if I source it in the REPL with source("script.r") I do not
> get any plots shown.
>
> Can you guys tell me what I do wrong. (Apart from either using RStudio
> or the REPL exclusively)
>
> Kind regards,
> Felix
>
> ______________________________________________
> [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: Executing an R script and show the plots.

Sorkin, John
Felix,
I suggest you consider using an IDE such as RStudio as you develop and run R code. An integrated development environment will allow you to concentrate on learning R
rather on the mechanics of running R in a
non-standard environment.
John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street<x-apple-data-detectors://12>
GRECC<x-apple-data-detectors://12> (BT/18/GR)
Baltimore, MD 21201-1524<x-apple-data-detectors://13/0>
(Phone) 410-605-711<tel:410-605-7119>9
(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing)

On Jan 26, 2020, at 3:20 PM, David Winsemius <[hidden email]> wrote:

You need to understand that the R interpreter does not have an interactive plot device when run as a script from an OS command line. You need to open an output device, `print` the output of the ggplot call, and then  _after_ closing the device appropriately, open the output in an appropriate viewer supplied by your unstated OS.


?Devices # brings up a list of possible devices and has a "See Also" section that says:

===========================

The individual help files for further information on any of the devices listed here;

on Windows:
windows.options,

on a Unix-alike:
X11.options, quartz.options,

ps.options and pdf.options for how to customize devices.

dev.interactive, dev.cur, dev.print, graphics.off, image, dev2bitmap.

On Unix-alikes only:
capabilities to see if X11, jpeg, png, tiff, quartz and the cairo-based devices are available.
==============================


So do some further reading to educate yourself.


--

David.

On 1/24/20 8:32 AM, Felix Blind wrote:
Dear R users,

i am a python user trying to get my statistical knowledge up to speed
and R is the language for that.
I would like to run R scripts like that: R -f script.r and still get the
plots that pop up.

When I type the following code in the R REPL I get a nice plot:

library(ggplot2)

data("midwest",
package="ggplot2")

ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()

But when I put the same code in a file script.r and run it with R -f
script.r or if I source it in the REPL with source("script.r") I do not
get any plots shown.

Can you guys tell me what I do wrong. (Apart from either using RStudio
or the REPL exclusively)

Kind regards,
Felix

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=02%7C01%7CJSorkin%40som.umaryland.edu%7Cff399da2c92c46d8dbef08d7a29d28e1%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637156668200832250&amp;sdata=%2F4npPPdmZiflpvoByexxwJKfkSQmGASQimaxnnDyOj0%3D&amp;reserved=0
PLEASE do read the posting guide https://nam03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&amp;data=02%7C01%7CJSorkin%40som.umaryland.edu%7Cff399da2c92c46d8dbef08d7a29d28e1%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637156668200832250&amp;sdata=Z%2FbUl0zEHpfVuQsvFysOAFps0VjeAtA7NdcL3VYJcus%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=02%7C01%7CJSorkin%40som.umaryland.edu%7Cff399da2c92c46d8dbef08d7a29d28e1%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637156668200842231&amp;sdata=VMdvcvmZ6Cn8kZ%2Buta44UT08Q5T%2FEf3X8ufWmYz9avE%3D&amp;reserved=0
PLEASE do read the posting guide https://nam03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&amp;data=02%7C01%7CJSorkin%40som.umaryland.edu%7Cff399da2c92c46d8dbef08d7a29d28e1%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637156668200842231&amp;sdata=0%2BBjKhdtQ7cMP6wuDm36%2BJmLLWAMLHCDHtJ8ugl2oag%3D&amp;reserved=0
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: [FORGED] Re: Executing an R script and show the plots.

Rolf Turner

On 27/01/20 9:47 am, Sorkin, John wrote:

> Felix, I suggest you consider using an IDE such as RStudio as you
> develop and run R code. An integrated development environment will
> allow you to concentrate on learning R rather on the mechanics of
> running R in a non-standard environment.

I disagree.  Or rather, I would say that this is a matter of personal
taste.  And personally I find that RStudio and its like just present
another "learning curve" to climb.  I would much rather just write R
code and get on with it.

I have never liked GUIs:

> GUIs normally make it simple to accomplish simple actions and
> impossible to accomplish complex actions.
>
>         --- Doug Gwyn
>             (22/Jun/91 in `comp.unix.wizards')

But as I say it's a matter of taste and inclination.  It is likely that
Felix is among the overwhelming majority of users who prefer GUIs.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Felix Blind
Thanks  a lot you guys,

you all were right; I used most of your suggestions as part of the solution.
It is a considerable effort to get persistent plots when running an R
script.
The problem is even when I open an interface with X11() it closes
immediately afterwards, when the script is done executing.

My workaround now is:
g <- ggplot(...)
X11()
print(g)
invisible(readLines("stdin", n=1))

But frankly I do not think that is feasible for a steady development
environment.

As to the suggestions to use Rstudio. I tried it before in university. I
did not like it, too much mouse, too crowded interface. Actually the
difficulty to just use scripts in R was one big reason that I now write
python code. (But that now borders on a discussion post, and that is not
my intention)

Kind regards,
Felix

On 1/26/20 10:22 PM, Rolf Turner wrote:

>
> On 27/01/20 9:47 am, Sorkin, John wrote:
>
>> Felix, I suggest you consider using an IDE such as RStudio as you
>> develop and run R code. An integrated development environment will
>> allow you to concentrate on learning R rather on the mechanics of
>> running R in a non-standard environment.
>
> I disagree.  Or rather, I would say that this is a matter of personal
> taste.  And personally I find that RStudio and its like just present
> another "learning curve" to climb.  I would much rather just write R
> code and get on with it.
>
> I have never liked GUIs:
>
>> GUIs normally make it simple to accomplish simple actions and
>> impossible to accomplish complex actions.
>>
>>         --- Doug Gwyn
>>             (22/Jun/91 in `comp.unix.wizards')
>
> But as I say it's a matter of taste and inclination.  It is likely
> that Felix is among the overwhelming majority of users who prefer GUIs.
>
> cheers,
>
> Rolf Turner
>

______________________________________________
[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: Executing an R script and show the plots.

Abby Spurdle
In reply to this post by Sorkin, John
> An integrated development environment will allow you to concentrate on learning R
> rather on the mechanics of running R in a
> non-standard environment.

Oh my gosh...
The terminal emulator is not a non-standard environment.

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Abby Spurdle
In reply to this post by Felix Blind
It is possible to produce interactive-style plots, while running
scripts from the terminal.
(Or at least, it was, because I've done before).

Can I verify, ***which OS*** are you using...???

I'm offsite now.
If you're using Windows or Linux, I can check later.
(If no one else posts the answer, that is).

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Abby Spurdle
Here's a solution for Windows, for use with graphics::plot.
Using, Linux and grid or ggplot2, may require some modification.
(However, I assuming the modifications would be easy to make).

You can create a file, say quasi_interactive.r

----quasi_interactive.r----
#for use with windows only
options (device = windows)

.initialize = function ()
{   n = length (dev.list () )
    if (n == 0)
        windows ()
    else
        locator (1)
}

.finalize = function ()
{   n = length (dev.list () )
    if (n > 0)
        invisible (locator (1) )
}

#assumes plots called with graphics::plot function
plot = function (x, ...)
{   .initialize ()
    graphics::plot (x, ...)
}

args = commandArgs (TRUE)
source (args [1])

.finalize ()
----end-of-file----

Then at the terminal, use either of:
> Rscript quasi_interactive.r my_script_file.r
> R -f quasi_interactive.r --args test.r

The first option is probably better.

Also, instead of locator (1) you could test for text input (say by
pressing enter), or wait (say 10 seconds per plot).
Another option is to open one graphics device for each plot.

B.

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Felix Blind
Thanks Abby,

I am using Linux and device X11 worked for me in the past. I will check
your script later and tell you if it works.

On 1/28/20 1:27 AM, Abby Spurdle wrote:

> Here's a solution for Windows, for use with graphics::plot.
> Using, Linux and grid or ggplot2, may require some modification.
> (However, I assuming the modifications would be easy to make).
>
> You can create a file, say quasi_interactive.r
>
> ----quasi_interactive.r----
> #for use with windows only
> options (device = windows)
>
> .initialize = function ()
> {   n = length (dev.list () )
>     if (n == 0)
>         windows ()
>     else
>         locator (1)
> }
>
> .finalize = function ()
> {   n = length (dev.list () )
>     if (n > 0)
>         invisible (locator (1) )
> }
>
> #assumes plots called with graphics::plot function
> plot = function (x, ...)
> {   .initialize ()
>     graphics::plot (x, ...)
> }
>
> args = commandArgs (TRUE)
> source (args [1])
>
> .finalize ()
> ----end-of-file----
>
> Then at the terminal, use either of:
>> Rscript quasi_interactive.r my_script_file.r
>> R -f quasi_interactive.r --args test.r
> The first option is probably better.
>
> Also, instead of locator (1) you could test for text input (say by
> pressing enter), or wait (say 10 seconds per plot).
> Another option is to open one graphics device for each plot.
>
> B.

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Martin Møller Skarbiniks Pedersen
On Tue, 28 Jan 2020 at 13:03, Felix Blind <[hidden email]> wrote:
>
> I am using Linux and device X11 worked for me in the past. I will check
> your script later and tell you if it works.

[...]

I am using R version 3.6.2 on ubuntu 18.04.3 LTS and the X11-device
when I make plot in the terminal works without problems.



Regards
Martin

______________________________________________
[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: Executing an R script and show the plots.

Martin Møller Skarbiniks Pedersen
In reply to this post by Felix Blind
On Sun, 26 Jan 2020 at 16:30, Felix Blind <[hidden email]> wrote:
>

> But when I put the same code in a file script.r and run it with R -f
> script.r or if I source it in the REPL with source("script.r") I do not
> get any plots shown.
>
> Can you guys tell me what I do wrong. (Apart from either using RStudio
> or the REPL exclusively)

Add Sys.sleep(Inf) to the end of your script.r and run it with

R --interactive < script.r

It works for me.

Regards
Martin

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Abby Spurdle
In reply to this post by Felix Blind
> I am using Linux and device X11 worked for me in the past. I will check
> your script later and tell you if it works.

The previous script isn't very good.
I've improved it.
The new version has been tested on Linux and Windows, and with lattice
and ggplot2.

I note that the script is more complex that originally intended.
And I suspect that there maybe (mostly small) problems, that would
require further maintenance.

Running R (by itself) non-REPL with partial interactivity, while it
has merit, may be problematic, in the long run.
(Unless, of course, one were to hack the graphics device(s), but that
would probably be a lot of work).

But in saying that, you're welcome to run R, however, you like.

----------------page.r----------------
os = tolower (Sys.info ()[1])
if (os == "windows")
    options (device = windows)
if (os == "linux")
    options (device = X11)

.initialize = function ()
{   n = length (dev.list () )
    if (n > 0)
        locator (1)
}

.finalize = function ()
{   n = length (dev.list () )
    if (n > 0)
        invisible (locator (1) )
}

"+" = function (a, b)
{   object = base::`+` (a, b)
    if (inherits (object, "gg") )
        print (object)
    object
}

.prompt = function (pkg, fun)
{   fbody = character (1)
    fbody [1] = paste (fun, "=", 'function (...) {')
    fbody [2] = '.initialize ()'
    fbody [3] = paste ('print (', pkg, '::', fun, '(...) )', sep="")
    fbody [4] = "}"
    eval (str2lang (paste (fbody, collapse="\n") ), -2)
}

####################################################
#add other top-level plotting functions, if required
####################################################
.prompt ("graphics", "plot")
.prompt ("lattice", "xyplot")
.prompt ("ggplot2", "ggplot")
####################################################

args = commandArgs (TRUE)
source (args [1])

.finalize ()
----------------EOF----------------

Usage is similar to before:
> Rscript page.r myfile.r

The file <myfile.r> shouldn't require any modification, can have multiple plots.

Click on the plot, to move to next one...

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Abby Spurdle
> .prompt = function (pkg, fun)
> {   fbody = character (1)

And I've already noticed a bug.
This isn't going too well...

______________________________________________
[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: [FORGED] Re: Executing an R script and show the plots.

Felix Blind
Thanks all for your solutions.

I like the setup best where I startup an xserver connection and then
halting the script with readlines.
I want to write and run small scripts and that seems to me the nearest
to a leightweight solution to the problem in R.

Kind regards,
Felix

On 1/30/20 7:15 AM, Abby Spurdle wrote:
>> .prompt = function (pkg, fun)
>> {   fbody = character (1)
> And I've already noticed a bug.
> This isn't going too well...

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