微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Shiny R 传单反应值

如何解决Shiny R 传单反应值

这个闪亮的应用代码尝试使用传单搜索优化的路由图。它使用旅行商问题(TSP)算法。需要的是,当用户更改 numericInput 值时,地图上的点数会发生变化。实际上,点数是常数,值为 4。 如何使用 numericInput 的“n”值获得反应函数以重新计算出现在新“output$newmap”图中的新优化解决方案?

library(shiny)
library(shinythemes)
library(TSP)
library(leaflet)
library(dplyr)
library(magrittr)
library(readr)



####################################
# User interface                   #
####################################

# Define UI for application that draws a histogram
ui <- fluidPage( 
    
  
     # Application title
   #titlePanel("Old Faithful Geyser Data"),tags$head("Old Faithful Geyser Data" ),tags$h2("Proyecto"),# Sidebar with a slider input for number of bins 
   sidebarLayout(position = "right",sidebarPanel(
         sliderInput("bins","Number of bins:",min = 1,max = 50,value = 3),hr(),actionButton("submitbutton","Submit",class = "btn btn-primary"),numericInput("obs2","Observations:",7,max = 100),sliderInput("integer","Integer:",min = 0,max = 200,value = 5)
         
         ),#este signo cierra el sidebar Panel
         
         mainPanel( verbatimtextoutput("value"),textoutput("selected_var"),tableOutput("values"),plotOutput("distPlot2"),leafletoutput("newmap"),verbatimtextoutput('contents'))
                    
         
  # Show a plot of the generated distribution

)
)


####################################
# Server                           #
####################################

server <- function(input,output,session) { 
  
  output$value <- renderText({input$obs2 })
  
  output$selected_var <- renderText({ 
    paste("You have selected",input$integer)
  })
  
  
  
  sliderValues <- reactive({
    
    data.frame(
      Name = c("Integer"),Value = as.character(c(input$integer)),stringsAsFactors = FALSE)
    
  })
  
  # Show the values in an HTML table ----
  output$values <- renderTable({
    sliderValues()
  })
  
    output$distPlot2 <- renderPlot({
    hist(rnorm(input$bins))
  })
  
  n<-4  ## THIS VALUE  SHOULD CHANGE WITH THE NUMERICINPUT

    # Tibble containing the geographic locations for our TSP problem
  data2 <- tibble(
    id = 1:n,lng = rnorm(n,mean = 9.18855,sd = 0.005),lat = rnorm(n,mean = 45.464685,sd = 0.005)
  )
 
  # distance matrix 
  dist_mat <- dist(
    data2 %>% select(lng,lat),method = 'euclidean' 
  )
  
  # Initialize the TSP object
  tsp_prob <- TSP(dist_mat)
  
  tsp_prob <- insert_dummy(tsp_prob,label = 'dummy')
  
  # TSP solver
  tour <- solve_TSP(
    tsp_prob,method = 'two_opt',control = list(rep = 16)
  )
  path <- names(cut_tour(tour,'dummy'))
  
  
  #str(tour)
  # Prepare the data for plotting
  data2 %<>% 
    mutate(
      id_order = order(as.integer(path))
    )
  
  # render newmap con tsp
  
  output$newmap <- renderLeaflet({data2 %>% 
      arrange(id_order) %>% 
      leaflet() %>% 
      addTiles() %>% 
      addCircleMarkers(
        ~lng,~lat,fillColor = 'red',fillOpacity = 0.5,stroke = FALSE
      ) %>% addpolylines(~lng,~lat)})
  
  
  
  
}
# Run the application 
shinyApp(ui = ui,server = server)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。