3

I am attempting to add markers to a map based on coordinates uploaded by a user. I am having trouble storing the file input as a data frame and then passing the coordinates from the data frame to the proxy map to add markers.

ui <- fluidPage(
  
  titlePanel(title = "My Dashboard"),
  
  sidebarLayout(
      fileInput(inputId = "file",
                label = "File upload"),
      
    mainPanel(
      
      leafletOutput("mymap")
      
    )
  )
)

server <- function(input, output) {

  m <- leaflet() %>%
    setView(lng = -71.0589,
            lat = 42.3601,
            zoom = 12) %>%
    addProviderTiles(providers$CartoDB.Positron)

  output$mymap <- renderLeaflet(m)

    observe({
    input$file
    df <- read.csv('input$file$datapath')
    proxy <- leafletProxy("mymap", data = df)
    proxy %>% addMarkers(~long, ~lat)
  })

shinyApp(ui = ui, server = server)
1
  • 1
    Hi welcome to SO. Can you be more precise ? Do you have errors when you try to launch the app ? To get more answers, you should make a reproductible example : provide a small extract of your data would be nice so we just need to copy paste your code. Here I think you should change df <- read.csv('input$file$datapath') to df <- read.csv(input$file$datapath) for starter Commented Oct 12, 2021 at 7:36

1 Answer 1

2

You were almost there, just change the way how you are reading the file to

observe({
   req(input$file)
   df <- read.csv(input$file$datapath)
   proxy <- leafletProxy("mymap", data = df)
   proxy %>% addMarkers(~long, ~lat)
})

That is removing the quotes '. The req makes sure that no error is thrown when there is no upload yet. When uploading a csv make sure that there are columns labeled long and lat.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.