我无法获得一个闪亮的应用程序来渲染绘图地图。
我收到的错误消息是:object' STATE'找不到
情节部分基本上来自本教程: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)
答案 0 :(得分:1)
错误在这一行:
plot_ly(z = df()[[input$var]], text = df()[[STATE]], locations = df()[[STATE]]
由于未引用STATE
,因此您要告诉R查找名称存储在对象STATE
中的列。如果你想获取名为&#34; STATE&#34;的列,你应该引用这个词,所以:
plot_ly(z = df()[[input$var]], text = df()[["STATE"]], locations = df()[["STATE"]]
或者指定值&#34; STATE&#34;使用STATE
到对象STATE <- "STATE"
。它不是一个非常好的解决方案,但它可以帮助您更好地理解问题。
希望这有帮助!