Search code examples
rshinyrendering

ShinyApp not rendering plots in R


I am having problems rendering any visualizations on Shiny. I tried many different plots, none of them worked. Here is the code:

library(shiny)

ui <- fluidPage("Comex", 
                selectInput("paises","Selecione o Destino da Exportação",PAISES$NO_PAIS, selected = PAISES$NO_PAIS[55]),
                plotlyOutput(outputId = "table"))

server <- function(input, output){
  output$table <- renderTable({
    p <- RPostgres::dbGetQuery(con, paste0("SELECT CO_ANO, NO_PAIS, SUM(VL_FOB) 
                                        FROM comex 
                                        INNER JOIN paises ON comex.CO_PAIS = paises.CO_PAIS 
                                        WHERE (SG_UF_MUN = 'AL') AND (NO_PAIS = '",input$paises,"')
                                        GROUP BY NO_PAIS, CO_ANO"))
    View(p)
  })}


shinyApp(ui, server)

The SQL command seems fine, as I successfully extracted data with this very code outside the shinyApp structure.


Solution

    1. The return value from View(.) is NULL, so your renderTable will always be blank; just make it p.

    2. If your ui contains plotlyOutput then replace renderTable with plotly::renderPlotly. The ui-component for shiny::renderTable is shiny::tableOutput.

      renderTable is intended for a tabular display of data.frame-like data, not a plot.

      Choose either:

      ui <- fluidPage(
        ...,
        tableOutput("table")
        ...
      )
      server <- function(input, output, session) {
        output$table <- renderTable({
          # code that returns a data.frame
        })
      }
      

      or

      ui <- fluidPage(
        ...,
        plotlyOutput("myplot")
        ...
      )
      server <- function(input, output, session) {
        output$myplot <- plotly::renderPlotly({
          # ...
          plot_lt(...)
        })
      }