【问题标题】:Shiny - how to display dataframe?闪亮 - 如何显示数据框?
【发布时间】:2019-11-30 22:48:58
【问题描述】:

我正在使用 Shiny,我尝试在 Shiny 应用程序中将数据框显示为表格。我的数据来自在线网站,我是通过 API 获取的:

make_house_representative_for_state <- function(state = "WA") {
  base_uri <- "https://api.propublica.org/congress/v1/members/"
  endpoint <- paste0(base_uri, "house/", state, "/current.json")
  response <- GET(endpoint, add_headers("X-API-Key" = ProPublica_KEY)) 
  df <- content(response, "text")
  dt <- fromJSON(df)
  output <- dt$results
  return(output)
}

API 网站:https://projects.propublica.org/api-docs/congress-api/

我将上面的代码放在另一个 R 文件中,并在我闪亮的 R 文件中获取它。

source("source/propublica.R")

state_representatives <- make_house_representative_for_state()

ui <- fluidPage(

    theme = shinytheme("united"),

    titlePanel("Congressinal Member Information"),

    tabsetPanel(
        type = "tabs", id = "nav_bar",

        tabPanel("State Rrepresentatives",
                 sidebarLayout(
                     sidebarPanel(
                         selectInput("selected_state",
                                     label = h3("Select A State"),
                                     choices = c("AL", "AK", "AZ", "AR", "CA",
                                                 "CO", "CT", "DE", "FL", "GA",
                                                 "HI", "ID", "IL", "IN", "IA",
                                                 "KS", "KY", "LA", "ME", "MD",
                                                 "MA", "MI", "MN", "MS", "MO",
                                                 "MT", "NE", "NV", "NH", "NJ",
                                                 "NM", "NY", "NC", "ND", "OH",
                                                 "OK", "OR", "PA", "RI", "SC",
                                                 "SD", "TN", "TX", "UT", "VT",
                                                 "VA", "WA", "WV", "WI", "WY"),
                                     selected = "WA"
                         )
                     ),

                     mainPanel(
                         tableOutput("state_representatives")
                     )
                 )
        )
    )
)

server <- function(input, output) {

    output$state_representatives <- renderUI({
        state_choose <- input$selected_state
        state_name <- query_to_state_name(state_choose)
        state <- make_house_representative_for_state(state_name)
        return(state)
        # table <- summary_info(state)
    })

}
query_to_state_name <- function(query) {
  t <- paste0("\"", query, "\"")
  return(t)
}

闪亮的应用程序确实运行了,但无论我点击哪个状态,它都什么也没有显示。当我尝试将“state_name”更改为“WA”(API 函数中的默认值)时,闪亮告诉我:

警告:writeImpl 中的错误:要写入的文本必须是长度为 1 的字符向量

我真的不知道为什么。我什至需要在这个数据框上做更多的事情,但即使是最基本的我也无法解决。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    以后发帖时,请提供以下信息:

    • 一个完全可重现的例子,意思是:从头到尾的代码(你缺少shinyapp(ui, server)),代码中的数据(你没有提供 API 密钥,所以你的代码不能运行),以及所有库的你使用的(我在shinythemes 上出错,因为我没有下载包(或知道它来自哪个包)

    至于您的问题,请使用renderTable 而不是renderUI。每个渲染/输出都是一对,renderUI 不与tableOutput 搭配使用。

    如果您是 Shiny 的新手,有一些关于服务器和 UI 如何相互交互的在线教程 (Example)。

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 2018-07-14
      • 2014-05-13
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多