1

Below you will find my first project trying to learn Shiny. I am trying to implement the below idea:

  1. choose with slide bar a number and display the correct answer of mathematical expression
  2. have a map
  3. insert a point at the map
  4. read a csv ( data with distances, csv located on the desktop)
  5. choose from csv the category that I want in order to plot a circle around the point in the map

Until now I have achieved the 3 first but I really struggle to find a solution for the next two. Can anyone guide me on how I will do it?

library(shiny)
library(shinythemes)
library(leaflet)
df <- data.frame(longitude = 26, latitude = 39)
# Define UI for slider app ----
ui <- fluidPage(
  #Navbar structure for UI
  navbarPage("SAR Model", theme = shinytheme("united"),
             tabPanel("Toblers Function", titlePanel("Toblers Function - Insert slope") ,
             sidebarLayout(
               sidebarPanel(
                 # Input: Slope interval with step value ----
                 sliderInput("slope", "Slope:",
                             min = -0.60, max = 0.50,
                             value = 0.0, step = 0.01)), 
               # Main panel for displaying outputs ----
               mainPanel( 
                 
                 # Output: Table summarizing the values entered ----
                 tableOutput("Values"),
                 tableOutput("slope")))),
  
tabPanel("Map",titlePanel("SAR MAP - insert a point"),mainPanel(leafletOutput("map", width = "100%", height = "700"))),
tabPanel("Data",titlePanel("Data Summury"), dataTableOutput("table"))))
    
server <- function(input, output) {
  # Reactive expression to create data frame off input value ----
  sliderValues <- reactive({
    data.frame(
      Name = c("Slope"), 
      Value = as.character(c(input$slope)),
      stringsAsFactors = TRUE)
  })
  # Show the values in an HTML table ----
  output$values <- renderTable({
    sliderValues()
  })
  output$slope <- renderText({
    paste0("The speed is ", 6*exp(-3.5*abs(input$slope+0.05)),"Km/h")
  })
output$map <- renderLeaflet({
  leaflet() %>%addTiles()
})
df_r <- reactiveValues(new_data = df)
# reactive list with id of added markers
clicked_markers <- reactiveValues(clickedMarker = NULL)
observeEvent(input$map_click, {
  click <- input$map_click
  click_lat <- click$lat
  click_long <- click$lng
  clicked_markers$clickedMarker <- c(clicked_markers$clickedMarker, 1)
  id <- length(clicked_markers$clickedMarker)
  # Add the marker to the map
  leafletProxy('map') %>%
    addMarkers(lng = click_long, lat = click_lat, group = 'new_circles',
               options = markerOptions(draggable = TRUE), layerId = id)
  # add new point to dataframe
  df_r$new_data <- rbind(rep(NA, ncol(df)), df_r$new_data)
  df_r$new_data$longitude[1] <- click_long
  df_r$new_data$latitude[1] <- click_lat
})
observeEvent(input$map_marker_mouseout,{
  click_marker <- input$map_marker_mouseout
  id <- input$map_marker_mouseout$id
  
  if(click_marker$lng != df_r$new_data$longitude[id] | click_marker$lat != df_r$new_data$latitude[id]){ # why is this always true??
    df_r$new_data$longitude[id] <- click_marker$lng
    df_r$new_data$latitude[id] <- click_marker$lat
  }
})
output$table <- renderDataTable({df_r$new_data})
}
shinyApp(ui = ui, server = server)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Tasos
  • 11
  • 1

0 Answers0