'Shiny interactive UI - not able to access new input variabel created using "insertUI"

Thanks for your help in advance! After an R update my code did not work anymore.

After creating, a new input variable with "insertUI" the new variable stays empty. Why? Maybe I struggel with the order of reactive calculations done in shiny.

My aim is to let the user change the color of the barplot factor by factor. At the same time the user sould be able to exclude some factors from the dataset. I want to include the name of the factor in the color-selector, to make it easy for the user.

Here is my reproducible example:

library(shiny)
library(dplyr)
library(tibble)
library(shinyWidgets)
library(ggplot2)


##load data
x=as.data.frame(Titanic)
 str(x)
choice_fac=colnames(x)[1:4]
choice_col=c("steelblue", "cornflowerblue","firebrick", "palegoldenrod","forestgreen", "darksalmon", "olivedrab", "plum")


ui<-fluidPage(
  
  fluidRow(
  column("Class:", checkboxGroupInput("Class_sel", "", choices=levels(x[,1]), selected=levels(x[,1]), inline=FALSE), width=2, style='margin-bottom:10px;  padding: 10px;'),
  column("Sex:" , checkboxGroupInput("Sex_sel", "", choices=levels(x[,2]), selected=levels(x[,2]), inline=FALSE), width=2,style='margin-bottom:10px; padding: 10px;'),
  column("Age:" ,checkboxGroupInput("Age_sel", "", choices=levels(x[,3]), selected=levels(x[,3]), inline=FALSE), width=2,style='margin-bottom:10px; padding: 10px;'),
  column("Survived:" ,checkboxGroupInput("Sur_sel", "", choices=levels(x[,4]), selected=levels(x[,4]), inline=FALSE), width=2,style='margin-bottom:10px; padding: 10px;')
  ),
  selectInput("Fac_sel", "Please select:", choices=choice_fac, selected=choice_fac[1], width = "100%", multiple=FALSE),
  fluidRow(
    tags$div(id = 'placeholder_colors'),
            ),
   plotOutput("plot")

)


server<-function(input, output){
  

  x_sel=reactive({
    x%>%filter(Class %in% input$Class_sel,
               Sex %in% input$Sex_sel,
               Age %in% input$Age_sel,
               Survived %in% input$Sur_sel)
    
  })
  
  
  col_match=reactive({
  
    col_n=x_sel()%>%distinct(!!sym(input$Fac_sel))%>%na.omit()
    col_n%>%add_column(col=choice_col[1:nrow(col_n)])%>%mutate(mycolor=paste("mycolor", row_number(), sep=""))
  })
  
  observeEvent(c(input$Fac_sel, input$Class_sel, input$Sex_sel, input$Age_sel, input$Sur_sel) , {
    removeUI(selector = 'div:has(>#mycolor1)')
    removeUI(selector = 'div:has(>#mycolor2)')
    removeUI(selector = 'div:has(>#mycolor3)')
    removeUI(selector = 'div:has(>#mycolor4)')
    removeUI(selector = 'div:has(>#mycolor5)')
    
    print(col_match())
    
      lapply(1:nrow(col_match()), function(i){
      insertUI(selector = "#placeholder_colors", ui = column(colorSelectorInput(inputId =col_match()[i,3], label = paste("Farbe ", i,": ", col_match()[i,1], sep=""), choices =choice_col, selected=col_match()[i,2]), width=3))
        }) 

  })
    

input_col=reactive({
  x=c(input$mycolor1, input$mycolor2, input$mycolor3, input$mycolor4 , input$mycolor5, rep("blue",5) )
  y=x[1:nrow(col_match())]
  col_match()%>%add_column(y)%>%pull(y,!!sym(input$Fac_sel))
})


output$plot=renderPlot({
    x_sel()%>%ggplot()+
    geom_col(aes(x=!!sym(input$Fac_sel), y=Freq, fill=!!sym(input$Fac_sel)))+
    scale_fill_manual(values=input_col())
                           })

observe({print(input_col())})  
observe({print(input$mycolor1)})  
observe({print(input$mycolor2)})  
observe({print(input$mycolor3)})  
observe({print(input$mycolor4)})  
observe({print(input$mycolor5)}) 

}

shinyApp(ui, server)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source