How to run an R script and show a plot?

99封情书 提交于 2019-12-24 19:04:53

问题


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.


回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!