library(tidyverse)
library(plotly)
library(here)

This part you will need to change. Remember that it is best practice to work from projects. If you have a class project with sub-folders you would open the project then navige to the velo_area (for example) folder and open your R markdown. The reason this is nice is that project will open with WR696 as the “working directory”. Problems loading data are generally due to thinking you are in a different wd than you actually are. Projects essentially eliminate this issue.

cross <- read_csv(here("hydrology/2_velocity_area_module/data/velo_area_data_R.csv"))
cross <- cross %>% 
  mutate(depth_m = depth_cm/100)


cross <- cross %>% 
  mutate(area_m2 = (lead(cross$width_m) - lag(cross$width_m)) / 2 *
  cross$depth_m, q = area_m2 * velo_m_s)

tot_q <- sum(cross$q, na.rm = TRUE)

cross <- cross %>% 
  mutate(q_per = (q/tot_q)*100)
cross_percent <- cross %>% 
  mutate(q_per = (q/tot_q)*100)

sections_exceeding <- cross_percent %>% 
  filter(q_per > 5)

mean_velo <- cross %>% 
  summarize(mean_velo = mean(velo_m_s))

total_area <- cross %>% 
  summarize(total_area = sum(area_m2, na.rm = TRUE))
fig <- cross %>% 
  mutate(new_depth = 1 - depth_m) %>% 
  ggplot(aes(x = width_m, y = new_depth)) +
  geom_point() +
  geom_line() +
  ylab("Depth (m)") +
  xlab("Width (m)") +
  theme_linedraw(base_size = 16) +
  ylim(0.5, 1)

ggplotly(fig)