0

I'm creating a shiny app to create graphs based on UI input. The problem is that the generated graph can only display one figure even though UI input is more than one. I'm running on R4.01 and shiny_1.7.1, plotly_4.10.0. The working shiny app can be seen at https://chopdsbu.shinyapps.io/testing_script/ My app codes:

ui <- fluidPage(
      sidebarLayout(
          sidebarPanel(
        uiOutput("fact"),
        uiOutput("amountCol")
          ),
        mainPanel(
        tabsetPanel(
                    tabPanel(tags$em("Boxplot",style="color: navy;"),
                   p("Boxplot for selected numerical variables"),
                   plotlyOutput("bplot")
          )
          
        ))
    ))


server  <- function(input, output,session) {
  
   output$amountCol <- renderUI({
    if (is.null(df)) return(NULL)
    #Let's only show numeric columns
    nums <- sapply(df, is.numeric)
    items=names(nums[nums])
    selectInput("Num", "Continous variables:", items,multiple = T)
  })
  
  output$fact <- renderUI({
    if (is.null(df)) return(NULL)
    nums <- sapply(df, is.character)
    filt <- lapply(df, function(x) length(unique(x)))
    filts <- filt<=30
    items=names(nums[filts])
    selectizeInput("char", "Categorical variables:",items,multiple = T)
  })
  
fact <- renderText({
    req(input$char) 
    factv <- vector()
    factlist <- paste(factv,input$char)
    return(factlist)
  })
  

num <- renderText({
    req(input$Num)
    numv <- vector()
    numlist <- paste0(numv,input$Num)
    return(numlist)
  })
  
  output$bplot <- renderPlotly({
     plist <- scan(text=num(),what = "",quiet = T)
    leng <- length(plist)
    plots <- vector(mode = "list", length = leng)
    bygrp='group'
        for(i in 1:leng){
        bplot <- ggplot(df, aes_string(y = plist[i], x= bygrp,color=bygrp))+geom_boxplot()
        plots[[i]] <- ggplotly(bplot) %>% plotly_build()
      }
    
    plots[[seq_along(1:length(plist))]]
        })
  
  }
shinyApp(ui = ui, server = server) 

I tried to use solution shown at R Shiny loop to display multiple plots but had no success. Codes I have tried:

      
      lapply(1:length(plist), function(i) {
         outputId <- paste0(i)
         plots[[outputId]] <- renderPlot(i)
      })

and

       for(j in 1:leng){
        print(plots[[j]])
       j+1
       }

I also tried plotly subplots but failed too. Many solutions I found over here were a couple of years old, so I'm wondering if there are better solution for this. Thanks!

ponyhd
  • 421
  • 1
  • 4
  • 15

0 Answers0