Shiny/R - 使单个按钮发送多个响应
Shiny/R - Make a single button send multiple responses
我需要“发送”按钮将所有应用信息发送到保存在 Google 云端硬盘中的电子表格(不仅仅是第一个信息);每个列(姓名,年龄,出生...)。谢谢!
这是我的代码:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(googlesheets4)
library(googledrive)
library(readr)
library(DT)
# Dashboard
ui <- dashboardPage(skin = "red",
dashboardHeader(
title = "Controller"
),
dashboardSidebar(
sidebarMenu(
menuItem(
"Home",
tabName = "home",
icon = icon("home"))
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "home", h2("Hello"),
br(),
box(
h2(icon("id-card"), "Client"),
width = 2000,
br(),
# Insery name
textInputIcon(
inputId = "name",
label = "Name"),
# Birth
dateInput(
inputId = "dob",
label ="Birth",
min = "1960-01-01",
max = Sys.Date(), format = "yyyy/mm/dd",
language = "en"),
textInputIcon(
inputId = "age",
label = "Age"),
# Phone
textInputIcon(
inputId = "phone",
label = "Phone"),
# E-mail
textInputIcon(
inputId = "email",
label = "E-mail"),
# Send
actionButton("send", "Send"),
)
)
)
)
)
# Server
server <- function(input, output, session) {
textname <- reactive({
as.data.frame(input$name)
})
observeEvent(input$sendname, {
Selfie <- gs4_get('link')
sheet_append(Selfie, data = textname())
})
observe({ dob <- input$dob
if(!is.null(dob)) {
days <- as.integer((Sys.Date() - as.Date(dob)))
years <- as.integer(days / 365)
months <- as.integer((days %% 365 ) / 30)
age <- paste(years, 'year(s)', months, 'month(s)')
#print(age)
updateTextInput(session, "age", value = age)
}
})
}
# App
shinyApp(ui = ui, server = server)
我们可以遍历 input
并在 df 中收集所有内容。
observeEvent(input$send, {
require(tidyverse)
#gather info of all the relevant inputs
client_df <- c('name', 'dob', 'age', 'phone', 'email') %>%
map_dfc(~ tibble("{.x}" := input[[.x]]))
print(client_df) #this will print a df with one row in the console
#Selfie <- gs4_get('link')
#sheet_append(Selfie, data = client_df)
})
我需要“发送”按钮将所有应用信息发送到保存在 Google 云端硬盘中的电子表格(不仅仅是第一个信息);每个列(姓名,年龄,出生...)。谢谢!
这是我的代码:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(googlesheets4)
library(googledrive)
library(readr)
library(DT)
# Dashboard
ui <- dashboardPage(skin = "red",
dashboardHeader(
title = "Controller"
),
dashboardSidebar(
sidebarMenu(
menuItem(
"Home",
tabName = "home",
icon = icon("home"))
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "home", h2("Hello"),
br(),
box(
h2(icon("id-card"), "Client"),
width = 2000,
br(),
# Insery name
textInputIcon(
inputId = "name",
label = "Name"),
# Birth
dateInput(
inputId = "dob",
label ="Birth",
min = "1960-01-01",
max = Sys.Date(), format = "yyyy/mm/dd",
language = "en"),
textInputIcon(
inputId = "age",
label = "Age"),
# Phone
textInputIcon(
inputId = "phone",
label = "Phone"),
# E-mail
textInputIcon(
inputId = "email",
label = "E-mail"),
# Send
actionButton("send", "Send"),
)
)
)
)
)
# Server
server <- function(input, output, session) {
textname <- reactive({
as.data.frame(input$name)
})
observeEvent(input$sendname, {
Selfie <- gs4_get('link')
sheet_append(Selfie, data = textname())
})
observe({ dob <- input$dob
if(!is.null(dob)) {
days <- as.integer((Sys.Date() - as.Date(dob)))
years <- as.integer(days / 365)
months <- as.integer((days %% 365 ) / 30)
age <- paste(years, 'year(s)', months, 'month(s)')
#print(age)
updateTextInput(session, "age", value = age)
}
})
}
# App
shinyApp(ui = ui, server = server)
我们可以遍历 input
并在 df 中收集所有内容。
observeEvent(input$send, {
require(tidyverse)
#gather info of all the relevant inputs
client_df <- c('name', 'dob', 'age', 'phone', 'email') %>%
map_dfc(~ tibble("{.x}" := input[[.x]]))
print(client_df) #this will print a df with one row in the console
#Selfie <- gs4_get('link')
#sheet_append(Selfie, data = client_df)
})