我想根据数据框生成 table 个输入,这样如果我更改 table 中的任何内容,数据框就会被修改。 -R闪亮
I want to generate a table of inputs based on a dataframe such that if I change anything in that table, the dataframe is modified. - R shiny
我有一个包含 3 列的数据框:-
- -has_subscribed (0,1)
- -性别(因素-男、女),
- -地址(文字)
并且我想基于此数据框生成 table 个输入,这样如果我更改 table 中的任何内容,数据框就会被修改。我希望 has_subscribed 显示为复选框,性别显示为下拉列表,地址显示为 text_input。我怎样才能在 R Shiny 中做到这一点?可能吗?
这是一个基本示例:
在您的工作目录中创建一个名为 responses
的目录,然后应用此代码:
请查看我的 post 和我的回答 以扩展此类任务:
library(shiny)
# Define the fields we want to save from the form
fields <- c("has_subscirbed", "gender", "address")
# Save a response
# ---- This is one of the two functions we will change for every storage type ----
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responses")) {
responses <<- rbind(responses, data)
} else {
responses <<- data
}
}
# Load all previous responses
# ---- This is one of the two functions we will change for every storage type ----
loadData <- function() {
if (exists("responses")) {
responses
}
}
# Shiny app with 3 fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
checkboxInput("has_subscribed", "Has Subscribed", FALSE),
selectInput("gender", "Gender", c("Female", "Male")),
textInput("address", "Address", ""),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
}
)
我有一个包含 3 列的数据框:-
- -has_subscribed (0,1)
- -性别(因素-男、女),
- -地址(文字)
并且我想基于此数据框生成 table 个输入,这样如果我更改 table 中的任何内容,数据框就会被修改。我希望 has_subscribed 显示为复选框,性别显示为下拉列表,地址显示为 text_input。我怎样才能在 R Shiny 中做到这一点?可能吗?
这是一个基本示例:
在您的工作目录中创建一个名为 responses
的目录,然后应用此代码:
请查看我的 post 和我的回答
library(shiny)
# Define the fields we want to save from the form
fields <- c("has_subscirbed", "gender", "address")
# Save a response
# ---- This is one of the two functions we will change for every storage type ----
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responses")) {
responses <<- rbind(responses, data)
} else {
responses <<- data
}
}
# Load all previous responses
# ---- This is one of the two functions we will change for every storage type ----
loadData <- function() {
if (exists("responses")) {
responses
}
}
# Shiny app with 3 fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
checkboxInput("has_subscribed", "Has Subscribed", FALSE),
selectInput("gender", "Gender", c("Female", "Male")),
textInput("address", "Address", ""),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
}
)