项目地址:https://github.com/daattali/shinyforms
devtools::install_github("daattali/shinyforms")创建问题列表。每个问题都是一个带有 id,type,title 以及 mandatory (mandatory 默认为 FALSE)的列表:
library(shiny)library(shinyforms)questions <- list( list(id = "name", type = "text", title = "Name", mandatory = TRUE), list(id = "age", type = "numeric", title = "Age"), list(id = "favourite_pkg", type = "text", title = "Favourite R package"), list(id = "terms", type = "checkbox", title = "I agree to the terms"))创建表单信息列表,其中包含 id,questions 列表和 storage 类型(保存位置):
formInfo <- list( id = "basicinfo", questions = questions, storage = list( # Right now, only flat file storage is supported type = STORAGE_TYPES$FLATFILE, # The path where responses are stored path = "responses" ))以上就是就是我们所需的表单内容。现在,我们可以通过 Shiny 调用 formUI() 和 formServer() 创建表单:
ui <- fluidPage( formUI(formInfo))server <- function(input, output, session) { formServer(formInfo)}shinyApp(ui = ui, server = server)当然,你也可以在 Shiny App 中添加更多内容。表单仅仅是一个“模块”,你可以将其插入任何所需 Shiny App 中。每次提交响应时,它将被另存为文件。
•表单结果保存到本地文件;•支持必填字段和可选字段(在问题列表中使用 mandatory 参数);•仅用一行代码即可为 Shiny UI 和 server 添加表单;•可以在同一 App 中包含多种不同形式;•以干净和用户友好的方法来捕获和报告错误;•问题和表格数据采用 R 列表格式;•支持的问题类型:文本,数字,复选框;•能够多次提交同一表单(在表单信息列表中使用 multiple = FALSE 参数以禁止多次提交);•支持管理员模式:如果在 URL 上添加 ?admin=1,则会在表格下方看到查看收集结果的按钮。如果你想查看所有收集结果,则必须输入密码以验证您是管理员 (在表单信息列表中使用 password 参数可设置密码);•支持更复杂的输入验证,当字段不满足某些条件时,会给出错误提示消息(在表单信息列表中使用 validations 参数);•可选加入“重置”按钮,用于重置表单中的字段(在表单信息列表中使用 reset = TRUE 参数);•问题可包含提示文本,显示在标题下方(在问题列表中使用 hint 参数)。
此示例与上一个示例相似,但进一步说明了其他的一些功能。它显示了如何在一个应用程序中插入两个表格,以及如何使用管理员查看功能。
library(shiny)library(shinyforms)# 定义第一个表格: basic informationbasicInfoForm <- list( id = "basicinfo", questions = list( list(id = "name", type = "text", title = "Name", mandatory = TRUE, hint = "Your name exactly as it is shown on your passport"), list(id = "age", type = "numeric", title = "Age", mandatory = FALSE), list(id = "favourite_pkg", type = "text", title = "Favourite R package"), list(id = "terms", type = "checkbox", title = "I agree to the terms") ), storage = list( type = STORAGE_TYPES$FLATFILE, path = "responses" ), name = "Personal info", password = "shinyforms", reset = TRUE, validations = list( list(condition = "nchar(input$name) >= 3", message = "Name must be at least 3 characters"), list(condition = "input$terms == TRUE", message = "You must agree to the terms") ))# 定义第二个表格: soccersoccerFormInfo <- list( id = "soccerform", questions = list( list(id = "team", type = "text", title = "Favourite soccer team"), list(id = "player", type = "text", title = "Favourite player") ), storage = list( type = STORAGE_TYPES$FLATFILE, path = "soccer" ), multiple = FALSE)ui <- fluidPage( h1("shinyforms example"), tabsetPanel( tabPanel( "Basic info", formUI(basicInfoForm) ), tabPanel( "Soccer", formUI(soccerFormInfo) ) ))server <- function(input, output, session) { formServer(basicInfoForm) formServer(soccerFormInfo)}shinyApp(ui = ui, server = server)足球表单使用了 multiple = FALSE 选项,这表示用户只能提交一次(如果重新启动Shiny应用程序,则同一用户可以再次提交表单)。第一个表格使用了 password 参数,这意味着如果在 URL 中添加 ?admin=1,即可查看所有收集结果。


在此示例中,我们将首先创建一个新的 Google 表格文档。接着,将工作表的密钥传递到存储列表,Shinyforms 即可与 Google 表格文档连接。
library(shiny)library(shinyforms)library(googlesheets)# 创建一个新的 google sheetsdf <- data.frame(name = "", age = 0, favourite_pkg = "", terms = TRUE)google_df <- gs_new("responses", input = df, trim = TRUE, verbose = FALSE)questions <- list( list(id = "name", type = "text", title = "Name", mandatory = TRUE), list(id = "age", type = "numeric", title = "Age"), list(id = "favourite_pkg", type = "text", title = "Favourite R package"), list(id = "terms", type = "checkbox", title = "I agree to the terms"))formInfo <- list( id = "basicinfo", questions = questions, storage = list( # Right now, only flat file storage is supported type = STORAGE_TYPES$GOOGLE_SHEETS, # The path where responses are stored path = "responses", # Get the Google sheet key key = google_df$sheet_key ))ui <- fluidPage( formUI(formInfo))server <- function(input, output, session) { formServer(formInfo)}shinyApp(ui = ui, server = server)