I am trying to build a dynamic web application based on Shiny package.
I am running R3.5.1 x64 on Windows 10 with RStudio 1.1.4.
I need the variables tabelaHeatMap
and weightHeatMap
to not have fixed values, because I'm retrieving them dynamically and I don't want too much code repetition. But the only dynamic way I have found is not working.
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)
As can be seen in the code, the calls to the methods add_heatmap
and update_heatmap
throw error.
One of the outer variables, tabelaHeatMap
, appears to be accessible, but the other, weightHeatMap
, makes the program throw the Error in eval: object 'weightHeatMap' not found error.
Both are visible inside the if
s, but there is some "gotcha" with R or Shiny that I am not aware.
Why can't the program "find" weightHeatMap
in that specific place of the code? How can I overcome this?