Calculating Distance between two addresses in shiny

Hello,

I am attempting to calculate the distance between two addresses with gmapdistance in a shiny app. I have the code working in an r script and not in a shiny app:

distance <- function(addr1, addr2) {
  meters <- gmapsdistance(origin = "addr1",
                          destination = "addr2",
                          mode = "driving",
                          key = Sys.getenv("KEY"))$Distance
  miles <- meters/1609.34
  return(miles)

However, when I put this in a shiny app, nothing happens. Here is my code:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput(inputId = "addr1",
                label = h4("Enter address 1:"),
                value = ""),
      textInput(inputId = "addr2",
                label = h4("Enter address 2:"),
                value = "")
    ),
    mainPanel(
      textOutput("value")
    )
  )
)

server <- function(input, output) {
  output$value <- renderText({
    meters <- gmapsdistance(origin = "addr1",
                          destination = "addr2",
                          mode = "driving",
                          key = ("KEY"))$Distance
    miles <- meters/1609.34
    return(miles)
  })
}

shinyApp(ui, server)

any insight into how to fix this would be much appreciated, thanks!!!

Welcome to the community @jillahmad17! I believe I found the issue. In the server, when you reference your inputs from the ui, you need to use input$addr1 and input$addr2 in place of "addr1" and "addr2".

1 Like

thank you so much! @scottyd22 that solved my problem :grinning:

I am attempting to make the app reactive, and here is the code:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput(inputId = "addr1",
                label = h4("Enter address 1:"),
                value = ""),
      textInput(inputId = "addr2",
                label = h4("Enter address 2:"),
                value = "")
    ),
    mainPanel(
      textOutput("value")
    )
  )
)

server <- function(input, output) {
  observeEvent ({
    input$addr1
    input$addr2
  }, {
    meters <- gmapsdistance(origin = input$addr1,
                            destination = input$addr2,
                            mode = "driving",
                            key = ("KEY"))$Distance
       miles <- meters/1609.34
       print(miles)
  })
}

however, this code gives me an error: Warning: Error in UseMethod: no applicable method for 'xmlChildren' applied to an object of class "NULL"

do you have any insight into this? thanks again for your help!

Glad it worked! As for the new issue, the inputs start off as NULL until something is entered, and the code is trying to execute gmapdistance() for NULL values. You can add a req() statement just before meters, which basically checks to see if a value exists (i.e. is not NULL) prior to carrying out the calculation. It would look something like the example below.

server <- function(input, output) {
  observeEvent ({
    input$addr1
    input$addr2
  }, {
    req(input$addr1, input$addr2)
    meters <- gmapsdistance(origin = input$addr1,
                            destination = input$addr2,
                            mode = "driving",
                            key = 'YOUR API KEY')$Distance
    miles <- meters/1609.34
    print(miles)
  })
1 Like

wow thank you so much. You helped me a bunch!

Scotty I should have taken my key out before posting, do you mind removing the API key please? Thank you again

Of course. I should have stripped it out too. All set.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.