0

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 ifs, 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?

  • I'm don't know enough about the scoping of shiny apps to answer your question directly. But if I was going to do this I would put `weightHeatMap` as a reactive variable. Then use an `eventReactive` to update the heat map. – Ian Wesley Dec 28 '18 at 17:56
  • 1
    This is probably related to [this issue](https://github.com/SymbolixAU/googleway/issues/168) which I haven't resolved yet. – SymbolixAU Dec 30 '18 at 01:25

1 Answers1

1

This seems like a reactivity issue. You could define reactive functions and store the variables to update: I have not made any changes to the UI Code, so I have included only the server code here.

server <- function(input, output) {
map_key = "testKey" 

getRadioInput1 <- shiny::reactive({
    mapVariablesButton1 <- list(tabelaHeatMap = tabela1, weightHeatmap = "receita_liq")
    return(mapVariablesButton1)  
  })  

getRadioInput2 <- shiny::reactive({
    mapVariablesButton2 <- list(tabelaHeatMap = tabela2, weightHeatmap = "custo_por_receita_liq")
    return(mapVariablesButton2)  
  })  

getAllInputs <- shiny::eventReactive(
    input$radioButton, {
      if (input$radioButton == 1) {
        print("Radio button == 1")
        tabelaHeatMap <- getRadioInput1()$tabelaHeatMap
        weightHeatmap <- getRadioInput1()$weightHeatmap
        finalInputs <- list(updatedHeatMap = tabelaHeatMap, updatedWeightMap = weightHeatmap)
        return(finalInputs)
      } else {
        print("Radio button == 2")
        tabelaHeatMap <- getRadioInput2()$tabelaHeatMap
        weightHeatmap <- getRadioInput2()$weightHeatmap
        finalInputs <- list(updatedHeatMap = tabelaHeatMap, updatedWeightMap = weightHeatmap)
        return(finalInputs)
      }
    }
  )

  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)
  )

  shiny::observe(print(getAllInputs()))


  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 = getAllInputs()$updatedHeatMap,
      lat = "latitude",
      lon = "longitude",
      option_radius = 0.25,
      weight = 1
    )
  })

  #Also produces the same error
    update_heatmap(
      map = google_map_update(map_id = "map"),
      data = getAllInputs()$updatedHeatMap,
      lat = "latitude",
      lon = "longitude",
      weight = 1
    )
  })
}

I have tried this on my machine, and the code runs. I see the output very briefly (this is because my keys are not valid) and the object not found error is no more.

The only issue I still face, is that of the weight argument in the add_heatmap() and update_heatmap() function. I suspect that this could be a bug. When I add weight = getAllInputs()$updatedWeightMap, the console throws a function getAllInputs() not found error, however I have left my debug code in there to verify that getAllInputs() does indeed exist, and is updating.

Based on all the evidence, it seems reasonable to conclude the following:

  • There is a really silly error in the code, which both of us missed :-) or / and,
  • There is an issue that needs to be reported to the googleway package developers.

Either way, I hope that what I have provided above helps get you to your solution quicker.

RickTastic
  • 292
  • 3
  • 9
  • Hi, thank you for your input. But I tested your suggestion, and the same error still occurs. As per SymbolixAU comment in my question, this does seem like a problem specific with googleway library. – Thullyo Castilho Jan 02 '19 at 11:25
  • Hi @ThullyoCastilho , I have amended the code and tested briefly on my machine. I have also kept my debug code in there to try and further assist and more explanation provided above. – RickTastic Jan 02 '19 at 13:58