Re: [分享] R shiny web app 一些經驗雜談

看板R_Language作者 (cywhale)時間7年前 (2018/04/13 22:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
最近嘗試R shiny async版本 (+ promises) 配合future的實現方式,很簡單的原則是把 long-run R functions 抽出來,例如 fx <- reactive({ future( foo() ) }) 在future()當中不要再放其他 shiny reactive controls 最後在 observeEvent(event, { fx() %...>% ({do something 或其他 side effects}) }) 簡單的demo可看作者 Joe Cheng的presentation,相當清楚 https://goo.gl/qPRrNG 詳細的教學可以看官網 https://rstudio.github.io/promises/index.html 官網中介紹的promise_all,我簡單做了可以抓取不同地方資料庫的應用,概念像是: f1 <- reactive({ future( fetchDB1() ) }) f2 <- reactive({ future( fetchDB2() ) }) observeEvent(event, { df1 <- f1() %...>% ({filtering...}) }) df2 <- f2() %...>% ({filtering...}) }) promise_all(df1, df2) %...>% { rbindlist(l=., fill=TRUE, use.names=TRUE) do some controls or side effects here... } }) 抓取DB1, DB2 等待的時間不盡相同,這樣的好處就是當某一個client user在 shiny app 做這件事時,shiny-server可以拿回控制權,其他client users 仍可正常使用這個shiny app 剛好 future 釋出1.8版,作者在blog上介紹了這一版與配合promises的修改 https://www.jottr.org/2018/04/12/future-results/ 但 R shiny async 還在development階段,我安裝在ubuntu沒有問題,但在windows安裝 遇到一些問題,還沒解決。至少 ubuntu上跑是沒有問題的,現階段也就這樣用。 有興趣可以試試看 devtools::install_github("rstudio/shiny") 有些若shiny app中有使用的套件也需要 async-compatible branches devtools::install_github("ramnathv/htmlwidgets@async") devtools::install_github("jcheng5/plotly@joe/feature/async") devtools::install_github("rstudio/DT@async") 簡單分享於此~ 比較詳細的細節可能要參考官網說明了! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.152.45 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1523630355.A.B4F.html
文章代碼(AID): #1QqC4JjF (R_Language)
文章代碼(AID): #1QqC4JjF (R_Language)