3

I recently ran into a problem that as my computing took lots of time, I'd like to show the text output on shiny rather than progress bar or loading message. My function looked like

printText <- function() {
  for(i in 1:10){
    Sys.sleep(0.1)
    print(paste("My text", i))
    y = i + 1
  }
  return(y)
}

I can print it with verbatimTextOutput but I also need the returned value of y. Now I am doing this:

runApp(list(
  ui = shinyUI(fluidPage(
    titlePanel("Print consol output"),
    sidebarLayout(
      sidebarPanel(actionButton("go", "Go")),
      mainPanel(
        verbatimTextOutput("text")
      )
    )
  )),
  server = shinyServer(function(input, output, session){
    observeEvent(input$go, {
      output$text <- renderPrint({
          y <- printText()
      })
    })
  })
))

The problem is that if I want to use the returned y I need to create a reactive object, which may take me 2 times longer because I execute printText() twice, while printing and pass to reactive object.

How could I get the value of y and print the text onto shiny without duplicated work? Notice that I'm not gonna use progress bar because my real function is not loop actually. What I want is to capture the text output during the process and get returned value.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Eric Hung
  • 512
  • 8
  • 23
  • There are two parts of the question: 1) console output: do you want these to be displayed to the shiny interface in real time (I did a little research and it seems to be quite difficult to do so)? 2) returned value: You only need the returned value after the long calculation right? I think you can probably use a global variable. – Xiongbing Jin Apr 02 '16 at 22:35
  • @warmoverflow 1) I've searched for a while and found nothing:( 2) you are right. Using global variable is a good idea. – Eric Hung Apr 02 '16 at 23:39
  • For 1, best solution I found is to use Javascript to update the page. See here for an example. http://stackoverflow.com/questions/17325521/r-shiny-display-loading-message-while-function-is-running/22076307#22076307 – Xiongbing Jin Apr 03 '16 at 00:00

1 Answers1

-1

You can do this way :

ui.r

textOutput("id")

server.r

output$id<-renderText(input$y)