Reputation: 2063
I have found a roadblock in trying to create a command line interface program with R. The objective is to load a file (copy the below as finances.csv):
"date","investpercent","expenses","savings","low","high","objective"
"2011-12-01",32,17000,20000,25978,20000,4763
"2012-08-01",31,31100,31100,35881,23892,6372
"2013-09-20",31,31100,47172,76174,27261,6372
Then print a plot from it, but not in a R environment, but straight from the command line, try the code (fix setwd to the proper path):
#! /usr/bin/Rscript
setwd(".")
data <- read.csv("finances.csv", stringsAsFactors = FALSE)
plot(type="l",as.Date(data$date, format = "%Y-%m-%d"),data$low,col="red")
lines(as.Date(data$date, format = "%Y-%m-%d"),data$high,col="green")
exit()
The above does not print the graph in a new window.
Upvotes: 2
Views: 1818
Reputation: 263481
I think you are confusing "interactive behavior" with "script behavior". There's no interactive plotting window if you are running this from the command line.
If you are in interactive mode (and the hash-bang is not active) then you would bring an executable .R file in with the source(filename)
-function.
If on the other hand, you are running this as an executable file from the command line, then you would open (and not forget to close) a file-oriented graphics device like this:
data <- read.csv("finances.csv", stringsAsFactors = FALSE)
png() # default name is Rplot.png but you could choose something else
plot(type="l",as.Date(data$date, format = "%Y-%m-%d"),data$low,col="red")
lines(as.Date(data$date, format = "%Y-%m-%d"),data$high,col="green")
dev.off()
exit()
You can find more details at the ?Devices
help page.
Upvotes: 2