shinyforms icon indicating copy to clipboard operation
shinyforms copied to clipboard

Rendering questions based on the input

Open maksymiuks opened this issue 5 years ago • 2 comments

I try to render questions based on the initial input with a hand of renderUI function evoked within the server. However, I constantly fail. Do you have any guide on how should I do it?

maksymiuks avatar Nov 05 '20 16:11 maksymiuks

This feature isn't supported. I'm not actively working on this package, but we can leave this issue open because it is something I wanted to explore when I do get back to working on it.

daattali avatar Nov 05 '20 19:11 daattali

Hello,

I needed to do something like this work, so i managed to use a reactive object to create a list of questions that goes to shinyforms formUI and formServer functions:

Code:

reactive_questions <- reactive({
    ids <-  Use some mechanism to get some input and turn it into an unique ID # its a vector

-- In my case, i used a select Input with division formulas, so i wanted N numerators and D denominators:

division<- input[['formula']] %>% strsplit(split = "/") %>% unlist()

-- this was my "mechanism":

ids <- division %>%
  strsplits(splits = c("*", "+", "-"), fixed = TRUE) %>% # just a recursive function for strsplit, taking many splits.
  stringr::str_replace_all("[[:punct:]]", "") %>% # These lines were specific for my string management.
  Xmisc::strip() %>%
  tolower() %>%
  stringr::str_replace_all(" ", "_") %>%
  stringi::stri_trans_general("Latin-ASCII")


labels <- turn the IDs into labels # also a vector

# for each element in the vectors, I add a question in the forms:    
questions <- list()

for (i in 1:length(ids)) {
  questions[[length(questions) + 1]] <- list(
    id = sprintf("%s", ids[i]),
    type = "numeric",
    title = labels[i],
    mandatory = TRUE
  )
}
return(questions) # list with questions
  })

form <- reactive({
     formInfo <<- list(
      id = "my_form_ID",
      questions = reactive_questions(),
      name = "My form name",
      reset = TRUE
    )
    return(formInfo)
  })

  output$ui_my_form<-  renderUI({
    formUI(form())
  })

It migth not be optimal, but it worked. Hope I can inspire you

HBuenoo avatar Feb 17 '21 20:02 HBuenoo