賞金明天到期。_ 此問題的答案有資格獲得 250聲望賞金。 勞拉正在尋找一個規范的答案。
您好,下面的圖表是我儀表板的一部分,但它使儀表板性能不佳。
更新需要時間。
htmlOutput()除了功能 之外,還有其他選擇renderUI()嗎?
我怎樣才能提高性能?在服務器外部創建和生成圖表是這樣的嗎?
library(shiny)
library(highcharter)
library(tidyverse)
df <-tibble(months = month.abb, value = ts(cumsum(rnorm(100)))[1:12] )
ui <- fluidPage(
h1("Highcharts"),
htmlOutput('chart_grid')
)
server <- function(input, output, session) {
output$chart_grid<- renderUI({
charts <- lapply(1:9, function(x) {
highchart() %>%
hc_add_series(type = 'spline',data = df, hcaes(x = months,y = value))%>%
hc_xAxis(categories = df$months)
})
hw_grid(charts, rowheight = 300,add_htmlgrid_css = TRUE)%>%
htmltools::browsable()
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
TL;DRhighcharts用于許多資料點的 JS 提升模塊,如果有一個或幾個繪圖版本,用于繪圖的靜態 HTML,自己的網格和 highcharter::highchartOutput
靜態HTML
策略是在閃亮的應用程式啟動之前創建所需的圖,但我們必須假設該圖有一個或幾個版本,因為大量的 html 集合也可能是一個錯誤的方向。通常我們htmlwidgets::saveWidget用來保存任何閃亮的小部件,以便我們可以獲得它的 html 表示。由于hw_grid沒有回傳閃亮的小部件,我將其保存為常規 html,但我們必須處理依賴關系。
在這里,我使用hchart(not highchart) 因為它會在繪圖上保留標簽。
您不必在 app.R 檔案中留下用于創建 html 的代碼。但在我的示例中,您仍然需要保留依賴項串列。然后你可以在 DOM 頭中添加依賴項htmltools::renderDependencies(htmltools::resolveDependencies(DEPS))。請記住,當您部署閃亮的應用程式時,您必須添加靜態檔案,例如 addResourcePath https://shiny.rstudio.com/reference/shiny/1.0.2/addResourcePath.html
library(shiny)
library(highcharter)
library(tidyverse)
library(magrittr)
df <- tibble::tibble(months = month.abb, value = ts(cumsum(rnorm(100)))[1:12])
charts <- lapply(1:9, function(x) {
hchart(df, type = "spline", hcaes(x = months, y = value)) %>%
hc_xAxis(categories = df$months) %>%
hc_boost(enabled = TRUE)
})
hc_test <- hw_grid(charts, rowheight = 300, add_htmlgrid_css = TRUE, browsable = TRUE)
# get HTML of the plot
# we could not use htmlwidgets::saveWidget as it is not a widget
writeLines(as.character(hc_test), "hc_test.html")
# get the dependencies of the plot
hc_deps <- htmltools::findDependencies(hc_test)
# unique dependencies in the HTML format, could be added in the head
# htmltools::renderDependencies(htmltools::resolveDependencies(hc_deps))
ui <- fluidPage(
h1("Highcharts"),
htmlOutput("chart_grid")
)
server <- function(input, output, session) {
output$chart_grid <- renderUI({
# Load HTML with proper dependencies
htmltools::attachDependencies(
shiny::HTML(readLines("hc_test.html")),
htmltools::resolveDependencies(hc_deps)
)
})
}
shinyApp(ui, server)
升壓模塊
源highchartsJS 庫提供了一個可以提高性能的提升模塊。從 R 的角度來看,它就像添加hc_boost(enabled = TRUE)到您的管道一樣簡單。
https://www.highcharts.com/docs/advanced-chart-features/boost-module
據我了解,hc_boost只有在特定情況下才能提高性能,并以失去某些功能為代價。我沒有測驗它是否真的按預期作業。
library(shiny)
library(highcharter)
library(tidyverse)
df <- tibble(months = month.abb, value = ts(cumsum(rnorm(100)))[1:12])
ui <- fluidPage(
h1("Highcharts"),
htmlOutput("chart_grid")
)
server <- function(input, output, session) {
output$chart_grid <- renderUI({
charts <- lapply(1:9, function(x) {
highchart() %>%
hc_add_series(type = "spline", data = df, hcaes(x = months, y = value)) %>%
hc_xAxis(categories = df$months) %>%
hc_boost(enabled = TRUE)
})
hw_grid(charts, rowheight = 300, add_htmlgrid_css = TRUE, browsable = TRUE)
})
}
shinyApp(ui, server)
Highcharts 和自己的網格
library(shiny)
library(highcharter)
library(tidyverse)
df <- tibble(months = month.abb, value = ts(cumsum(rnorm(100)))[1:12])
ui <- fluidPage(
h1("Highcharts"),
shiny::tags$div(
style = "display:flex;flex-wrap: wrap;",
lapply(1:9, function(x) shiny::tags$div(
style = "flex: 1 1 30%;",
highcharter::highchartOutput(sprintf("hplot%s", x)))
)
)
)
server <- function(input, output, session) {
charts <- lapply(1:9, function(x) {
output[[sprintf("hplot%s", x)]] <- highcharter::renderHighchart(
highchart(width = 600) %>%
hc_add_series(type = "spline", data = df, hcaes(x = months, y = value)) %>%
hc_xAxis(categories = df$months)
)
})
}
shinyApp(ui, server)
順便提一句。
我個人認為 highcharter 包在代碼質量和檔案方面需要做很多作業,例如hw_grid甚至沒有記錄回傳值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535761.html
標籤:r表现闪亮的
上一篇:MySQL性能-交叉連接與左連接
