Plot.ly地图不呈现在闪亮的应用程序
我无法获得闪亮的应用程序呈现一个剧情地图。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)
回答:
的错误是在这一行:
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
。这不是一个非常好的解决方案,但它可以帮助你更好地理解问题。
希望这会有所帮助!
以上是 Plot.ly地图不呈现在闪亮的应用程序 的全部内容, 来源链接: utcz.com/qa/257721.html