1

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:

  1. No plot appears, alto the code works fine from the interactive R shell.
  2. readline() works only in interactive mode, as per the documentation, and scan() is line-buffered, instead of reacting to a single keypress

How does this work? My environment is Debian.

Vorac
  • 8,726
  • 11
  • 58
  • 101

1 Answers1

2

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!

Vorac
  • 8,726
  • 11
  • 58
  • 101