Shiny 初步了解

想了解Shiny app運行原理,可以查看先前的文章一個 Shiny app的基本組成部分


Shiny app框架

簡單來說,app.R的框架如下:

library(shiny)

# See above for the definitions of ui and server
ui <- ...

server <- ...

shinyApp(ui = ui, server = server)
  • 首先需要調用shiny
  • 編寫ui和server
  • 使用shinyApp()運行

Shiny app運行和調試

一個app最好存放在一個單獨的文件夾,這樣可以方便地調用;比如這兒我們創建一個文件夾為appname/, 然后將編寫的app.R拷貝至文件夾appname/,在R studio端使用runApp運行

library(shiny)
runApp("app文件夾名")

runApp來運行例子程序, 這個函數會啟動shiny程序并打開瀏覽器以便查看程序。
在shiny程序運行的過程中,R控制臺的交互將被阻斷,也就是說,不能在控制臺運行命令。
要停止shiny程序,你只需中斷R,有兩種方式:(1)在R的任何前端里按下Escape鍵;(2)點擊R環境里提供的停止按鈕。

在獨立進程里運行

如果你不想在運行shiny程序的時候阻斷訪問控制臺,則可以在單獨的進程中運行shiny程序。你可以打開終端窗口或者控制臺窗口,然后執行下面的命令:

R -e "shiny::runApp('./shinyapp')"

Shiny app 例子

安裝的Shiny包中打包了一些例子可以直接運行

runExample("01_hello")      # a histogram
runExample("02_text")       # tables and data frames
runExample("03_reactivity") # a reactive expression
runExample("04_mpg")        # global variables
runExample("05_sliders")    # slider bars
runExample("06_tabsets")    # tabbed panels
runExample("07_widgets")    # help text and submit buttons
runExample("08_html")       # Shiny app built from HTML
runExample("09_upload")     # file upload wizard
runExample("10_download")   # file download wizard
runExample("11_timer")      # an automated timer

開始的時候,大家可以參考這些例子進行修改,編寫一些簡單的Shiny app;在[Github shiny-examples])(https://github.com/rstudio/shiny-examples)還有上百個例子,大家可以學習;

Gallery中有發表的一些優秀app,大家也可以看看源碼。

參考:

Welcome to Shiny

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。