2017-12-02 269 views
1

我無法獲得閃亮的應用程序呈現一個劇情地圖。Plot.ly地圖不呈現在閃亮的應用程序

我得到的錯誤信息是:對象「狀態」未找到

的plotly部分主要來自本教程:https://plot.ly/r/shinyapp-map-click/#shiny-app

不知道它與我的活性元素或者不這樣做。反應元素工作良好,創建一個ggplot地圖。任何幫助表示讚賞。

library(shiny) 
library(dplyr) 
library(plotly) 


state_tot <- read.csv("https://raw.githubusercontent.com/bkreis84/Data-604---Model/master/VIS/codeS.csv") 


ui <- fluidPage(

    # Application title 
    titlePanel("IRS Tax Data 2010 - 2015"), 

    sidebarLayout(
     sidebarPanel(

     selectInput("var", 
        label = "Select Variable:", 
        choices = c('Unemployment $ per Return' = 'UNEMP_COMP_PR', 
           '% of Returns with Business Income' = 'PERC_BUSINESS_RETURN', 
           '% with Real Estate Deduction' = 'PERC_RE', 
           'AGI Per Return' = 'AGI_PR'), 
        selected = '% with Business Income'), 


     sliderInput("yr", 
        "Select Year:", 
        min = 2010, 
        max = 2015, 
        value = 2015) 



    ), 


     # Show a plot of the generated distribution 
     mainPanel(
     plotlyOutput("plot") 



    ) 
) 
) 


server <- function(input, output) { 

    select <- reactive({ 
    year_sel <- input$yr 

    }) 

    df <- reactive({ 
    state_tot %>% 
     filter(YEAR == select()) 
    }) 

    high <- reactive({ 
    switch(input$var, 
      "PERC_BUSINESS_RETURN" = "green", 
      "AGI_PR" = "green", 
      "PERC_RE" = "green", 
      "UNEMP_COMP_PR" = "red") 
    }) 

    low <- reactive({ 
    switch(input$var, 
      "PERC_BUSINESS_RETURN" = "red", 
      "AGI_PR" = "red", 
      "PERC_RE" = "red", 
      "UNEMP_COMP_PR" = "green") 
    }) 



    output$plot <- renderPlotly({ 
    g <- list(
     scope = 'usa', 
     projection = list(type = 'albers usa'), 
     lakecolor = toRGB('white') 
    ) 
    plot_ly(z = df()[[input$var]], text = df()[[STATE]], locations = df()[[STATE]], 
      type = 'choropleth', locationmode = 'USA-states') %>% 
     layout(geo = g) 
    }) 



} 

# Run the application 
shinyApp(ui = ui, server = server) 

回答

1

的錯誤是在這一行:

plot_ly(z = df()[[input$var]], text = df()[[STATE]], locations = df()[[STATE]] 

由於STATE沒有加引號,你告訴R鍵尋找該名被存儲在對象STATE列。如果你想獲取所謂的「國家」之列,你要引用的話,那麼:

plot_ly(z = df()[[input$var]], text = df()[["STATE"]], locations = df()[["STATE"]] 

或可替代值「國家」 STATE <- "STATE"分配給對象STATE。這不是一個非常好的解決方案,但它可以幫助你更好地理解問題。

希望這會有所幫助!

+0

就是這樣。謝謝! – BrianK

+0

如果有幫助,請您[接受](https://stackoverflow.com/help/someone-answers)答案?謝謝! – Florian

相關問題