【问题标题】:ShinyApp not rendering plots in RShinyApp 不在 R 中渲染绘图
【发布时间】:2020-10-16 15:27:10
【问题描述】:

我在 Shiny 上渲染任何可视化时遇到问题。我尝试了许多不同的情节,但都没有奏效。代码如下:

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 结构之外使用这段代码成功提取了数据。

【问题讨论】:

  • 你能包含标准输出/标准错误吗?

标签: r shiny rendering


【解决方案1】:
  1. View(.) 的返回值为NULL,因此您的renderTable 将始终为空;就让它p

  2. 如果您的 ui 包含 plotlyOutput,则将 renderTable 替换为 plotly::renderPlotlyshiny::renderTable 的 ui 组件是 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(...)
      })
    }
    

【讨论】:

  • 太棒了,它现在渲染了表格。但是我遇到了另一个问题:由于某种原因,SQL 'SUM(VL_FOB)' 在闪亮的渲染表上返回 0,而相同的代码在 R 中返回正确的数字。
  • 这表明input$paises(闪亮)不是您在控制台上使用的。但这确实是一个不同的问题,将由样本数据提供信息。包含shiny 和SQL 查询的问题很难处理,因为确保可重复性的最佳方法是创建data.frame,插入(临时)表,然后运行查询该表的shiny 应用程序。祝你好运!
猜你喜欢
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 2022-06-19
  • 2020-12-13
  • 1970-01-01
  • 2022-01-18
  • 2023-03-11
  • 2021-10-26
相关资源
最近更新 更多