问题描述
这个闪亮的应用代码尝试使用传单搜索优化的路由图。它使用旅行商问题(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 (将#修改为@)