3

I am using an R package that, in addition to calculating and returning things, prints some very useful info to the console. For example, it prints what iteration it is on right now.

How could I print that console output directly to my UI?

Assume this is my UI:

ui <- shinyUI(
  fluidPage(
    titlePanel("Print consol output"),
    sidebarLayout(
      sidebarPanel(actionButton("go", "Go")),
      mainPanel(
        verbatimTextOutput("console_text")
      )
    )
  )
)

My user clicks on actionButton “Go” and my package starts doing things - while sending stuff to the console at the same time. I guess, I want the content of the console to be saved as output$console_text - but I don’t know if that’s the right approach and how to do it.

I didn't want to make the code super-complicated. So, instead of a package, I created my own little printing function in Server.

server <- function(input, output, session) {
  myfunction <- function(x) { 
     for(i in 1:x) cat(i)
     return(x)
  }
  observeEvent(input$go, {
    {
       # This is probably wrong:
      output$console_text <- renderPrint(onerun <- myfunction(20))
    }
  })
}

shinyApp(ui, server)

Thank you very much!

user3245256
  • 1,842
  • 4
  • 24
  • 51
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 05 '18 at 22:24
  • I know, didn't want to make it too complicated. So, instead of a package I created a little function that prints to console. – user3245256 Feb 05 '18 at 22:26
  • Well, the function won’t return till The for loop is done so you can’t print anything till it’s done. You can capture output you with `cat()` by using `caputure.output()` and print it after the fact – MrFlick Feb 05 '18 at 22:38
  • But that's the issue. The package I am using is running thousands of iterations. And for every 100s iteration it prints something to console - just like my function prints something for each value of i. So, it's absolutely impossible to print this to UI? – user3245256 Feb 05 '18 at 22:49
  • If the function uses cat() and a for loop and you don’t want to change it, then there’s not really much you can do. – MrFlick Feb 05 '18 at 23:05
  • I hope I have understood your question correctly. I had a similar issue. Have a look at the RALSA package where this is solved: https://cran.r-project.org/package=RALSA – panman Apr 09 '23 at 18:02

0 Answers0