首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在更改输入值时,不要删除(用户输入的)反应性变量的值。

在更改输入值时,不要删除(用户输入的)反应性变量的值。
EN

Stack Overflow用户
提问于 2022-05-08 21:23:41
回答 1查看 53关注 0票数 2

我想收集个人信息(姓名和年龄)作为用户输入附带的演示代码。初始个体数为1,当用户增加/减少个体数的值时,它将响应地添加/减少相应的行以收集单个信息。我的问题是,在增加/减少个人数量时,如何避免删除当前的输入信息。例如,屏幕快照1显示用户(me)输入的个人1的信息。当我将个人数量的值改为2时,我之前输入的信息被删除了(屏幕截图2),我不得不重新输入个人1的信息。当我将个人数量增加到2时,我期望的是屏幕截图3 (个人1的信息不会被删除)。有人能帮我吗?谢谢!

更新05/15/2022

使用isolate的更新答案是我正在寻找的。再次感谢@jpdugo17 17的帮助。

示例代码

代码语言:javascript
复制
library(shiny)
ui <- fluidPage(
  tabsetPanel(
    tabPanel(
      h4("Individual Information"),
      fluidRow(column(4,numericInput("ninds",
                                     label = "Number of individuals",
                                     value = 1, min = 1, step = 0.5, width = "300px"))),
      br(),
      fluidRow(column(2,align = "center",strong("Individual #")),
               column(5,align = "center",strong("Individual Name")),
               column(5,align = "center",strong("Age"))),
      fluidRow(
        column(2,wellPanel(uiOutput("indNum"))),
        column(5,wellPanel(uiOutput("Name"))),
        column(5,wellPanel(uiOutput("Age"))))
    )
  )
)

server <- function(input, output) {
  # create reactive variable paste0("individualNum", i) for further using
  output$indNum <- renderUI({
    num <- as.integer(input$ninds)
    req(num)
    lapply(1:num, function(i) {
      numericInput(paste0("individualNum", i), value = i, label = "", max = i, min = i)
    })
  })
  # create reactive variable paste0("name", i) for further using 
  output$Name <- renderUI({
    num <- as.integer(input$ninds)
    req(num)
    lapply(1:num, function(i) {
      textInput(paste0("name", i), label = "")
    })
  })
  # create reactie variable paste0("age", i) for further using 
  output$Age <- renderUI({
    num <- as.integer(input$ninds)
    req(num)
    lapply(1:num, function(i) {
      numericInput(paste0("age", i), label = "", value = 0)
    })
  })
}

# Run the app ----
shinyApp(ui, server)

screenshot-1

screenshot-2

screenshot-3

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-09 04:03:19

编辑

我们可以将isolate与输入的当前值一起使用,并将其作为numericInputtextInput中的value参数传递。这将起作用,因为还不存在的任何输入都会产生NULL

代码语言:javascript
复制
server <- function(input, output) {
  # create reactive variable paste0("individualNum", i) for further using

  num <- reactive({
    req(input$ninds)
    input$ninds
  })

  output$indNum <- renderUI({
    lapply(1:num(), function(i) {
      numericInput(paste0("individualNum", i), value = i, label = "", max = i, min = i)
    })
  })
  # create reactive variable paste0("name", i) for further using
  output$Name <- renderUI({
    lapply(1:num(), function(i) {
      textInput(paste0("name", i), label = "", value = isolate(input[[paste0("name", i)]]))
    })
  })
  # create reactie variable paste0("age", i) for further using
  output$Age <- renderUI({
    lapply(1:num(), function(i) {
      numericInput(paste0("age", i), label = "", value = isolate(input[[paste0("age", i)]]))
    })
  })
}

使用insertUI的原始答案

我们可以实现这样的逻辑:创建一个计数器来存储当前的输入编号,制作两个按钮,一个按钮添加,另一个按钮删除输入。我们必须用一个带有唯一id的div包装每个输入(因为输入函数通常会添加多个元素)。

代码语言:javascript
复制
library(shiny)
library(purrr)

ui <- fluidPage(
  tabsetPanel(
    tabPanel(
      h4("Individual Information"),
      fluidRow(
        column(
          4, fluidRow(
            column(6, numericInput("ninds",
              label = "Number of individuals",
              value = 1, min = 1, step = 0.5, width = "300px"
            )), column(3, actionButton("add_ui", "Add Individual", style = "background-color: green;")),
            column(3, actionButton("remove_ui", "Remove Individual", style = "background-color: red;"))
          )
        )
      ),
      br(),
      fluidRow(
        column(2, align = "center", strong("Individual #")),
        column(5, align = "center", strong("Individual Name")),
        column(5, align = "center", strong("Age"))
      ),
      fluidRow(
        column(2, wellPanel(id = "IndNumber")),
        column(5, wellPanel(id = "Name")),
        column(5, wellPanel(id = "Age"))
      )
    )
  )
)


server <- function(input, output) {

  # track the number of inputs
  ui_counter <- reactiveVal(1)

  observeEvent(input$add_ui, {
    div_nms <- map_chr(c("individualNum", "name", "age"), ~ paste0("div", .x, ui_counter()))

    # individual number
    insertUI(
      selector = "#IndNumber",
      ui = div(
        id = div_nms[[1]],
        numericInput(paste0("individualNum", ui_counter()),
          label = "",
          value = ui_counter(),
          min   = ui_counter(),
          max   = ui_counter()
        )
      )
    )

    # name input
    insertUI(
      selector = "#Name",
      ui = div(id = div_nms[[2]], textInput(paste0("name", ui_counter()),
        label = ""
      ))
    )

    # age input
    insertUI(
      selector = "#Age",
      ui = div(id = div_nms[[3]], numericInput(paste0("age", ui_counter()),
        label = "",
        value = 0
      ))
    )


    ui_counter(ui_counter() + 1)
  })

  # observer to remove the divs containing the inputs
  observeEvent(input$remove_ui, {
    if (ui_counter() > 1) {
      walk(c("individualNum", "name", "age"), ~ removeUI(paste0("#div", .x, ui_counter() - 1)))
      ui_counter(ui_counter() - 1)
    }
  })
}

# Run the app ----
shinyApp(ui, server)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72165191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档