Shiny是一個(gè)R程序包,它有助于使用R代碼創(chuàng)建交互式Web應(yīng)用程序旁仿,該代碼可以在本地也可以在自己的服務(wù)器上托管藕夫。shiny可以從極其簡單到極其復(fù)雜。在其眾多用途中枯冈,Shiny是與他人進(jìn)行數(shù)據(jù)互動(dòng)的絕佳方法毅贮,本文通過一個(gè)示例先來簡單介紹一下Shiny
安裝Shiny軟件包
install.packages("shiny")
library(shiny)
Shiny附帶了一組內(nèi)置示例,可以使用runExample( )函數(shù)來查看示例
runExample("01_hello")
本示例是一個(gè)帶有滑塊的直方圖示例,用于控制箱的大小尘奏。該代碼也顯示在示例中
運(yùn)行下方代碼查看更多的示例
runExample("02_text")
runExample("03_reactivity")
runExample("04_mpg")
runExample("05_sliders")
runExample("06_tabsets")
runExample("07_widgets")
runExample("08_html")
runExample("09_upload")
runExample("11_timer")
在RStudio中轉(zhuǎn)到File -> New File -> Shiny Web App選擇一個(gè)文件Web應(yīng)用程序滩褥,將看到如下代碼
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for 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")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
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')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Shiny將其應(yīng)用程序的功能分為三個(gè)不同的部分:
第一部分
ui <- fluidPage()
第二部分
server <- function(input, output) {}
第三部分
shinyApp(ui = ui, server = server)
下面通過一個(gè)例子來展示
library(shiny)
library(shinyWidgets)
library(dslabs)
library(tidyverse)
library(plotly)
data("us_contagious_diseases")
disease <- us_contagious_diseases
disease <- mutate(disease, percapita = count/(population/100000)) %>%
pivot_longer(cols = c(count, percapita),
names_to = "data", values_to = "value")
ui <- fluidPage(
titlePanel("Diseases in the US 1928-2011"),
sidebarLayout(
sidebarPanel(
# inputs
# selectizeInput()所有狀態(tài)名稱創(chuàng)建一個(gè)下拉菜單
selectizeInput("stateInput", "State",
choices = unique(disease$state),
selected="Virginia", multiple =FALSE),
# checkboxGroupInput()來創(chuàng)建復(fù)選框
checkboxGroupInput("diseaseInput", "Disease",
choices = c("Hepatitis A",
"Measles",
"Mumps", "Pertussis",
"Polio", "Rubella",
"Smallpox"),
selected = c("Hepatitis A", "Polio")),
# sliderInput()創(chuàng)建可以滑動(dòng)的刻度條
sliderInput("yearInput", "Year", min=1928, max=2011,
value=c(1928, 2011), sep=""),
radioGroupButtons("dataInput", "Data",
choiceNames = list("Count", "Per capita"),
choiceValues = list("count", "percapita"))
),
mainPanel(
plotOutput("diseaseplot"),
br(), br(),
verbatimTextOutput("stats"),
br(), br(),
plotlyOutput("distplot")
)
)
)
server <- function(input, output) {
d <- reactive({
disease %>%
filter(state == input$stateInput,
disease %in% input$diseaseInput,
year >= input$yearInput[1],
year <= input$yearInput[2],
data == input$dataInput)
})
output$diseaseplot <- renderPlot({
ggplot(d(), aes(x=year, y = value, color=disease)) +
geom_line() +
theme_bw() +
xlab("Year") +
ylab(input$dataInput) +
ggtitle("Cases over time")
})
output$stats <- renderPrint({
aggregate(value ~ disease, data = d(), sum)
})
output$distplot <- renderPlotly({
box <- plot_ly(d(), y = ~value,
color = ~disease, type = "box") %>%
layout(title = "Distribution of cases over different years",
yaxis = list(title=input$dataInput))
})
}
shinyApp(ui=ui, server=server)
- renderPlot() 僅創(chuàng)建一個(gè)圖,而其他類型的輸出還有許多其他功能
- renderDataTable() 創(chuàng)建一個(gè)交互式表
- renderImage() 創(chuàng)建圖像
- renderText() 創(chuàng)建一個(gè)字符串
可以看到要完全掌握Shiny進(jìn)行交互式數(shù)據(jù)分析還是有一定難度的炫加,上面所列的11個(gè)示例數(shù)據(jù)希望對(duì)大家有所幫助