How do I ensure variable value propagation within same code block in R?

13 views
Skip to first unread message

Mrinmay Kalita

unread,
Sep 29, 2022, 3:01:48 AM9/29/22
to Shiny - Web Framework for R
Hello,

I have an input in a code block in R (Shiny) using R Studio IDE.

How do I use the value of that input within the same block, i.e. in below example; how to make sure that value of input$histPlotNumFig is used later below in the line "for (i in 1:input$histPlotNumFig) {" without the compiler giving argument length is zero error.

CODE-

    output$plotHistogramsUI <- renderUI({
      req(dataCache$featureNames)
     
      allColumns <- union(dataCache$featureNames, dataCache$predTargetNames)
     
      ctrl <- list(fluidRow(column(6, numericInput('histPlotNumFig', 'Number of plots', 1, min = 1)),
                            column(6, numericInput('histPlotNumFigPerRow', 'Plots per row', 3, min = 1))))
     
      ctrl <- append(ctrl, list(selectInput('plotHistograms', label = 'Feature',
                               multiple = T, choices = allColumns, selected = NULL)))
     
      dF <- dataCache$dataFrame
     
      for (i in 1:input$histPlotNumFig) {
        # reuse existing values if any
        isolate({
          if (is.null(input[[paste0('histPlotFeatures', i)]])) {
            selected <- NULL
          } else {
            selected <- input[[paste0('histPlotFeatures', i)]]
          }
        })
       
        numColumns <- dataCache$dataCols[sapply(dataCache$dataFrame[, dataCache$dataCols, drop = F], is.numeric)]
       
        ctrl <- append(ctrl, list(selectizeInput(paste0('histPlotFeatures', i), label = paste0('Feature(s) - Plot ', i),
                                                 multiple = T, choices = numColumns, selected = selected)))
      }

Regards
Reply all
Reply to author
Forward
0 new messages