A thread in which my students will post their first reprex's

Here is my question:

library(gapminder)

gapminder %>%
  filter(year == 1997) %>%
  group_by(continent) %>%
  summarise(avg_pop = mean(pop)) %>%
  gt()
#> Error in gapminder %>% filter(year == 1997) %>% group_by(continent) %>% : could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

Whoops! Looks like you forgot to include your packages in this reprex. I think you'll need dplyr and gapminder)

mtcars %>% 
  ggplot(aes(x = cyl, y = mpg)) %>% 
  geom_point()
#> Error in mtcars %>% ggplot(aes(x = cyl, y = mpg)) %>% geom_point(): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

gapminder %>%
  glimpse()
#> Error in gapminder %>% glimpse(): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

x <- gapminder %>%
  filter(continent == "Asia") %>%
  group_by(country)
#> Error in gapminder %>% filter(continent == "Asia") %>% group_by(country): could not find function "%>%"

Here's our question:

gapminder %>%
  ggplot(aes(x = lifeExp, y = population)) +
  geom_point()
#> Error in gapminder %>% ggplot(aes(x = lifeExp, y = population)): could not find function "%>%"

Here is my question:

cars %>% 
  filter(speed = "4")
#> Error in cars %>% filter(speed = "4"): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

# Come up with a good mistake

mtcars %>% 
  group_by(mtcars$mpg) %>% 
  count()
#> Error in mtcars %>% group_by(mtcars$mpg) %>% count(): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

library(reprex)
#> Warning: package 'reprex' was built under R version 3.5.3
library(gapminder)
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.5.3

gapminder %>%
ggplot(aes(x = lifeExp, y = population)) +
  geom_point()
#> Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
#> Error: Aesthetics must be either length 1 or the same as the data (1704): y

Created on 2019-04-16 by the reprex package (v0.2.1)

??

library(reprex) 
#> Warning: package 'reprex' was built under R version 3.5.3
library(gapminder)
#> Warning: package 'gapminder' was built under R version 3.5.2
library(tidyverse) 
#> Warning: package 'tidyverse' was built under R version 3.5.2
#> Error: package or namespace load failed for 'tidyverse' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
#>  there is no package called 'purrr'
gapminder %>% 
  ggplot(aes(x = lifeExp, y = population)) +
  geom_point()
#> Error in gapminder %>% ggplot(aes(x = lifeExp, y = population)): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

Here is my question:

mtcars %>% filter(cyl == 4 & hp > 105)
#> Error in mtcars %>% filter(cyl == 4 & hp > 105): could not find function "%>%"

Created on 2019-04-16 by the reprex package (v0.2.1)

My test question

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
library(gapminder)
library(reprex)
#> Warning: package 'reprex' was built under R version 3.5.3

gapminder %>% ggplot(x = year, y = gdp) + geom_point()
#> Error: geom_point requires the following missing aesthetics: x, y

Created on 2019-04-16 by the reprex package (v0.2.1)

Hi there! Your x and y axis variables need to be inside an aes() call. These variables are setting the aesthetics for your plot, which is where that error message comes from. Try this:

library(tidyverse)
library(gapminder)

gapminder %>% 
  ggplot(aes(x = year, y = gdpPercap)) + geom_point()

Created on 2019-04-16 by the reprex package (v0.2.1)

1 Like

%>% is a function, originally designed in the {Magrittr} package and re-exported in the {dplyr} package.

Long story short, seems you need to load the {dplyr} package before running this code.

library(dplyr)

Load dplyr package first

library (dplyr)

Tidyverse package didn't load, rather it failed to load. Reinstall and try load again

It looks like

  1. you need to make sure the magrittr pipe is installed.
    That usually means installing a package and then using a library call to make it available.
    I usually install.packages(tidyverse)
    then library(tidyverse)
install.packages('tidyverse')
library(tidyverse)

You could also use dplyr or the magrittr package itself.

  1. You want to set wait equal to max(waiting).
    This requires one equals sign.
    When you use two equal signs, you are (effectively) asking a question - are these two things equal? - in a logical expression.
    Try this instead
faithful %>%
  summarize(wait=max(waiting))
1 Like

ggplot() function requires 2 arguments: data and mapping. You provided the gapminder data, but when you specified mapping, you forgot to wrap it around aes(). Following should work:

gapminder %>% ggplot(aes(x = year, y = gdp)) + geom_point()

Great idea @dkane. Just to understand do the students want answers to these question or suggestions on how to improve their reprex ?

Sorry! They don't need answers to these questions. The purpose was to force them to ask a question at least once.

3 Likes