Returning radio buttons and text box values in the same shiny module

shiny
shiny-modules

#1

I got help solving a dynamic radio button problem here: https://stackoverflow.com/questions/47341853/recovering-radiobutton-selections-in-shiny-using-modules-and-lapply

Now I am trying to add a textAreaInput so that the user can answer the question using a radio button selectiion and record notes on the reasons for their answer. I am still getting the correct behavior for radiobuttons, which returns the selected radio button result. But the textAreaInput will not perform the same way.

Specifically the module dynamically adds the number of questions and optional answers (as radio buttons). Then it reads the answers and outputs them into a list of answers. So if there are three questions, the output list will have three answers, one for each question. I want to add the textAreaInput to the list, so that for each of the questions there is a set of notes. Now the returned list should have 6 items

The list has six elements, including the three answers, but I am only getting the contents of the last text area input, not the contents of all three.

Here is a simple example:

global.R:

library(shinyjs)
questListInput <- function(id){
  ns <- NS(id)
  uiOutput(ns("c1"))
}
questList <- function(input, output, session, q, l) {
  ###This function requires the type of question to be specified
  #  ns <- session$ns
  output$c1 <- renderUI({
    lapply(1:nrow(q),
       function(i) {
         createLabel <- function(name,j) {
           paste0(name,": ",q$Element.Question[j],sep = "")
         }

     labelQ <- createLabel(q[i,]$QID,i)


         createOptions <- function() {
           as.matrix(q[i,4:length(q)])
         }
         tagList(
           fluidPage("testing",
           tags$hr(style="border-color: darkblue;"),
           fluidRow(
             column(6,
                    h4((paste0(labelQ))),
                    radioButtons(session$ns(paste0(l,i)), 
                                 "Please Select:", 
                                 choices = c(createOptions())
                    )
             ),
             column(6,
                    textAreaInput(inputId = session$ns(paste0(l,"a",i)),
                                  label = "Input Notes and Comments:",
                                  value = "Testing This Again",
                                  placeholder = "Testing This Out"
                    )
                )
           )
         )
         )
       }
)
})
observe({
 lapply(1:nrow(q),
       function(i) {
            runjs(paste0("Shiny.onInputChange(",session$ns(paste0(l,i)),",",input[[paste0(l,i)]],");"))
     runjs(paste0("Shiny.onInputChange(",session$ns(paste0(l,"a",i)),",",input[[paste0(l,"a",i)]],");"))
       }
)
 })
 }

ui.R:

library(shiny)
library(shinyjs)

shinyUI(
  fluidRow(
    shinyjs::useShinyjs(),
    questListInput("occList1"),
    textOutput("answerSet")
  )
)

server.R:

Category <- c("Occurrence","Occurrence","Occurrence")
QID <- c("C01","C02","C03")
Element.Question <- c("Problem 1","Problem 2","Problem 3")
Low <- c("Solution 1","Solution 1","Solution 1")
Medium <- c("Solution 2","Solution 2","Solution 2")
High <- c("Solution 3","Solution 3","Solution 3")

questionSet <- data.frame(Category,QID,Element.Question,Low,Medium,High)

occQuestSet <- questionSet[questionSet$Category == "Occurrence",]

shinyServer(function(input, output, session) {
browser()
answers <- callModule(questList, “occList1”, occQuestSet, “Occur1”)

 observe({

   occNames1 <- names(input)[grep("^occList1",names(input))]
   occInputList1 <- unlist(lapply(occNames1[1:nrow(occQuestSet)],function(id){input[[id]]}))

   print(paste0("inuptID of the list of occurrence notes: ",occNames1))
   print(paste0("list of Radio Selections:  ",lapply(occNames1[1:nrow(occQuestSet)],function(id){input[[id]]})))  
    print(paste0("list of notes:  ",lapply(occNames1[nrow(occQuestSet)+1:2*nrow(occQuestSet)],function(id){input[[id]]})))

  })

})