geanders / noaastormevents

explore noaa storm database
14 stars 1 forks source link

Error in details vignette under ex <- county_events_2015 #25

Closed theresekon closed 4 years ago

theresekon commented 4 years ago

When I try to run the following code chunk in the details.Rmd file:

ex <- county_events_2015 %>% 
  filter(str_to_lower(state) %in% as.character(state.fips$polyname)) %>% 
  unite(begin_date, begin_yearmonth, begin_day, sep = "-") %>% 
  mutate(begin_date = ymd(begin_date)) %>% 
  unite(end_date, end_yearmonth, end_day, sep = "-") %>% 
  mutate(end_date = ymd(end_date)) %>% 
  mutate(damage_property = parse_damage(damage_property),
         damage_crops = parse_damage(damage_crops),
         state = str_to_title(state)) %>% 
  select(begin_date, end_date, episode_id, state, damage_property, damage_crops, event_type) %>% 
  gather(key = damage_type, value = damage, contains("damage")) %>% 
  group_by(episode_id) %>% 
  summarize(begin_date = min(begin_date),
            end_date = max(end_date),
            state = first(state),
            event_type = paste(unique(event_type), collapse = ", "),
            damage = sum(damage)) %>% 
  arrange(desc(damage)) %>% 
  slice(1:50) %>% 
  arrange(state, begin_date) %>% 
  mutate(n = 1:n(),
         damage_text = ifelse(damage == 0, "", 
                              paste0("$", formatC(damage, format = "d", big.mark = ","))))

ggplot(ex, aes(x = begin_date, y = n, color = state)) + 
  geom_segment(aes(xend = end_date, yend = n)) + 
  geom_point(size = 0.2) + 
  geom_point(aes(x = end_date), size = 0.2) +
  theme_classic() + 
  theme(legend.position = "bottom",
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank()) + 
  geom_text(aes(label = event_type, x = begin_date - 2), 
            hjust = "right", size = 2, color = "black") + 
  geom_text(aes(label = damage_text, x = end_date + 2), 
            hjust = "left", size = 2, color = "black") +  
  labs(color = "") + 
  scale_x_date(name = "Episode date", 
               breaks = ymd(c(20150101, 20150301, 20150501, 
                                   20150701, 20150901, 20151101,
                              20160101)), 
               date_labels = "%b",
               limits = c(ymd(20141001), ymd(20160201)))

I get an error message reading:

Error in county_events_2015 %>% filter(str_to_lower(state) %in%

geanders commented 4 years ago

Ahhh. This could be something called a "namespace" issue. When you load two or more packages that have a function with the same name, then R can get confused about which version to use. This issue happens a lot with filter, because several packages have a function with that name.

One way to resolve this is to use the full package::function() notation, because that tells R which package to look in to get the function to use. Try changing filter to dplyr::filter and see if that helps here.

theresekon commented 4 years ago

Okay, thank you I will try that. I also realized that the whole error message wasn't appearing on my screen. The whole thing actually reads

Error in county_events_2015 %>% filter(str_to_lower(state) %in% as.character(state.fips$polyname)) %>% : could not find function "%>%" Error in ggplot(ex, aes(x = begin_date, y = n, color = state)) : could not find function "ggplot"

geanders commented 4 years ago

Oh, for that one, try adding:

library(ggplot2)

somewhere in the vignette before this point.