r - Keep UI Text Input after adding or removing Inputs -


i'm building small ui user enter splitlayout row of text builds statement (not needed question) solve puzzle.

first input

however, if user decides he/she needs additional row or less rows solve puzzle i'd adding or removing new row of inputs not delete remaining input rows.

second input attempt * gray placeholder.

how can best achieve desired result of:

enter image description here

please find trimmed code below. input.

library(shiny)  # define ui ui <- fluidpage(   # application title   titlepanel("identify a, b , c"),   sidebarlayout(     sidebarpanel(width = 5,                  helptext("present statement , receive response: 1 knight tells truth, 2 knave lies, , 3 normal can 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) {   ####### don't want these delete 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 application  shinyapp(ui = ui, server = server) 

it looks gave answer using uioutput+renderui, i'm going go other route: using insertui , removeui.

instead of having numeric input "number of questions", replaced button "add question" , 1 "remove question". have variable keeping track of how many questions there are. every time "add question" pressed, add 1 row. when "remove question" pressed, remove last row.

here's code:

library(shiny)  # define ui ui <- fluidpage(   # application title   titlepanel("identify a, b , c"),   sidebarlayout(     sidebarpanel(       width = 5,       helptext("present statement , receive response: 1 knight tells truth, 2 knave lies, , 3 normal can either."),       # buttons add/remove 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 number of questions   values <- reactivevalues(num_questions = 0)    # add question   observeevent(input$add, ignorenull = false, {     values$num_questions <- values$num_questions + 1     num <- values$num_questions     insertui(       selector = "#questions", = "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 question   observeevent(input$remove, {     num <- values$num_questions     # don't let user remove first question     if (num == 1) {       return()     }     removeui(selector = paste0("#question", num))     values$num_questions <- values$num_questions - 1   })   }  # run application  shinyapp(ui = ui, server = server) 

edit

op requested way retrieve user's input based on question number. that:

  1. add following ui

    numericinput("question_num", "show question number", 1), textoutput("question") 
  2. add following server

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

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -