ShinyApp 不在 R 中渲染绘图

ShinyApp not rendering plots in R

我在 Shiny 上渲染任何可视化时遇到问题。我尝试了许多不同的情节,其中 none 成功了。这是代码:

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)

SQL 命令似乎没问题,因为我使用 shinyApp 结构之外的这段代码成功提取了数据。

  1. View(.) 的 return 值为 NULL,因此您的 renderTable 将始终为空;做到 p.

  2. 如果您的 ui 包含 plotlyOutput,则将 renderTable 替换为 plotly::renderPlotlyshiny::renderTable 的 ui-component 是 shiny::tableOutput.

    renderTable 旨在以表格形式显示类似 data.frame 的数据,而不是绘图。

    选择:

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

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