英文:
Dynamic widgets in shiny
问题
我有一个示例应用程序如下。 创意是默认情况下只能看到2个小部件。 但是当单击“添加”时,小部件会继续添加。 但是当我们第一次单击“添加”时,小部件会弹出(这没问题),现在当在“第一家公司”中输入一些值,然后单击“添加”,另一个小部件会弹出(这也没问题)。 但是现在,“第一家公司”的值消失了。 我们可以在单击“添加”时使其出现吗?
library(shiny)
ui <- fluidPage(
uiOutput("fcompany"),
uiOutput("add_exp"),
actionButton("add_button", "Add")
)
server <- function(input, output, session) {
get_data <- reactiveValues()
get_data$all_experiences <- c(0)
output$fcompany <- renderUI({
column(width = 2, textInput("id_zero", label = "Gra", placeholder = "Col"),
numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10))
})
observeEvent(input$add_button,{
added_variable <- max(get_data$all_experiences)+1
excel_Input <- structure(list(number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), words = c("one",
"two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten"), company = c("First Company", "Second Company", "Third Company",
"Fourth Company", "Fifth Company", "Sixth Company", "Seventh Company",
"Eighth Company", "Ninth Company", "Tenth Company")), row.names = c(NA,
10L), class = "data.frame")
output$add_exp <- renderUI({
lapply(1:added_variable, function(i){
column(width = 2, textInput(paste0("id_",excel_Input$words[excel_Input$number == i]), label = excel_Input$company[excel_Input$number == i],
placeholder = paste0("Enter the ", excel_Input$company[excel_Input$number == i])),
numericInput(paste0("idexp_",excel_Input$words[excel_Input$number == i]), label = "", value = 4, min = 0, max = 10))
})
})
get_data$all_experiences <- c(get_data$all_experiences, added_variable)
})
}
shinyApp(ui, server)
英文:
I have a sample application as below. The idea is only 2 widgets are seen by default. But when you click on "Add", the widgets keep on adding. But when we click "Add" for the first time, the widgets pops up (that is fine) and now when enter some value in "First Company" and then click on "Add", another widgets pops up(this is also fine). But now, the values in "First Company" disappears. Can we make is appear when we click on "Add"
library(shiny)
ui <- fluidPage(
uiOutput("fcompany"),
uiOutput("add_exp"),
actionButton("add_button", "Add")
)
server <- function(input, output, session) {
get_data <- reactiveValues()
get_data$all_experiences <- c(0)
output$fcompany <- renderUI({
column(width = 2, textInput("id_zero", label = "Gra", placeholder = "Col"),
numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10))
})
observeEvent(input$add_button,{
added_varaible <- max(get_data$all_experiences)+1
excel_Input <- structure(list(number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), words = c("one",
"two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten"), company = c("First Company", "Second Company", "Third Company",
"Fourth Company", "Fifth Company", "Sixth Company", "Seventh Company",
"Eighth Company", "Ninth Company", "Tenth Company")), row.names = c(NA,
10L), class = "data.frame")
output$add_exp <- renderUI({
lapply(1:added_varaible, function(i){
column(width = 2, textInput(paste0("id_",excel_Input$words[excel_Input$number == i]), label = excel_Input$company[excel_Input$number == i],
placeholder = paste0("Enter the ", excel_Input$company[excel_Input$number == i])),
numericInput(paste0("idexp_",excel_Input$words[excel_Input$number == i]), label = "", value = 4, min = 0, max = 10))
})
})
get_data$all_experiences <- c(get_data$all_experiences, added_varaible)
})
}
shinyApp(ui, server)
答案1
得分: 1
以下是代码的翻译部分:
你没有提供有关应用程序更广泛目标的多少上下文。
我建议预定义所有需要的扩展输入,并默认隐藏它们。稍后,如果需要,我们可以取消隐藏每个输入。
library(shiny)
excel_Input <- structure(
list(
number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
words = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"),
company = c(
"First Company", "Second Company", "Third Company",
"Fourth Company", "Fifth Company", "Sixth Company", "Seventh Company",
"Eighth Company", "Ninth Company", "Tenth Company"
)
),
row.names = c(NA, 10L),
class = "data.frame"
)
ex_inputs <- append(
list(shiny::tags$div(
shiny::textInput("id_zero", label = "Gra", placeholder = "Col"),
shiny::numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10)
)),
lapply(
seq_len(nrow(excel_Input)),
function(i) {
ex_word <- excel_Input$words[excel_Input$number == i]
ex_label <- excel_Input$company[excel_Input$number == i]
ex_company <- excel_Input$company[excel_Input$number == i]
shiny::tags$div(
id = sprintf("ex_%s", i),
class = "hidden",
shiny::textInput(
paste0("id_", ex_word),
label = ex_label,
placeholder = paste0("输入", ex_company)
),
shiny::numericInput(paste0("idexp_", ex_word), label = ex_word, value = 4, min = 0, max = 10)
)
}
)
)
ui <- fluidPage(
shinyjs::useShinyjs(),
shiny::tags$div(
shiny::tags$div(
style = "display: flex",
ex_inputs
),
shiny::tags$div(
actionButton("add_button", "添加")
)
)
)
server <- function(input, output, session) {
counter <- shiny::reactiveVal(0)
shiny::observeEvent(input$add_button, {
if (counter() < 10) counter(counter() + 1)
})
shiny::observe(shinyjs::removeClass(sprintf("ex_%s", counter()), "hidden"))
# 测试
observe({
lapply(seq_len(counter()), function(i) {
ex_word <- excel_Input$words[excel_Input$number == i]
print(input[[paste0("id_", ex_word)]])
})
})
}
shiny::shinyApp(ui, server)
英文:
You did not provide much context about the broader objective of the app.
I propose to pre-define all needed extension inputs and hide them by default. Later we can unhide each if needed.
library(shiny)
excel_Input <- structure(
list(
number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
words = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"),
company = c(
"First Company", "Second Company", "Third Company",
"Fourth Company", "Fifth Company", "Sixth Company", "Seventh Company",
"Eighth Company", "Ninth Company", "Tenth Company"
)
),
row.names = c(NA, 10L),
class = "data.frame"
)
ex_inputs <- append(
list(shiny::tags$div(
shiny::textInput("id_zero", label = "Gra", placeholder = "Col"),
shiny::numericInput("idexp_zero", label = "", value = 4, min = 0, max = 10)
)),
lapply(
seq_len(nrow(excel_Input)),
function(i) {
ex_word <- excel_Input$words[excel_Input$number == i]
ex_label <- excel_Input$company[excel_Input$number == i]
ex_company <- excel_Input$company[excel_Input$number == i]
shiny::tags$div(
id = sprintf("ex_%s", i),
class = "hidden",
shiny::textInput(
paste0("id_", ex_word),
label = ex_label,
placeholder = paste0("Enter the ", ex_company)
),
shiny::numericInput(paste0("idexp_", ex_word), label = ex_word, value = 4, min = 0, max = 10)
)
}
)
)
ui <- fluidPage(
shinyjs::useShinyjs(),
shiny::tags$div(
shiny::tags$div(
style = "display: flex",
ex_inputs
),
shiny::tags$div(
actionButton("add_button", "Add")
)
)
)
server <- function(input, output, session) {
counter <- shiny::reactiveVal(0)
shiny::observeEvent(input$add_button, {
if (counter() < 10) counter(counter() + 1)
})
shiny::observe(shinyjs::removeClass(sprintf("ex_%s", counter()), "hidden"))
# Test
observe({
lapply(seq_len(counter()), function(i) {
ex_word <- excel_Input$words[excel_Input$number == i]
print(input[[paste0("id_", ex_word)]])
})
})
}
shiny::shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论