添加或删除输入后保留UI文本输入

Eva*_*and 10 r dynamic textinput shiny

我正在构建一个小UI,用户将进入splitLayout文本行,构建一个语句(此问题不需要)来解决难题.

第一输入

但是,如果用户决定他/她需要额外的或更少的行来解决难题,我想添加或删除新的输入行以不删除剩余的输入行.

第二次输入尝试*灰色是占位符.

我怎样才能最好地达到我想要的结果:

在此输入图像描述

请在下面找到我修剪过的代码.感谢您的输入.

library(shiny)

# Define UI
ui <- fluidPage(
  # Application title
  titlePanel("Identify A, B and C"),
  sidebarLayout(
    sidebarPanel(width = 5,
                 helpText("Present a statement and receive a response: 1 is a Knight who always tells the truth, 2 is a Knave who always lies, and 3 is a Normal who can do either."),
                 # Number of Questions
                 numericInput(inputId = "Questions", label = "Number of Questions", 
                              value = 1, min = 1, max = 10, step = 1),
                 splitLayout(cellWidths = c("25%","70%"), 
                             style = "border: 1px solid silver;",
                             cellArgs = list(style = "padding: 3px"),
                             uiOutput("textQuestions"), uiOutput("textQuestions2"))
    ),
    mainPanel(
      # Right hand side output
    )
  )
)

# Define server logic 
server <- function(input, output) {
  ####### I don't want these to delete initially everytime??
  output$textQuestions <- renderUI({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      textInput(inputId = paste0("Who", i), label = paste0(i, ". Ask:"), placeholder = "A")
    })
  })
  ########
  output$textQuestions2 <- renderUI({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      textInput(inputId = paste0("Q", i) , label = paste0("Logic:"), 
                value = "", placeholder = "A == 1 & (B != 2 | C == 3)")
    })
  })
  ######
}

# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

Dea*_*ali 5

看起来有人已经用uiOutput+ 给你一个答案renderUI,所以我要走另一条路:用insertUIremoveUI.

我没有为"问题数量"提供数字输入,而是将其替换为"添加问题"按钮,另一个替换为"删除问题".我有一个变量跟踪有多少问题.每按一次"添加问题",我们就会添加一行.当按下"删除问题"时,我们删除最后一行.

这是代码:

library(shiny)

# Define UI
ui <- fluidPage(
  # Application title
  titlePanel("Identify A, B and C"),
  sidebarLayout(
    sidebarPanel(
      width = 5,
      helpText("Present a statement and receive a response: 1 is a Knight who always tells the truth, 2 is a Knave who always lies, and 3 is a Normal who can do either."),
      # Buttons to add/remove a question
      actionButton("add", "Add question"),
      actionButton("remove", "Remove question"),
      div(id = "questions",
          style = "border: 1px solid silver;")
    ),
    mainPanel(
      # Right hand side output
    )
  )
)

# Define server logic 
server <- function(input, output) {
  # Keep track of the number of questions
  values <- reactiveValues(num_questions = 0)

  # Add a question
  observeEvent(input$add, ignoreNULL = FALSE, {
    values$num_questions <- values$num_questions + 1
    num <- values$num_questions
    insertUI(
      selector = "#questions", where = "beforeEnd",
      splitLayout(
        cellWidths = c("25%","70%"), 
        cellArgs = list(style = "padding: 3px"),
        id = paste0("question", num),
        textInput(inputId = paste0("Who", num),
                  label = paste0(num, ". Ask:"),
                  placeholder = "A"),
        textInput(inputId = paste0("Q", num) ,
                  label = paste0("Logic:"),
                  placeholder = "A == 1 & (B != 2 | C == 3)")
      )
    )
  })

  # Remove a question
  observeEvent(input$remove, {
    num <- values$num_questions
    # Don't let the user remove the very first question
    if (num == 1) {
      return()
    }
    removeUI(selector = paste0("#question", num))
    values$num_questions <- values$num_questions - 1
  })


}

# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

编辑

OP请求一种基于问题编号检索用户输入的方法.要做到这一点:

  1. 将以下内容添加到UI

    numericInput("question_num", "Show question number", 1),
    textOutput("question")
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将以下内容添加到服务器

    get_question <- function(q) {
      paste(
        input[[paste0("Who", q)]],
        ":",
        input[[paste0("Q", q)]]
      )
    }
    
    output$question <- renderText({
      get_question(input$question_num)
    })
    
    Run Code Online (Sandbox Code Playgroud)