【问题标题】:Parallel processing in shiny app闪亮应用程序中的并行处理
【发布时间】:2018-03-28 03:41:18
【问题描述】:

我有一个闪亮的应用程序,它可以生成带有美国各县具体特征信息的绘图地图。该应用程序运行良好,但每次我打电话时都需要 40 秒才能创建地图。我使用带有 if 和 else if 的反应式表达式 (rt2) 来进行预处理 有没有办法进行某种并行处理或类似的处理以节省时间?

数据集示例

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

ui.r

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

server.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"))
              })
            }

【问题讨论】:

  • 我试图用你的示例数据集复制一个更大的数据集,但这并不容易。在我的环境中它非常快,但是没有出现一些图,并且 fill = cut_number(rt2()[,7],input$selectsol11) 被删除,因为它产生了错误。此外,有些事情很奇怪,例如:为什么您的sliderInput 中有您的server 并且具有相同的ID?
  • 该示例仅适用于我无法提供的特定数据集。我的服务器中有滑块,因为我想要它的 uioutput。这个作品不用担心。我只是想知道如何让它更快地工作
  • 您应该提供一个可重现的最小示例,而不仅仅是复制和粘贴您的所有代码。

标签: r ggplot2 parallel-processing shiny plotly


【解决方案1】:

由于我无法重现该示例,因此我无法知道您的应用程序需要什么时间。因此,我建议您在运行应用程序时进行一些分析:Rstudio 中有一个直接可用的工具-

->“开始分析”然后运行你的应用程序

-> 应用加载后“停止分析”。

从中,您将能够看到花费最多时间(或内存)的部分是什么。 根据您所展示的内容,我建议 foo1 &lt;- map_data("county") 应该花费最多的计算时间。因此,如果是这种情况,您应该了解如何加速此功能或找到另一个功能。对于map_data(),我认为与实现并行处理无关。

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2016-02-09
    • 2018-12-05
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2016-06-02
    相关资源
    最近更新 更多