作者cywhale (cywhale)
看板R_Language
标题Re: [分享] R shiny web app 一些经验杂谈
时间Fri Apr 13 22:39:13 2018
最近尝试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://webptt.com/cn.aspx?n=bbs/R_Language/M.1523630355.A.B4F.html