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

时间:2017-04-07 09:15:00

标签: r dynamic shiny textinput

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

First Input

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

Second Input Attempt *灰色是占位符。

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

enter image description here

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

private var traits: UIAccessibilityTraits = UIAccessibilityTraitNone

// MARK: UITableViewCell life cycle
override func awakeFromNib() {
    super.awakeFromNib()
    traits = super.accessibilityTraits
}

// MARK: UIAccessibilityElement
override var accessibilityTraits: UIAccessibilityTraits {
    get {
        if isSelected {
            return traits | UIAccessibilityTraitSelected
        }
        return traits
    }

    set {
        traits = newValue
    }
}

2 个答案:

答案 0 :(得分:5)

看起来有人已经使用uiOutput + renderUI给了您答案,所以我将走另一条路线:使用insertUIremoveUI。< / p>

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

以下是代码:

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)

修改

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

  1. 将以下内容添加到用户界面

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

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

答案 1 :(得分:2)

您可以将其存储在无效值中:

  global <- reactiveValues(ask = c(), logic = c())

  observe({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      inputVal <- input[[paste0("Who", i)]]
      if(!is.null(inputVal)){
        global$logic[i] <- inputVal
      }
      inputValQ <- input[[paste0("Q", i)]]
      if(!is.null(inputValQ)){
        global$ask[i] <- inputValQ
      }
    })
  })

这将为您生成以下代码示例: 作为副作用,如果输入被移除然后重新输入,则还将存储值。

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) {
  global <- reactiveValues(ask = c(), logic = c())

  observe({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      inputVal <- input[[paste0("Who", i)]]
      if(!is.null(inputVal)){
        global$ask[i] <- inputVal
      }
      inputValQ <- input[[paste0("Q", i)]]
      if(!is.null(inputValQ)){
        global$logic[i] <- inputValQ
      }
    })
  })
  ####### 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", value = global$ask[i])
    })
  })
  ########
  output$textQuestions2 <- renderUI({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      textInput(inputId = paste0("Q", i) , label = paste0("Logic:"), value = global$logic[i],
                placeholder = "A == 1 & (B != 2 | C == 3)")
    })
  })
  ######
}

# Run the application 
shinyApp(ui = ui, server = server)