Kalluri Siva
Kalluri Siva

Reputation: 43

Render output of print(data.tree) to the UI in shiny

I have output of data.tree package as follows in the console :

print(acme, "cost", "p")

#Out of Data.tree print JPEG

enter image description here

I want to render the same output structure in the shiny UI.

I have tried paste(console.output(print(acme, "cost", "p")),collapse = "
"), but the output structure is not same.

ui <- fluidPage(
          sliderInput(inputId = "slider", 
          label = "My number", 
          min = 300, 
          max = 19000,
          value = 5000),
          htmlOutput("mytext")
                   )

server <- function(input, output) {
          output$mytext <- renderText({
          paste(capture.output(print(acme, "cost", "p")),collapse = "<br/>")
       })
       }

shinyApp(ui = ui, server = server)

Please suggest me any render functions , that will print the output exactly like the above in the Shiny UI.

Upvotes: 4

Views: 784

Answers (1)

James
James

Reputation: 125

TL:DR; Replace renderText() with renderPrint() in server and htmlOutput() with verbatimTextOutput() in ui.

The issue is with how renderText() works. It concatenates all the text input with cat() which will fail when using against the data.tree object, which is a list. You should use renderPrint() to capture the output of your print statement. No capture.output() required. The renderPrint() will not work with various output methods, the safest one to use is verbatimTextOutput(). I base that recommendation from experimentation, not actual documentation research. htmlOutput might work but I an not sure.

Updated code:

ui <- fluidPage(
          sliderInput(inputId = "slider", 
          label = "My number", 
          min = 300, 
          max = 19000,
          value = 5000),
          verbatimTextOutput("mytext")
                   )

server <- function(input, output) {
          output$mytext <- renderPrint({
          print(acme, "cost", "p"))
       })
       }

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions