我有一个闪亮的应用程序,可以生成带有美国各县特定特征信息的绘图地图。该应用程序运行良好,但每次调用地图时都需要 40 秒才能创建地图。我使用带有if的反应表达式(rt2)和elseif进行预处理有没有办法进行某种并行处理或类似的东西来节省时间?
数据集示例
County_ID County_Name EQI air_EQI water_EQI land_EQI sociod_EQI
<int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1001 Autauga, AL 0.0041379 0.9553846 -1.1097280 -0.7065906 0.6704357
2 1003 Baldwin, AL 0.2002343 0.7179643 -0.5659107 -1.0842990 0.5530728
3 1005 Barbour, AL -0.9506388 0.1310074 -0.9780902 -1.2814700 -1.2362940
4 1007 Bibb, AL -1.0889200 0.0652890 -0.9681726 -0.8274103 -0.6000178
5 1009 Blount, AL -0.5139221 0.4021944 -0.7186447 -0.6229339 0.2965088
6 1011 Bullock, AL -2.0828290 -0.3091858 -1.4513350 -1.2580140 -1.8239700
UIR
library(plotly)
library(shiny)
library(tidyr)
library(maps)
library(shinydashboard)
library(viridis)
library(ggplot2)
ui <- navbarPage(
title="Computational Modelling and Investigation of Neuropsychiatric Disease (MIND)",
tabPanel("Home Tab",
sidebarLayout(
sidebarPanel(
uiOutput("selectcol11"),
uiOutput("selectsol11")
),
mainPanel(plotlyOutput("plot2")))))
服务器.r
server <- function(input, output) {
output$selectcol11<-renderUI({
selectInput("selectcol11","Select Measure",choices = c("EQI","air_EQI","water_EQI",
"land_EQI","sociod_EQI","built_EQI","good_days","bad_days" ),selected = "EQI")
})
output$selectsol11<- renderUI({
sliderInput("selectsol11", "Select Resolution",
min = 2, max = 7, value = 5,step =1)})
rt2 <- reactive({
if(input$selectcol11== "EQI"){
foo <- cbind(data.frame(do.call('rbind', strsplit(as.character(df$County_Name),',',fixed=TRUE))),df[,c(1,3:10)])
colnames(foo)[1] <- "County"
colnames(foo)[2] <- "State"
foo1 <- map_data("county")
eqi1 <- foo %>%
group_by(County) %>%
summarise(EQI = sum(EQI))
eqi10 <- foo %>%
group_by(County) %>%
summarise(County_ID = sum(County_ID))
eqi<- cbind(eqi1,eqi10[,2])
eqi$County <- tolower(eqi$County) # matching string
foo1_eqi <- merge(foo1, eqi, by.x = "subregion", by.y = "County")
foo1_eqi
final.plot1<-foo1_eqi[order(foo1_eqi$order), ]
final.plot1$County_Name_ID <- with(final.plot1, paste("County:",subregion, '<br>', "County_ID:", County_ID ))
final.plot1
}
else if(input$selectcol11== "air_EQI"){
foo <- cbind(data.frame(do.call('rbind', strsplit(as.character(df$County_Name),',',fixed=TRUE))),df[,c(1,3:10)])
colnames(foo)[1] <- "County"
colnames(foo)[2] <- "State"
foo1 <- map_data("county")
eqi1 <- foo %>%
group_by(County) %>%
summarise(air_EQI = sum(air_EQI))
eqi10 <- foo %>%
group_by(County) %>%
summarise(County_ID = sum(County_ID))
eqi<- cbind(eqi1,eqi10[,2])
eqi$County <- tolower(eqi$County) # matching string
foo1_eqi <- merge(foo1, eqi, by.x = "subregion", by.y = "County")
foo1_eqi
final.plot1<-foo1_eqi[order(foo1_eqi$order), ]
final.plot1$County_Name_ID <- with(final.plot1, paste("County:",subregion, '<br>', "County_ID:", County_ID ))
final.plot1
}
})
output$plot2 <- renderPlotly ({
p<- ggplot(rt2(), aes(x = long, y = lat, group=group,text=County_Name_ID,fill = cut_number(rt2()[,7],input$selectsol11)))
p<- p+ geom_polygon(colour=alpha("black", 1/2))
p<- p+coord_equal()
p<- p+ viridis::scale_fill_viridis(discrete = TRUE)
p<- p+ scale_fill_discrete("Measure Density")
ggplotly(p, tooltip = c("County_Name_ID"))
})
}
由于我无法重现该示例,因此我无法知道您的应用程序中需要什么时间。所以我建议你在运行应用程序时做一些分析:Rstudio中有一个直接可用的工具 -
->"开始分析",然后运行您的应用程序
-> 加载应用程序后"停止分析"。
由此,您将能够看到花费最多时间(或内存)的部分是什么。根据您展示的内容,我建议这是应该花费最多计算时间的foo1 <- map_data("county")
。因此,如果是这种情况,您应该了解如何加速此功能或找到另一个功能。对于map_data()
,我认为实现并行处理无关紧要。