I'm trying to utilize Google's API for their drop down search feature to map a selected address using Leaflet. However, I also have an existing dataset that I'd like to add to the Leaflet map based on a checkboxInput. This means that both need to be reactive, and I'm having a hard time rbind-ing this data.
I have utilized code from the responses to R Shiny map search input box in order to create the code in the ui function that creates the Google dropdown.
I am using RCurl to pull in a dataset from Google Drive
library(RCurl)
library(shiny)
library(googleway)
myCsv <- getURL("https://docs.google.com/spreadsheets/d/e/2PACX-1vTc6H0ikgU53DJsGWzaSJO__77xIVkwAbyI2vjkYuHSAjj9hzWVtVAOhah8B5baDO4ilfqJTm5OWFzW/pub?output=csv")
location_data = read.csv(textConnection(myCsv))
or if easier to read in via table:
Locations Latitude Longitude
location1 40.72993 -74.00022
location2 40.72993 -74.00022
location3 40.67678 -73.98048
location4 40.76954 -73.95436
location5 40.72504 -73.98145
location6 40.80251 -73.96745
My ui function:
ui <- basicPage(
div(
checkboxInput("checkbox", "Map Other Locations", value = FALSE),
textInput(inputId = "my_address", label = "Type An Address")
,textOutput(outputId = "full_address")
#Google HTML dropdown code
,HTML(paste0(" <script>
function initAutocomplete() {
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('my_address'),{types: ['geocode']});
autocomplete.setFields(['address_components', 'formatted_address', 'geometry', 'icon', 'name']);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
var addressPretty = place.formatted_address;
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || ''),
(place.address_components[3] && place.address_components[3].short_name || ''),
(place.address_components[4] && place.address_components[4].short_name || ''),
(place.address_components[5] && place.address_components[5].short_name || ''),
(place.address_components[6] && place.address_components[6].short_name || ''),
(place.address_components[7] && place.address_components[7].short_name || '')
].join(' ');
}
var address_number =''
address_number = [(place.address_components[0] && place.address_components[0].short_name || '')]
var coords = place.geometry.location;
//console.log(address);
Shiny.onInputChange('jsValue', address);
Shiny.onInputChange('jsValueAddressNumber', address_number);
Shiny.onInputChange('jsValuePretty', addressPretty);
Shiny.onInputChange('jsValueCoords', coords);});}
</script>
<script src='https://maps.googleapis.com/maps/api/js?key=", key,"&libraries=places&callback=initAutocomplete' async defer></script>"))
#map output
,leafletOutput("mymap",height = 600)
)
)
My server code:
server <- function(input, output, session){
#if checkbox is checked, rbind location + google data
#if checkbox not checked, provide google data
location_data_reactive <- reactive({
if (input$checkbox == TRUE) {
input_address <- input$my_address
not_a_df <- google_geocode(address = my_address)
coords <- geocode_coordinates(not_a_df)
coords_to_rbind <- c(as.character("Input Location"), coords$lat[1], coords$lng[1])
data_to_map <- rbind(location_data, test_to_rbind)
final_data <- rbind(location_data, my_coords)
} else if (input$checkbox == FALSE) {
input_address <- input$my_address
not_a_df <- google_geocode(address = my_address)
coords <- geocode_coordinates(not_a_df)
final_data <- c(coords$lat[1], coords$lng[1])
colnames(final_data) <- c("Latitude", "Longitude")
} else { location_data[0,] }
})
#leaflet map
output$mymap <- renderLeaflet({
location_data_to_map <- location_data_reactive()
m <- leaflet(data = location_data_to_map) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addAwesomeMarkers(lng = ~Longitude,
lat = ~Latitude
)
m
})
}
shinyApp(ui, server)
I receive the below error message for everything I try to do and run with stacking this data or rearranging the if statement.
Error: address must be a string of length 1
Can anyone assist with stacking these two data reactive data frames? Thank you!