renderHighcharts 与 renderUI;渲染多个图时哪一个更快

发布于 2025-01-17 04:37:41 字数 3293 浏览 4 评论 0原文

我正在构建一个应用程序,它有时会在网格布局中呈现 7-16 个 Highcharts。 这些 Highcharts 的渲染速度相当慢,我正在努力使其更快。

现在我正在使用 renderUI()、htmlOutput() 和 highcharter::hw_grid() 来渲染图表。 (参见代码块 1) 我发现了这个帖子,他们在那里尝试首先使用 highcharter::renderHighchart() 渲染图形,然后在 renderUI() 函数中使用 highcharter::higchartOutput() 来提高性能。我对代码进行了一些更改,以便它的输出与我的第一个版本中的相同,但想法应该保持不变。(参见代码块 2)

我现在已经在两个闪亮的应用程序中实现了两个版本并尝试评估他们与专业人士。我对 profvis 很陌生,我不知道在哪里可以准确地比较这两个选项。所以我刚刚查看了 RunApp() 花费的总时间。我使用 renderUI() 和 htmlOutput() 的第一个版本似乎比第二个版本更快。这与前面提到的帖子的结果相矛盾。

现在我想知道哪些选项更快以及为什么。我是否应该将代码从版本 1 更改为版本 2 以提高性能?

代码块 1


samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)

ui <- shinyUI(fluidPage(
    htmlOutput('plots')
))



server <- shinyServer(function(input, output) {
    

    
    output$plots <- renderUI({
        
        output=list()
        for (i in 1:50) {
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            
            output[[plotname]] <- 
                highchart() %>%
                hc_chart(type = "column") %>%
                hc_title(text = "renderHighchart()") %>%
                hc_xAxis(categories = seq_along(samp)) %>%
                hc_add_series(
                    data = samp,
                    name = "Downloads"
                )
        }
        hw_grid(output,ncol=3)
    })
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

代码块 2

samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)


ui <- shinyUI(fluidPage(
    uiOutput('plots')
))


server <- shinyServer(function(input, output) {
    
    n.col <- 3
    
    output$plots <- renderUI({
        col.width <- round(12/n.col) # Calculate bootstrap column width
        n.row <- ceiling(50/n.col) # calculate number of rows
        cnter <<- 0 # Counter variable
        
        # Create row with columns
        rows  <- lapply(1:n.row,function(row.num){
            cols  <- lapply(1:n.col, function(i) {
                cnter    <<- cnter + 1
                plotname <- paste("plot", cnter, sep="")
                column(col.width, highchartOutput(plotname))
            }) 
            fluidRow( do.call(tagList, cols) )
        })
        
        do.call(tagList, rows)
    })
    
    for (i in 1:50) {
        local({
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            output[[plotname]] <- renderHighchart({
                highchart() %>%
                    hc_chart(type = "column") %>%
                    hc_title(text = "renderHighchart()") %>%
                    hc_xAxis(categories = seq_along(samp)) %>%
                    hc_add_series(
                        data = samp,
                        name = "Downloads"
                    )
            })
        })
    }
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

I am building an App that at some point renders 7-16 Highcharts in a grid-layout.
The rendering of these Highcharts is quite slow and I am trying to make it faster.

Right now I am using renderUI(), htmlOutput() and highcharter::hw_grid() to render the graphs. ( See Code chunk 1)
I have come upon this Post where they tried to improve the performance by first using highcharter::renderHighchart() to render the Graphs and then using highcharter::higchartOutput() in renderUI() function. I have changed the code a bit so that it's output is the same as in my first Version, but the Idea should have stayed the same.(see Code chunk 2)

I have now Implemented both versions in two shiny-Apps and tried to evaluate them with profvis. I am very new to profvis and i am not sure where to look exactly to compare these two options. So I have just looked at the total time RunApp() takes. My first Version using renderUI() and htmlOutput() seemst to be faster than version two. This contradicts the results of the beforementioned Post.

Now I am wondering which options is faster and why. Should I change my code from Version 1 to version 2 to improve performance?

Code Chunk 1


samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)

ui <- shinyUI(fluidPage(
    htmlOutput('plots')
))



server <- shinyServer(function(input, output) {
    

    
    output$plots <- renderUI({
        
        output=list()
        for (i in 1:50) {
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            
            output[[plotname]] <- 
                highchart() %>%
                hc_chart(type = "column") %>%
                hc_title(text = "renderHighchart()") %>%
                hc_xAxis(categories = seq_along(samp)) %>%
                hc_add_series(
                    data = samp,
                    name = "Downloads"
                )
        }
        hw_grid(output,ncol=3)
    })
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

Code chunk 2

samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)


ui <- shinyUI(fluidPage(
    uiOutput('plots')
))


server <- shinyServer(function(input, output) {
    
    n.col <- 3
    
    output$plots <- renderUI({
        col.width <- round(12/n.col) # Calculate bootstrap column width
        n.row <- ceiling(50/n.col) # calculate number of rows
        cnter <<- 0 # Counter variable
        
        # Create row with columns
        rows  <- lapply(1:n.row,function(row.num){
            cols  <- lapply(1:n.col, function(i) {
                cnter    <<- cnter + 1
                plotname <- paste("plot", cnter, sep="")
                column(col.width, highchartOutput(plotname))
            }) 
            fluidRow( do.call(tagList, cols) )
        })
        
        do.call(tagList, rows)
    })
    
    for (i in 1:50) {
        local({
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            output[[plotname]] <- renderHighchart({
                highchart() %>%
                    hc_chart(type = "column") %>%
                    hc_title(text = "renderHighchart()") %>%
                    hc_xAxis(categories = seq_along(samp)) %>%
                    hc_add_series(
                        data = samp,
                        name = "Downloads"
                    )
            })
        })
    }
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼忆七猫命九 2025-01-24 04:37:41

我现在已将这两个选项放在一个应用程序中

samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)


ui <- shinyUI(
    fluidPage(
    fluidRow(uiOutput('plots')),
    fluidRow(htmlOutput('rUI')))
)


server <- shinyServer(function(input, output) {
    
    n.col <- 3
    
    output$plots <- renderUI({
        col.width <- round(12/n.col) # Calculate bootstrap column width
        n.row <- ceiling(50/n.col) # calculate number of rows
        cnter <<- 0 # Counter variable
        
        # Create row with columns
        rows  <- lapply(1:n.row,function(row.num){
            cols  <- lapply(1:n.col, function(i) {
                cnter    <<- cnter + 1
                plotname <- paste("plot", cnter, sep="")
                column(col.width, highchartOutput(plotname))
            }) 
            fluidRow( do.call(tagList, cols) )
        })
        
        do.call(tagList, rows)
    })
    
    for (i in 1:50) {
        local({
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            output[[plotname]] <- renderHighchart({
                highchart() %>%
                    hc_chart(type = "column") %>%
                    hc_title(text = "renderHighchart()") %>%
                    hc_xAxis(categories = seq_along(samp)) %>%
                    hc_add_series(
                        data = samp,
                        name = "Downloads"
                    )
            })
        })
    }
    
    output$rUI <- renderUI({
        
        output=list()
        for (i in 1:50) {
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            
            output[[plotname]] <- 
                highchart() %>%
                hc_chart(type = "column") %>%
                hc_title(text = "renderUI") %>%
                hc_xAxis(categories = seq_along(samp)) %>%
                hc_add_series(
                    data = samp,
                    name = "Downloads"
                )
        }
        hw_grid(output,ncol=3)
    })
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

如果我现在查看 profvis 中 output$plotsoutput$rUi 的聚合时间,我可以看到聚合时间对于 output$plots 确实更快

I have now put the two options in one app

samp <- sample(5000, 100, replace = TRUE)
library("highcharter")
library(shiny)


ui <- shinyUI(
    fluidPage(
    fluidRow(uiOutput('plots')),
    fluidRow(htmlOutput('rUI')))
)


server <- shinyServer(function(input, output) {
    
    n.col <- 3
    
    output$plots <- renderUI({
        col.width <- round(12/n.col) # Calculate bootstrap column width
        n.row <- ceiling(50/n.col) # calculate number of rows
        cnter <<- 0 # Counter variable
        
        # Create row with columns
        rows  <- lapply(1:n.row,function(row.num){
            cols  <- lapply(1:n.col, function(i) {
                cnter    <<- cnter + 1
                plotname <- paste("plot", cnter, sep="")
                column(col.width, highchartOutput(plotname))
            }) 
            fluidRow( do.call(tagList, cols) )
        })
        
        do.call(tagList, rows)
    })
    
    for (i in 1:50) {
        local({
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            output[[plotname]] <- renderHighchart({
                highchart() %>%
                    hc_chart(type = "column") %>%
                    hc_title(text = "renderHighchart()") %>%
                    hc_xAxis(categories = seq_along(samp)) %>%
                    hc_add_series(
                        data = samp,
                        name = "Downloads"
                    )
            })
        })
    }
    
    output$rUI <- renderUI({
        
        output=list()
        for (i in 1:50) {
            n <- i # Make local variable
            plotname <- paste("plot", n , sep="")
            
            output[[plotname]] <- 
                highchart() %>%
                hc_chart(type = "column") %>%
                hc_title(text = "renderUI") %>%
                hc_xAxis(categories = seq_along(samp)) %>%
                hc_add_series(
                    data = samp,
                    name = "Downloads"
                )
        }
        hw_grid(output,ncol=3)
    })
})

app=shinyApp(ui=ui,server=server)

profvis::profvis(runApp(app))

If I now look at the aggregated time of output$plots and output$rUi in profvis, I can see that the aggregated time for output$plots is indeed faster

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文