我正在尝试构建一个基于 Shiny 包的动态 Web 应用程序。
我在带有 RStudio 1.1.4 的 Windows 10 上运行 R3.5.1 x64。我需要变量tabelaHeatMap
并且weightHeatMap
没有固定值,因为我正在动态检索它们并且我不想要太多的代码重复。但我发现的唯一动态方法不起作用。
library('shiny')
library('googleway')
ui <- fluidPage(
sidebarLayout(
position = "right",
sidebarPanel = sidebarPanel(
radioButtons(
inputId = "radioButton",
label = h3("Dados para visualização"),
choices = list("Receita líq." = 1,
"Custos/receita líq." = 2),
selected = 2
)
),
mainPanel = mainPanel(google_mapOutput(outputId = "map", width = "100%"))
)
)
server <- function(input, output) {
map_key = #HERE I PUT MY ACTUAL GOOGLE MAPS KEY
tabela1 = data.frame(
latitude = c(1, 2, 3),
longitude = c(1, 2, 3),
receita_liq = c(1, 2, 3)
)
tabela2 = data.frame(
latitude = c(1, 2, 3),
longitude = c(1, 2, 3),
custo_por_receita_liq = c(1, 2, 3)
)
tabelaHeatMap = tabela2
weightHeatMap = "custo_por_receita_liq"
output$map <- renderGoogle_map({
gmap = google_map(
fullscreen_control = TRUE,
street_view_control = FALSE,
map_type_control = FALSE,
search_box = FALSE,
key = map_key
)
#Error
add_heatmap(
map = gmap,
data = tabelaHeatMap,
lat = "latitude",
lon = "longitude",
option_radius = 0.25,
weight = weightHeatMap
)
})
observeEvent(input$radioButton, {
if (input$radioButton == 1) {
tabelaHeatMap <<- tabela1
weightHeatMap <<- "receita_liq"
}
else if (input$radioButton == 2) {
tabelaHeatMap <<- tabela2
weightHeatMap <<- "custo_por_receita_liq"
}
#Also produces the same error
update_heatmap(
map = google_map_update(map_id = "map"),
data = tabelaHeatMap,
lat = "latitude",
lon = "longitude",
weight = weightHeatMap
)
})
}
shinyApp(ui = ui, server = server)
从代码中可以看出,对方法的调用add_heatmap
并update_heatmap
抛出错误。一个外部变量 ,tabelaHeatMap
似乎是可访问的,但另一个 ,weightHeatMap
使程序在 eval 中抛出 Error: object 'weightHeatMap' not found 错误。两者都在if
s 中可见,但是我不知道 R 或 Shiny 有一些“陷阱”。
为什么程序不能weightHeatMap
在代码的那个特定位置“找到”?我该如何克服呢?