1

下面你会发现我第一个尝试学习 Shiny 的项目。我正在尝试实现以下想法:

  1. 用滑动条选择一个数字并显示数学表达式的正确答案
  2. 有地图
  3. 在地图上插入一个点
  4. 读取 csv (带距离的数据,csv 位于桌面上)
  5. 从 csv 中选择我想要的类别,以便在地图中的点周围绘制一个圆圈

到目前为止,我已经获得了第 3 个,但我真的很难为接下来的两个找到解决方案。谁能指导我如何做?

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

0 回答 0