1. 程式人生 > >R語言圖形介面的shiny軟體包的方法

R語言圖形介面的shiny軟體包的方法

R和web的相關軟體包有rApache, Rhttpd, Rack, Rook。這些軟體包不僅需要開發者熟悉R,而且熟悉網頁程式語言(html,CSS,JS)。shiny是R語言的web介面軟體包,可在R程式設計環境下執行。shiny包集成了bootstrap、jquery、ajax等特性。

1.shiny軟體包應用

1.1  shiny軟體包資料

(1)datasets資料集

(2)讀資料

fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain'))

1.2 hello Shiny例程

>library(shiny)

>runExample("01_hello")

Listening on http://127.0.0.1:3839

##在新的web網頁中 Hello Shiny! Number of bins: 150003016111621263136414650 hello shiny 正態分佈直方圖                                      hello Shine          

1.3 兩個R語言程式

一個Shiny應用程式常用包含一個資料夾,是兩個基本程式序組成,使用者介面指令碼(a user-interface)和伺服器指令碼(a server),稱為ui.R檔案和server.R檔案。這兩個檔案儲存在使用者目錄下,在函式runApp中設定目錄名稱。
使用者端 ui.R 構建使用者介面 library(shiny) # Define UI for application that draws a histogram   定義user interface 直方圖的應用
shinyUI(fluidPage(      #構建使用者介面
  # Application title
  titlePanel("Hello Shiny!"),       #標題欄 
  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(                    #側邊欄
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    # Show a plot of the generated distribution
    mainPanel(                   #主面板
      plotOutput("distPlot")
    )
  )
))
server.R是伺服器端程式。
library(shiny) # Define server logic required to draw a histogram
shinyServer(function(input, output) {    #監聽,使用者行為
  # Expression that generates a histogram. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot
  output$distPlot <- renderPlot({    #伺服器根據使用者的輸入請求,決定處理的反應式程式設計renderPlot.
    x <- faithful[, 2]  # Old Faithful Geyser data
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')   #直方圖
  })
})
這兩個程式可修改引數,則直方圖的顏色和正態分佈隨機數不同。