我正在寻找提高我的闪亮应用程序性能的建议。
我构建了一个有趣的应用程序来玩乐和训练。此应用程序的目的是在用户单击地图时在地图上添加一个点。这些点也包含在数据表中。因此,这些点在地图和数据表中可见。这是代码:
this_table = data.frame(lat = NA, lng = NA, Distance = NA)
ui <- fluidPage(
navbarPage("nav", id="nav",
tabPanel("Interactive map",
tags$head(
# Include our custom CSS
includeCSS("./www/style.css")
),
leafletOutput("map", height=900),
# Shiny versions prior to 0.11 should use class = "modal" instead.
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
width = 450, height = "auto",
h2("Controls"),
DTOutput("data"),
sliderInput("distance", "Dist in meters",min=0, max=50000, step = 1, value=1000)
)
),
tabPanel("Data"
)
)
)
server <- function(input, output, session) {
# --------- MAP panel
output$map<- renderLeaflet({
leaflet(options = leafletOptions(minZoom = 6, dragging = T))%>%
addProviderTiles(provider = "OpenStreetMap.France")%>%
setView(lng = 2.43, lat=46.53,zoom = 7) %>%
setMaxBounds(lng1 = 2.43 + 9,
lat1 = 46.53 + 12,
lng2 = 2.43 - 7,
lat2 = 46.53 - 10)
})
## Observe mouse clicks and add markers
observeEvent(input$map_click, {
## Get the click info like had been doing
click <- input$map_click
clat <- click$lat
clng <- click$lng
## Add the maker to the map proxy
## not need to re-render the whole thing
## the markers a group, "markers", so you can
## then do something like hide all the markers with hideGroup('markers')
leafletProxy('map') %>% # use the proxy to save computation
addMarkers(lng=clng, lat=clat, group='markers')
})
# ------------- Data Absolute panel
this_table <- reactiveVal(this_table)
observeEvent(input$map_click, {
click <- input$map_click
t = rbind(data.frame(lat = click$lat,
lng = click$lng,
Distance = input$distance), this_table())
this_table(t)
})
observeEvent(input$delete_btn, {
t = this_table()
if (!is.null(input$data_rows_selected)) {
t <- t[-as.numeric(input$data_rows_selected),]
}
this_table(t)
})
output$data<-renderDT({
datatable(this_table(), selection = 'single', options = list(dom = 't'))
})
}
shinyApp(ui, server)
此代码有效,但是当我单击以添加点时,我可以看到数据表正在刷新。对于我的工作,我构建了另一个应用程序,但是使用了具有类似功能的OpenLayers,并且没有刷新。
这就是为什么我想知道是否有一种更有效的方式来编写我的代码,从而防止刷新数据表?
感谢所有能给我带来的帮助
编辑:我应用的所有代码
答案 0 :(得分:0)
我会尝试两种想法:
您将两次定义诸如right
之类的变量,并具有由同一事件驱动的两个离散表达式。尝试合并为一个。
sort(list, left, endIndex - 1);
sort(list, endIndex + 1, right);
这可能不适合您的情况,但可以缓解单击时出现的滞后。创建一个动作按钮,并从该按钮驱动第二个watchEvent表达式。
click