最近又要写app了,记录一下笔者认为的要点方便以后回顾,主要来自官方的 Mastering shiny 的Get started部分。PS:这本书习题答案在 这里 。 1、编写中的要点 1.1 使用响应表达式来减少代码重复 重复的代码,比如反复读取数据,会浪费算力,增加维护难度。传统的做法是单独使用一个变量来捕获input,或者使用函数来捕获计算。但是此处介绍了一个新的方法,即响应表达式—— reactive expressions 。 基本用法是用reactive({...})函数把你获取变量的代码包起来,他会在需要的时候更新。 server <- function(input, output, session) { # Create a reactive expression dataset <- reactive({ get(input$dataset, "package:datasets") }) output$summary <- renderPrint({ # Use a reactive expression by calling it like a function summary(dataset()) }) output$table <- renderTable({ dataset() }) } 1.2 中括号 如果render里有多行代码才需要加中括号。render中应该尽量减少计算。 1.3 UI和server输出的对应关系 textOutput()对应renderText()或renderPrint(),前者输出字符串,后者是R里的代码运行输出格式。 tableOutput()对应renderTable(),dataTableOutput()对应renderDataTable(),前者一般用于小的表格,后者表格可以交互。 plotOutput()对应renderPlot(),可以输出base、ggplot2和plotly等交互图。 2、前端——UI 2.1 Inputs shiny中有许多不同的输入函数可供使用,如 sliderInput() , selectInput() , textIn...