I believe this question has been asked for almost all languages except for R.
Essentially I'd like to check if stdin()
is empty but I do not know how to do it.
This is because if you perform the following bit of code your session will hang (probably until something is sent to the relevant R process).
input <- file("stdin", "r")
result <- readLines(input, 1) # Session hangs here
Ideally, I could check the status of input
before I try to read anything from it.
WARNING
If you run the above script then you will likely need to kill the running process wherever it lives (either interactively in some IDE or in an Rscript).
EDIT
I found a helpful SO post that shed some light on the situation. In summary you can use CTRL + D to signal end of file condition to the input (so far working for readLines(input, -1)
.
My ultimate goal is to write an Rscript that could accept input from a stdin
connection via a pipe. However as I have discovered, the R interpreter will wait until it receives an end of file condition before returning. In the event nothing is actually piped into stdin
, is there a way to programatically call this signal?