ShinyApp网页工具:第一天笔记

初识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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许超Steven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值