Creating a dynamic time series

Hi everyone, I am currently working on creating a dynamic time series in R that I have now struggled with for days without making any progress.

I would be really great if anyone had any suggestions what is wrong with my code.

the base of my code is the data set called "DATA.Shiny". my column names are structured in this way:

The column Company.Names encompasses all the different company names.

My goal is now separate the variable name from the year (e.g. Revenues & 2019, 2020, 2021) , select a sector and a company name. The selection of company names should be dynamically adjusted to only companies in the selected sector.

The plot should then show the time series for the selected companies for the selected variable across all years.

In addition the time series should also show the mean of the country where the selected company is located across all years.

The code I currently have is the following:

          tabName = "time",
              selectInput("sectortime", "Select Sector", choices = unique(DATA.Shiny$Sector)),
              selectInput("companytime", "Select Company", choices = unique(DATA.Shiny$Company.Name)),  # Update choices dynamically in observeEvent
              selectInput("variabletime", "Select Variable", choices = names(DATA.Shiny)[6:125], multiple = FALSE))
              plotOutput("timeseries", height = "600px", width = "800px")

The corresponding server code

 timeyears <- reactive({
    unique(sub(".*\\.", "", names(DATA.Shiny)[6:53]))
  # Reactive expression for filtered data
  filtered_data_timeseries <- reactive({
    req(input$sectortime, input$companytime, input$variabletime)
    selected_metric <- gsub("\\.\\d+$", "", input$variabletime)
    selected_var <- paste0(selected_metric, ".", timeyears())
    DATA.Shiny %>%
        Sector == input$sectortime,
        Company.Name == input$companytime
      ) %>%
      select(Company.Name, Country, Income.Classification.WB,  matches(selected_var))
  observeEvent(input$sectortime, {
    choices <- unique(DATA.Shiny$Company.Name[DATA.Shiny$Sector == input$sectortime])
    updateSelectInput(session, "companytime", choices = choices)
  # Update choices for Select Variable based on the available metrics
    updateSelectInput(session, "variabletime", choices = unique(sub("\\.\\d+$", "", names(DATA.Shiny)[6:53])))
  # Means for time series
  means_timeseries <- reactive({
    req(input$companytime, filtered_data_timeseries())
    # Calculate means for selected company
    mean_company <- filtered_data_timeseries() %>%
      mutate(Category = input$companytime)
    # Calculate means for country
    mean_country <- filtered_data_timeseries() %>%
      group_by(Country) %>%
      summarise(across(where(is.numeric), mean)) %>%
      mutate(Category = Country)
    # Calculate means for income category
    mean_ic <- filtered_data_timeseries() %>%
      group_by(Income.Classification.WB) %>%
      summarise(across(where(is.numeric), mean)) %>%
      mutate(Category = Income.Classification.WB)
    bind_rows(mean_company, mean_country, mean_ic)

  # Output for time series plot
  output$timeseriesplot <- renderPlot({
    req(nrow(filtered_data_timeseries()) > 0)

    ggplot(data = means_timeseries(), aes(x = "Category", y = !!sym(input$variabletime), color = Category)) +
      geom_line(aes(y = !!sym(input$variabletime)), position = "dodge") +
      labs(title = paste("Time Series Plot for", input$variabletime),
           x = "Category",
           y = input$variabletime,
           caption = "Source: Orbis") +
      theme_minimal() +
      theme(axis.text.x = element_text(size = 16),
            legend.key.size = unit(1, "cm"),  # Adjust the legend key size here
            legend.text = element_text(size = 14))  # Adjust the legend text size here

Thank you so much for any suggestions!