How to renderPrint?

Viewed 360

I'm trying to create a console and enter code to display in the panel. Based on this solution, I created this code. But when running, for example, the following lines the output is printed in the console, but not on the Shiny App.

x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))
print(x)

enter image description here

How do I make this output appear on the Shiny app?

Code:


library(shiny)
library(shinyAce)

ui <- fluidPage(
    wellPanel(
      tagList(wellPanel(uiOutput("plotorprint")))
        ),
    wellPanel(
        aceEditor("code", mode = "r", height = "100px", 
                  highlightActiveLine = FALSE, 
                  showLineNumbers = FALSE, 
                  minLines = 2,  
                  maxLines = 30, 
                  fontSize = 16, 
                  autoScrollEditorIntoView = TRUE,
                  placeholder = "CONSOLE"))
)


server <- function(input, output) {
  
  ace_obj <- reactive({
    eval(parse(text=input$code))
  })

 
  output$printout <- renderPrint({
    ace_obj()
  })
  
  output$plotout <- renderPlot({        
    ace_obj()
  })
  
  output$plotorprint <- renderUI({
    if (is.data.frame(ace_obj)) { # Check if output of f(x) is data.frame
      verbatimTextOutput("printout") # If so, create a print
    } else {                      # If not,
      plotOutput("plotout") # create a plot
    }
  })
  
  
}



shinyApp(ui = ui, server = server)

I'm trying to display output in the white space above the console panel.

enter image description here

2 Answers

You could use req() (require) to only evaluate renderPrint and renderPlot if your condition is met (you can add as many conditions as you like for both plot and print in require):

library(shiny)
library(shinyAce)

ui <- fluidPage(
  wellPanel(
    tagList(wellPanel(
      verbatimTextOutput("printout"),
      plotOutput("plotout")
    ))
  ),
  wellPanel(
    aceEditor("code", mode = "r", height = "100px", 
              highlightActiveLine = FALSE, 
              showLineNumbers = FALSE, 
              minLines = 2,  
              maxLines = 30, 
              fontSize = 16, 
              autoScrollEditorIntoView = TRUE,
              placeholder = "CONSOLE"))
)


server <- function(input, output) {
  
  ace_obj <- reactive({
    eval(parse(text=input$code))
  })
  
  
  output$printout <- renderPrint({
    req(ace_obj(), is.data.frame(ace_obj()))
    ace_obj()
  })
  
  output$plotout <- renderPlot({        
    req(ace_obj())
    ace_obj()
  })
  
}



shinyApp(ui = ui, server = server)

A simpler solution including renderUI is fixing the if-condition. ace_obj is a reactive object and is evaluated only with ace_obj():

output$plotorprint <- renderUI({
  if (is.data.frame(ace_obj())) { # Check if output of f(x) is data.frame
    verbatimTextOutput("printout") # If so, create a print
  } else {                      # If not,
    plotOutput("plotout") # create a plot
  }
})

Then evaluate in your interactive shiny console without print():

x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))
x
Related