Skip to content

Rendering questions based on the input #44

Open

Description

@maksymiuks

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?

Activity

  1. daattali commented on Nov 5, 2020

    @daattali
    Owner

    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.

  2. HBuenoo commented on Feb 17, 2021

    @HBuenoo

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions