coatless / quarto-webr

Community developed Quarto Extension to Embed webR for HTML Documents, RevealJS, Websites, Blogs, and Books.
https://quarto-webr.thecoatlessprofessor.com/
395 stars 19 forks source link

Hide code option for interactive graph #224

Open PykaAlexandro opened 1 month ago

PykaAlexandro commented 1 month ago

Hello,

great work you are doing here guys, I have a request, if I may, I would like to have the possibility to have an interactive code chunk (#| context: interactive, to have the "Run Code" button) but to hide the actual code.

That would be neat when you have lenghty code chunks, especially with interactive ggplot2 graph with a lot of customization.

I would then choose the parameters in a previous code chunk, that has the #| context: interactive as well, but in this case the code should be visible.

I hope I made myself clear, I would add some code, commented:

#| message: 'false'
#| context: setup
#*this is to load the data frame*
url <- "https://raw.githubusercontent.com/PykaAlexandro/DataKitFall2024/refs/heads/main/data_1-FL_renamed.csv"
download.file(url, "data_1-FL_renamed.csv")
data_1_FL_renamed <- read_csv("data_1-FL_renamed.csv")
#| message: 'false'
#| context: interactive
#*this is where I choose the parameters for the ggplot2 graph*
#choosing the county and setting its Area Median Income
county_fips <- 95
county_AMI <- 90400
#| message: 'false'
#| context: interactive
#*this is the actual graph, I don't like that it shows all the code, I would like it to be hidden :)*
#we construct the graph for the county of choice
general_graph <- data_1_FL_renamed %>%
  filter(county_fips_code == county_fips) %>%
#we change the negative values (present with missing data) to NAs  
  mutate(across(everything(), ~ case_when(.x >=0 ~ .x))) %>%
  ggplot(aes(med_house_income_est)) +
  geom_density()
#we save the graph's information into an object
general_built <- ggplot_build(general_graph)
general_built_data <- data.frame(general_built$data[[1]], stringsAsFactors = F)
#we add the segments based on the county's AMI
general_built_data <- general_built_data %>%
  mutate(Income = case_when(x < county_AMI * 0.3 ~ "Extremely Low",
                            x >= county_AMI * 0.3 & x < county_AMI * 0.5 ~ "Very Low",
                            x >= county_AMI * 0.5 & x < county_AMI * 0.8 ~ "Low",
                            x >= county_AMI * 0.8 & x < county_AMI * 1.2 ~ "Moderate",
                            x >= county_AMI * 1.2 ~ "Upper"),
         Income = factor(Income, levels = c("Extremely Low", "Very Low", "Low", "Moderate", "Upper"), ordered = TRUE))
#we define a colorblind friendly palette
cbbPalette <- c("#000000", "#E69F00", "#F0E442", "#56B4E9", "#009E73")
#we plot the income distribution segmented by the different income levels
ggplot(data = general_built_data, aes(x, y, fill = Income)) +
  geom_line() +
  geom_area() +
  geom_vline(xintercept = county_AMI, linetype = 2) +
  scale_x_continuous(labels =  scales::label_dollar()) +
  scale_y_continuous(labels = NULL, breaks = NULL) +
  scale_fill_manual(values = cbbPalette) +
  theme(legend.position = "bottom",
        legend.title = element_blank()) +
  labs(x = NULL,
       y = NULL,
       title ="Distribution of Income per Census Tract for Orange County, Florida")

thanks for your time, keep up the good work, cheers!