R之Shiny學習筆記


官方教程:https://shiny.rstudio.com/tutorial/

中文教程:http://yanping.me/shiny-tutorial/

英文教程:https://deanattali.com/blog/building-shiny-apps-tutorial/

Shiny是一個R包,使用它可以很容易構建交互式web應用程序。

1. 入門

Hello Shiny是個簡單的應用程序, 這個程序可以生成正態分布的隨機數,隨機數個數可以由用戶定義,並且繪制這些隨機數的直方圖。

library(shiny)
runExample("01_hello")

Shiny Text這個應用程序展示的是直接打印R對象,以及用HTML表格展示數據框。

更多示例:

"01_hello", "02_text", "03_reactivity", "04_mpg", "05_sliders", "06_tabsets", "07_widgets", "08_html", "09_upload", "10_download", "11_timer"

分別向我們演示了:

示例 輸入形式 輸出形式
01_hello      # a histogram 滑動條輸入(sliderInput) 圖形輸出(plotOutput)
02_text       # tables and data frames 選擇輸入(selectInput) 表格輸出(tableOutput)
03_reactivity# a reactive expression 文本輸入(textInput),數字輸入(numericInput) 反應式(標題h3)
04_mpg       # global variables 復選框輸入(checkboxInput)  
05_sliders    # slider bars

滑動條輸入

(數值類型、范圍、步長、雙取值、符號標示、動畫)

 
06_tabsets    # tabbed panels 單選按鈕(radioButtons) 標簽頁(tabsetPanel)
07_widgets    # help text and submit buttons 幫助信息(helpText),動作按鈕(actionButton) verbatimTextOutput
08_html        # Shiny app built from HTML HTML樣式  
09_upload     # file upload wizard 文件輸入(fileInput)  
10_download # file download wizard   文件輸出(downloadButton)
11_timer       # an automated timer   時間輸出

在shiny中使用反應值時,最常見的方式是使用input對象。input對象會被傳遞給shinyServer函數中,讓我們可以用類似列表的語法來訪問網頁上的輸入值。為了將反應值轉化為可以在網頁上呈現的輸出,我們要將它們賦值給output對象(同樣傳遞給shinyServer函數)。

input values => R code => output values

創建:

2. 運行&調試

服務端腳本給兩個輸出賦值:output$captionoutput$mpgPlot。為了讓用戶接口能顯示輸出,我們需要在主UI面板上添加相應的元素。

打印

cat("foo\n")
cat("bar\n", file=stderr())

調試瀏覽器

# Always stop execution here
browser() 
# Stop execution when the user selects "am"
browser(expr = identical(input$variable, "am"))

錯誤處理器

# Immediately enter the browser when an error occurs
options(error = browser)
# Call the recover function when an error occurs
options(error = recover)

3. HTML元素

shiny function HTML5 equivalent creates
p <p> 段落
h1 <h1> 一級標題
h2 <h2> 二級標題
h3 <h3> 三級標題
h4 <h4> 四級標題
h5 <h5> 五級標題
h6 <h6> 六級標題
a <a> 鏈接
br <br> 換行
div <div> 容器
span <span> 內聯文本
pre <pre> 格式字體
code <code> 代碼塊
img <img> 圖片
strong <strong> 粗體
em <em> 斜體
HTML   直接將字符串作為HTML代碼傳遞

使用HTML定義前端不需要ui.R文件,由index.html文件定義即可。

4. 輸入

function widget
actionButton Action Button
checkboxGroupInput A group of check boxes
checkboxInput A single check box
dateInput A calendar to aid date selection
dateRangeInput A pair of calendars for selecting a date range
fileInput A file upload control wizard
helpText Help text that can be added to an input form
numericInput A field to enter numbers
radioButtons A set of radio buttons
selectInput A box with choices to select from
sliderInput A slider bar
submitButton A submit button
textInput A field to enter text

5. 輸出

Output function Creates
dataTableOutput DataTable
htmlOutput raw HTML
imageOutput image
plotOutput plot
tableOutput table
textOutput text
uiOutput raw HTML
verbatimTextOutput text

6. 分享

  1. 通過R腳本;
  2. 通過網頁。

7. 練習

博客:https://www.cnblogs.com/dingdangsunny/p/12586274.html

日期計算器:https://dingdangsunny.shinyapps.io/DateCalculate

FFT分析:https://dingdangsunny.shinyapps.io/FastFourierTransform


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM