1
votes

I am working with Shiny and I try to display a dataframe as table in the shiny app. My data came from online website and I got them by using 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 website: https://projects.propublica.org/api-docs/congress-api/

I put the above code in another R file and source it in my shiny R file.

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)
}

The shiny app did run but no matter which state I click on it just shows nothing. When I tried to change "state_name" to "WA"(the default value in the API function), the shiny told me:

Warning: Error in writeImpl: Text to be written must be a length-one character vector

I really cannot figure out why. I even need to do more on this data frame in shiny but even the most basic one I cannot solve.

1

1 Answers

0
votes

In the future when posting, please provide the following:

  • a fully reproducible example, meaning: start to finish code (you are missing shinyapp(ui, server)), data in the code (you didn't provide the API key so your code can't be run), and all the library's that you used (I got on error on shinythemes because I don't have the package downloaded (or know what package it comes from)

As for your issue, use renderTable instead of renderUI. Each render/output are a pair and renderUI doesn't go with tableOutput.

If you are new to Shiny, there are some great tutorials online about how the server and UI can interact with each other (Example).