构建Shiny应用

构建Shiny应用

1.什么是Shiny?

Shiny是一个R的应用包,帮助用户构建可交互的web应用。它可以结合HTML和CSS代码,以及R 语言的运算能力。

2.下载R Shiny

下载R包

install.packages("shiny")

加载R包

library(shiny)

3.Shiny应用结构

Shiny的结构:

在里面输入inputIdoutputId

ui

titlePanel and sidebarLayoutare the two most popular elements to add fluidPage

library(shiny)

shinyUI(pageWithSidebar)(
    titlePanel("Mile Per Gallon"),
    
    
    sidebarLayout(
    
        sidebarPanel(),
        
        mainPanel()
    )
    
)

server

shinyServer(function(input,output){

})


shinyApp(ui=ui,server = server)

4.构建一个简单应用:

ui文件用于控制应用的外观

ui<-fluidPage(
    titlePanel("Hello"),
    
    sidebarPanel(
    
        sliderInput(inputId="bins",label="Number of bins",
        min = 1,
        max = 50,
        value =30
        )
    ),
    
    mainPanel(
        plotOutput(outputId="distPlot")
    )

)

接下来

server<-function(input,output){

    output$distPlot<-renderPlot({
        x<-faithful$waiting
        
        bins<-seq(min(x),max(x),length.out=input$bins+1)
        
       hist(x, breaks = bins, col = "#75AADB", border = "orange",
      xlab = "Waiting time to next eruption (in mins)",
      main = "Histogram of waiting times")
    
    })
    
}

5.运行结果:

猜你喜欢

转载自www.cnblogs.com/zhichun/p/11966733.html
今日推荐