filtering a dataframe with a reactive variable

Hello, I am new to programming. I'm trying to create an interactive "app" on r markdown with "shinyApp", where users can upload an excel file and create a graph by pressing a button.
Initially I analyze the total arrival of tourists by their home country and then the average night stay (total number of nights spent in the city/ number of arrivals).
In the first analysis I filter out the top 12 countries by total number of tourists with the code i provide below.
My question is: for the second graph (average night stay), which is in another code chunk, I would like to reuse the "filtered_df" to consider only the relevant rows (highest number of tourists) with the new data uploaded by the users. The tables are organized in the same way with the same variable names. How can i do that?
I hope that I was able to explain the matter well :slight_smile:

Group by 'nazione' (country) and calculate the sum of tourists

sum_tourists_by_country <- tidy_data %>%
group_by(nazione) %>%
summarise(total_turisti = sum(turisti, na.rm = TRUE)) # na.rm = TRUE to handle missing values

Sort the dataframe based on the total sum of tourists in descending order

sorted_df <- sum_tourists_by_country %>% arrange(desc(total_turisti))

Extract the top 12 rows (countries) from the sorted dataframe

top_12_countries_df <- head(sorted_df, 12)

filtered_df <- tidy_data %>%
semi_join(top_12_countries_df, by = "nazione")

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.