内容简介:以下是最近整理的Shiny小程序的一些笔记曾经在写Shiny小程序的时候,需要包括2个文件:如果是在写代码的时候测试的话,出了用
以下是最近整理的Shiny小程序的一些笔记
App formats and launching apps
曾经在写Shiny小程序的时候,需要包括2个文件: ui.R
和 server.R
,里面分别包含了两个函数: shinyUI()
和 shinyServer()
;但是现在的Shiny程序并不需要这样形式了,只需要一个 app.R
,格式跟我们平时在RStduio中写的shiny程序一样,由三部分组成,如下:
## app.R ## ui <- fluidPage( ...... ) server <- function(input, output) { ...... } shinyApp(ui = ui, server = server)
如果是在写代码的时候测试的话,出了用 shinyApp
外,还可以用
app <- shinyApp(ui, server) runApp(app)
或者如果myapp文件夹下有shiny的小程序话,可以这样调用
runApp("myapp")
Custom Function
在写shiny的server部分时,不可避免会遇到一些自定义函数,这时可以选择如下:
func <- function(x){x + 1} server <- function(input, output){ res <- reactive({ func(as.numeric(input$n)) }) output$text <- renderText({res()}) }
除了上述方式外,可以用将自定义函数直接写在 reactive({})
中,然后再计算输出,如:
server <- function(input, output){ res <- reactive({ as.numeric(input$n) + 1 }) output$text <- renderText({res()}) }
Stop reactions with isolate()
最基本的shiny程序是立即将observer传递给reactive expression,但是有时我们希望其是可控的,比如跟 actionButton
搭配,当点击actionButton才让reactive执行,这时可以考虑用 isolate()
了
使用 isolate()
来隔离reactive expression使其在没有点击actionButton前无法接受observer值,如例子如下:
server <- function(input, output) { output$distPlot <- renderPlot({ # Take a dependency on input$goButton input$goButton # Use isolate() to avoid dependency on input$obs dist <- isolate(rnorm(input$obs)) hist(dist) }) }
如果你不想在页面一开始就输出表格/图片等信息,可以用 input$goButton == 0
的if语句来控制(这种方法很好用的),并且 isolate({})
还可以用于控制reactive expressions,如下的 fib()
函数
output$nthValue <- renderText({ if (input$goButton == 0) return() isolate({ fib(as.numeric(input$n)) }) })
Progress indicators
如果shiny程序将要执行一个优点耗时的计算过程时,可以考虑增加一个progress bar来告诉使用者shiny正在计算ing,蛮实用的一个工具
我之前是这样写的,如下:
server <- function(input, output){ data <- reactive({ data.frame(x=rnorm(10), y=rnorm(10)) }) output$table <- renderTable({ input$goTable withProgress(message = "Try it:", value = 0, { n <- nrow(data()) for (i in 1:n) { incProgress(1/n, detail = "Please wait...") Sys.sleep(0.25) } }) data() }) }
这次看了其教程后发现还有一些功能更为好使的写法(更加简洁和便于理解):
server <- function(input, output){ data <- reactive({ data.frame(x=rnorm(10), y=rnorm(10)) }) output$table <- renderTable({ input$goTable # Create a Progress object progress <- shiny::Progress$new() # Make sure it closes when we exit this reactive, even if there's an error on.exit(progress$close()) progress$set(message = "Try it:", value = 0) n <- nrow(data()) for (i in 1:n) { progress$inc(1/n, detail = "Please wait...") Sys.sleep(0.2) } data() }) }
Render images in a Shiny app
如果想展示一些经过R作图后产生的图片,那么在shiny中是要用 renderPlot()
即可,这也是很常见的用法;但是如果想直接将一张图片传到shiny网页上,则需要考虑用其他方法了
我之前用 renderPlot()
后发现有问题,这次才知道要用 renderImage
才行,如:
output$image <- renderImage({ if (is.null(input$picture)) return(NULL) if (input$picture == "face") { return(list( src = "images/face.png", contentType = "image/png", alt = "Face" )) } }, deleteFile = FALSE)
未完待续。。还有一些继续整理下
本文出自于 http://www.bioinfo-scrounger.com 转载请注明出处
以上所述就是小编给大家介绍的《Shiny用法整理(一)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- shiny用法整理(二)
- shiny用法整理(三)
- Pandas数据可视化工具——Seaborn用法整理
- 照片整理系列二 —— 照片整理及归档的辛酸历程
- 我自己整理的码农周刊一周分享整理
- 【复习资料】ES6/ES7/ES8/ES9资料整理(个人整理)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Development Recipes
Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00
You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!