I have an app with multiple inputs depending on one another. What I want to do now is to dynamically change the number of fill elements in my ggplot object. This is necessary for me since I have a lot of groups in my real data, and it should be possible to display them all (for completeness) or just a subset (for visibility).
So my questions is: how can I use an Input slider which takes the number of selected elements as max-value, to limit the number of allowed choices for the user?
The selection should follow two rules:  
- Select the n-elements with the highest value(relValue in my example)
 - Order the n-elements ascending by name(subgroup)
 
Here is my code:
tabA <- rep('A',1000)
tabB <- rep('B',1000)
tab <- c(tabA,tabB)
groupA <- rep(c('AA','BB'),500)
groupB <- rep(c('CC','DD'),500)
group <- c(groupA, groupB)
subgroupA <- rep(c('AAA','BBB','CCC','DDD'),125)
subgroupB <- rep(c('EEE','FFF','GGG','HHH'),125)
subgroupC <- rep(c('III','JJJ','KKK','LLL'),125)
subgroupD <- rep(c('MMM','NNN','OOO','PPP'),125)
subgroup1 <- c(subgroupA, subgroupB)
subgroup2 <- c(subgroupC, subgroupD)
subgroup <- c(subgroup1, subgroup2)
year <- rep(seq(1990,1999),100)
relValue <- rnorm(2000, 30, 10)
df <- data.frame(tab, group, subgroup, year, relValue, stringsAsFactors = FALSE)
library(shiny)
library(plotly)
library(ggplot2)
library(shinyWidgets)
ui <- fluidPage(
  sidebarPanel(
    uiOutput('selected_precision'),
    selectInput(inputId = 'selected_tab', label = 'tab', choices = ''),
    radioButtons(inputId = 'selected_group', label = 'group', choices = ''),
    pickerInput(inputId = 'selected_subgroup', label = 'subgroup', choices = '', multiple = TRUE)
    ),
    mainPanel(
      plotlyOutput('graph')
    )
  )
server <- function(input, output, session){
  output$selected_precision <- renderUI({ ### here the slider input is called
    req(input$selected_subgroup)
    sliderInput('selected_precision', label = 'precision', min = 1, max = length(input$selected_subgroup),
                value = length(input$selected_subgroup), round = TRUE, step = 1)
  })
  observe({
    updateSelectInput(session,
                      'selected_tab',
                      choices = df$tab)
  })
  observeEvent(input$selected_tab, {
    req(input$selected_tab)
    updateRadioButtons(
      session,
      'selected_group',
      choices = df %>%
        filter(tab == input$selected_tab) %>%
        select(group) %>%
        distinct(group) %>%
        .[[1]]
    )
  })
  filteredChoices <- reactive({
    df %>%
      #arrange(relValue) %>% ####I thought that was the way to go, but to no success...
      filter(tab == input$selected_tab) %>%
      filter(group == input$selected_group) %>%
      select(subgroup) %>%
      distinct(subgroup) %>%
      #top_n(length(subgroup)) %>%
      arrange(subgroup) %>%
      .[[1]]
  })
  observeEvent(c(input$selected_tab,input$selected_group),{
    req(input$selected_group)
    updatePickerInput(
      session,
      'selected_subgroup',
      choices = filteredChoices(),
      selected = filteredChoices()
    )
  })
  plotdata <- reactive({
      df %>%
        filter(group == input$selected_group) %>%
        filter(subgroup %in% input$selected_subgroup)
  })
  output$graph <- renderPlotly({
    req(nrow(plotdata()) > 0)
    plotdata() %>%
      plot_ly %>%
      ggplot()+
      geom_bar(plotdata(), mapping = aes(x = year, y = relValue, fill = subgroup)
                          ,stat = 'identity')
  })
}
shinyApp(ui,server)
EDIT: Code improved by request