# Define UI for dataset viewer app ---- # 定义ui, 该app用于查看数据 ui <- fluidPage( # App title ---- # App标题 titlePanel("Reactivity"), # Sidebar layout with input and output definitions ---- # 导航栏 sidebarLayout( # Sidebar panel for inputs ---- # 输入数据的导航栏 sidebarPanel( # Input: Text for providing a caption ---- # Note: Changes made to the caption in the textInput control # are updated in the output area immediately as you type
# Input: 选择的标题会马上显示在右侧的结果显示区域中
textInput(inputId = "caption", label = "Caption:", value = "Data Summary"), # Input: Selector for choosing dataset ---- # Input: 输入数据选择器 selectInput(inputId = "dataset", label = "Choose a dataset:", choices = c("rock", "pressure", "cars")), # Input: Numeric entry for number of obs to view ---- # Input:设定要查看的记录条数 numericInput(inputId = "obs", label = "Number of observations to view:", value = 10)
), # Main panel for displaying outputs ---- # 显示输出结果的主要面板
mainPanel( # Output: Formatted text for caption ---- # Output: 修改标题格式 h3(textOutput("caption", container = span)), # Output: Verbatim text for data summary ---- # Output: 打印data summary verbatimTextOutput("summary"), # Output: HTML table with requested number of observations ---- # Output: 生成HTML表格,以显示指定数量的观测值 tableOutput("view")
# Define UI for dataset viewer app ---- ui <- fluidPage( # App title ---- titlePanel("More Widgets"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- # 导航栏 sidebarPanel( # Input: Select a dataset ---- # Input: 选择数据 selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars")), # Input: Specify the number of observations to view ---- # Input: 查看多少条记录 numericInput("obs", "Number of observations to view:", 10), # Include clarifying text ---- # 帮助 helpText("Note: while the data view will show only the specified", "number of observations, the summary will still be based", "on the full dataset."), # Input: actionButton() to defer the rendering of output ---- # until the user explicitly clicks the button (rather than # doing it immediately when inputs change). This is useful if # the computations required to render output are inordinately # time-consuming. # 当获得结果所需时间较长, 是否运行脚本需要由用户决定, 此时用本按钮。 actionButton("update", "Update View")
), # Main panel for displaying outputs ---- # 主面板 mainPanel( # Output: Header + summary of distribution ---- # 输出 h4("Summary"), verbatimTextOutput("summary"), # Output: Header + table of distribution ---- # 输出 h4("Observations"), tableOutput("view") )
) )# Define server logic to summarize and view selected dataset ---- server <- function(input, output) { # Return the requested dataset ---- # Note that we use eventReactive() here, which depends on # input$update (the action button), so that the output is only # updated when the user clicks the button datasetInput <- eventReactive(input$update, { switch(input$dataset, "rock" = rock, "pressure" = pressure, "cars" = cars) }, ignoreNULL = FALSE) # Generate a summary of the dataset ---- output$summary <- renderPrint({ dataset <- datasetInput() summary(dataset) }) # Show the first "n" observations ---- # The use of isolate() is necessary because we don't want the table # to update whenever input$obs changes (only when the user clicks # the action button) output$view <- renderTable({ head(datasetInput(), n = isolate(input$obs)) })