问题
How to wait for a keypress in R?
I am attempting to construct a simple script, which creates a graph; closes it when a key is pressed. My code:
#!/usr/bin/Rscript
library(tidyverse)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), colour = "blue")
I have tried the answers from the linked question to pause the script after the plot is drawn. However:
- No plot appears, alto the code works fine from the interactive
R
shell. readline()
works only in interactive mode, as per the documentation, andscan()
is line-buffered, instead of reacting to a single keypress
How does this work? My environment is Debian.
回答1:
It seems the problems I was trying to solve is non-existent. The mentioned above functions have a very good reason not to work in non-interactive mode - they are not needed.
Long story short, ggplot2 automatically exports plots to a .pdf when run in non-interacive mode!
来源:https://stackoverflow.com/questions/47294283/how-to-run-an-r-script-and-show-a-plot