首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Shinyforms | 用 Shiny 写一个信息收集表

Shinyforms | 用 Shiny 写一个信息收集表

作者头像
生信菜鸟团
发布2020-12-03 15:57:31
发布2020-12-03 15:57:31
5.1K0
举报
文章被收录于专栏:生信菜鸟团生信菜鸟团

项目地址:https://github.com/daattali/shinyforms

安装

代码语言:javascript
复制
devtools::install_github("daattali/shinyforms")

使用方法

创建问题列表。每个问题都是一个带有 idtypetitle 以及 mandatorymandatory 默认为 FALSE)的列表:

代码语言:javascript
复制
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"))

创建表单信息列表,其中包含 idquestions 列表和 storage 类型(保存位置):

代码语言:javascript
复制
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() 创建表单:

代码语言:javascript
复制
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 参数)。

再举一个例子

此示例与上一个示例相似,但进一步说明了其他的一些功能。它显示了如何在一个应用程序中插入两个表格,以及如何使用管理员查看功能。

代码语言:javascript
复制
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 Sheets

在此示例中,我们将首先创建一个新的 Google 表格文档。接着,将工作表的密钥传递到存储列表,Shinyforms 即可与 Google 表格文档连接。

代码语言:javascript
复制
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)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信菜鸟团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 使用方法
  • 当前功能
  • 再举一个例子
  • 如何将收集结果保存至 Google Sheets
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档