How to read from stdin in Shiny?


#1

I can read from stdin in an R script:

$ echo "some text" | Rscript script.R

Note: using Docker and the rocker/r-base image you can run:

$ echo "some text" | docker run --rm -i \
    -v $(pwd):/usr/src/app rocker/r-base \
    bash -c 'Rscript /usr/src/app/script.R'

sript.R :

f <- file("stdin")
i <- readLines(f)
close(f)
cat(i)

Now I want to reproduce this behaviour in shiny:

$ echo "some text" | shiny-server

Note: or with docker using rocker/shiny:

$ docker run --rm -it -p 80:3838 -v $(pwd):/srv/shiny-server/ rocker/shiny bash
# and then run in the container
$ echo "some text" | shiny-server

app.R :

library(shiny)
ui <- pageWithSidebar(
    headerPanel("example"),
    sidebarPanel(),
    mainPanel(textOutput("my_stdin"))
)

server <- function(input, output, session) {
    output$my_stdin <- renderText({
        f <- file("stdin")
        i <- readLines(f)
        close(f)
        i
    })
}
shinyApp(ui, server)

Why is there no output from shiny?

The actual szenario is reading streaming data from $ nc -u -l 1.2.3.4 1234 but the simple example above also illustrates my problem.

PS: I did ask this a while ago also on Stackoverflow but there are no answers so far: https://stackoverflow.com/questions/50792645/how-to-read-from-stdin-in-shiny