【发布时间】: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 的字符向量
我真的不知道为什么。我什至需要在这个数据框上做更多的事情,但即使是最基本的我也无法解决。
【问题讨论】: