初识ShinyApp
ShinyApp用R语言构建的交互式网页APP,将底层的代码包装成交互的界面,方便没有R语言基础的小白来用交互式的网页APP来处理自己的数据 下面示例的代码主要由两部分组成,ui和server。第一天主要认识Shiny的文件结构,ui储存了交互式页面的代码,server里面是处理数据的底层代码。复制下面代码到Rstudio中直接运行,创建一个通过滑动滑块改变直方图中矩形的个数是交互式网页工具
##安装shiny包 install.packages("shiny") ##加载包 library(shiny) # Define UI for app that draws a histogram ---- ui <- fluidPage( # App title ----为界面加上标题 titlePanel("Hello world!"), # Sidebar layout with input and output definitions ----加入一个工具条 sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( # Input: Slider for the number of bins ---- sliderInput(inputId = "bins", label = "Number of bins:", min = 5, max = 50, value = 30) ), # Main panel for displaying outputs ---- mainPanel( # Output: Histogram ---- plotOutput(outputId = "distPlot") ) ) ) # Define server logic required to draw a histogram ---- server <- function(input, output) { # Histogram of the Old Faithful Geyser Data ---- # with requested number of bins # This expression that generates a histogram is wrapped in a call # to renderPlot to indicate that: # # 1. It is "reactive" and therefore should be automatically # re-executed when inputs (input$bins) change # 2. Its output type is a plot output$distPlot <- renderPlot({ x <- faithful$waiting bins <- seq(min(x), max(x), length.out = input$bins + 1) hist(x, breaks = bins, col = "#75AADB", border = "yellow", xlab = "Waiting time to next eruption (in mins)", main = "Histogram of waiting times") }) } # Create Shiny app ---- ##运行ShinyApp shinyApp(ui = ui, server = server)
image-20210502203542958
ShinyApp文件结构
- 单文件结构:将ui和server写到了一个R文件里
- 双文件结构:将ui和server拆分到两个R文件里
Rsudio常用快捷键
- 显示所有的快捷键:alt+shift+k
- 光标移动到代码界面:ctrl+1
- 光标移动到控制面板界面:ctrl+2
- 打开终端命令行:alt+shift+twin10系统下开启linux子系统的话,可以alt+shift+t打开终端的命令行
【参考资料】
1.https://mp.weixin.qq.com/s/iyl_duzfCQmvD8ywlpfyiA