[問題] Shiny可以在運算未完成時於UI提示嗎?
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
使用者(已經有用R 做過不少作品)
[問題敘述]:
我最近做了一隻Shiny程式,但它需要的運算耗時比較久,
(而且在shinyapps.io上花的時間比我在本地長很多...)
好奇有沒有可能在UI裡面加一行提示(例如「運算中,請稍候」之類的),
而且只在結果還沒出來時才有那一行?
我知道也許長期目標是讓我的程式碼更有效率一點,
但目前我還想不到改寫的方式。 orz
[程式範例]:
ui <- fluidPage(
# Other UI elements
conditionalPanel(condition = "output.done == 'FALSE'",
helpText("運算中,請稍候"))
)
server <- function(input, output) {
output$done <- reactive({"FALSE"})
# Complicated processes
output$done <- reactive({"TRUE"})
outputOptions(output, "done", suspendWhenHidden = FALSE)
}
[關鍵字]:
shiny, dynamic UI
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.58.15
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1503326582.A.2EC.html
→
08/22 00:28, , 1F
08/22 00:28, 1F
→
08/22 00:29, , 2F
08/22 00:29, 2F
→
08/22 00:31, , 3F
08/22 00:31, 3F
→
08/22 00:31, , 4F
08/22 00:31, 4F
→
08/22 00:31, , 5F
08/22 00:31, 5F
→
08/22 01:51, , 6F
08/22 01:51, 6F
推
08/22 09:15, , 7F
08/22 09:15, 7F
→
08/22 10:37, , 8F
08/22 10:37, 8F
※ 編輯: clsmbstu (140.112.25.106), 08/22/2017 11:13:43
→
08/22 15:35, , 9F
08/22 15:35, 9F
→
08/22 15:35, , 10F
08/22 15:35, 10F
→
08/22 15:37, , 11F
08/22 15:37, 11F
→
08/22 15:38, , 12F
08/22 15:38, 12F
推
08/23 01:05, , 13F
08/23 01:05, 13F
→
08/23 01:06, , 14F
08/23 01:06, 14F
我是參考你附的連結的寫法,
(本來想把每個server outputs都包在一個withProgress一起讀進度,但邏輯好像不對)
以下是我的server function一部分的簡化(省去根據UI的一些條件判斷式):
output$mapplot <- renderLeaflet({
# Define palettes
pal <- colorNumeric("YlOrRd", merge_map()$Values)
labs <- sprintf(
"<strong>%s</strong><br>%g%%", merge_map()$CountyTown, merge_map()$Values
) %>%
lapply(HTML)
# Map plotting
leaflet(merge_map()) %>%
setView(121, 23.5, 7) %>%
addTiles() %>%
addPolygons(weight = 2, color = "white", dashArray = 3,
fillColor = ~pal(Values), fillOpacity = 0.8,
highlightOptions = highlightOptions(
weight = 5, color = "#636363", bringToFront = TRUE
),
label = labs,
labelOptions = labelOptions(
textsize = "15px",
style = list("font-weight" = "normal")
)) %>%
addLegend(position = "bottomright",
pal = pal, values = ~Values, opacity = 0.8,
title = "Percentages",
labFormat = labelFormat(suffix = "%"))
})
為了加進度條,變成:
output$mapplot <- renderLeaflet({
withProgress(
value = 0, message = "地圖繪製中", detail = "製作說明標籤"
expr = {
# Define palettes
pal <- colorNumeric("YlOrRd", merge_map()$Values)
labs <- sprintf(
"<strong>%s</strong><br>%g%%", merge_map()$CountyTown, merge_map()$Values
) %>%
lapply(HTML)
incProgress(0.1, detail = "套疊地圖圖層")
# Map plotting
final_map <- leaflet(merge_map()) %>%
setView(121, 23.5, 7) %>%
addTiles()
incProgress(0.2, detail = "繪製各區域資料")
final_map <- addPolygons(final_map, weight = 2, color = "white",
dashArray = 3,
fillColor = ~pal(Values), fillOpacity = 0.8,
highlightOptions = highlightOptions(
weight = 5, color = "#636363", bringToFront = TRUE
),
label = labs,
labelOptions = labelOptions(
textsize = "15px",
style = list("font-weight" = "normal")
))
incProgress(0.5, detail = "製作圖例")
final_map <- addLegend(final_map, position = "bottomright",
pal = pal, values = ~Values, opacity = 0.8,
title = "Percentages",
labFormat = labelFormat(suffix = "%"))
incProgress(0.2, detail = "即將完成")
}
)
final_map
})
先不提每次的進度前進多少其實是我的主觀認定這件事,
要把原本的pipeline切開才能更新進度條也是有點麻煩。
最後更奇怪的是,進度條還是幾乎瞬間跑完,但地圖並沒有馬上出來...
我懷疑要把物件呈現上來本身也很花時間?
但是在這裡的倒數第二行"final_map"似乎不能包進withProgress裡面,
否則根本不會有地圖出來。
這樣提供的資訊夠嗎?
※ 編輯: clsmbstu (140.112.121.113), 08/23/2017 17:00:25
推
08/23 22:04, , 15F
08/23 22:04, 15F
→
08/23 22:08, , 16F
08/23 22:08, 16F
→
08/23 22:09, , 17F
08/23 22:09, 17F
→
08/23 22:30, , 18F
08/23 22:30, 18F
→
08/23 22:31, , 19F
08/23 22:31, 19F
→
08/23 22:32, , 20F
08/23 22:32, 20F
推
08/23 23:14, , 21F
08/23 23:14, 21F
→
08/23 23:14, , 22F
08/23 23:14, 22F
→
08/23 23:40, , 23F
08/23 23:40, 23F
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章