! could not find function %>%

Hi guys, so I get this issues that puzzle me.

I tried to write run code chunk in RMarkdown using ggplot2, dplyr, and ggrepel packages. Before I move my code in RMarkdown I put it in R Script, and it run smoothly. But when I tried to run it in RMarkdown they send me this message "! could not find function %>%". The code is like this:

total_industry_and_registered %>%
ggplot(aes(x=provinsi, y=jumlah_pendaftar, color=pulau, label=provinsi))+
geom_point()+
labs(title="Number of Registered Industry of Traditional Medicine by Province",
caption = "Source data: Portal Satu Data BPOM",
x="",
y="Total Industry")+
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())+
geom_text_repel(size=3)+
theme(legend.position="bottom")

Did anyone has run the same issue as me?

You need to attach the package that has %>%, i.e. write

library(dplyr)

at the beginning of the first chunk that uses it. Or the very first chunk or the document.

2 Likes

Strictly speaking %>% is a part of magrittr, which is called by dplyr. Alternatively, if you are using R version 4.1, or more recent, you can replace this with the pipe symbol |> and it does not require any extra packages.

1 Like

%>% and |> are mostly interchangeable but behave differently within the tidy dialect than they do in base. The difference lies in how the LHE (left hand expression) is carried over.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)
mtcars |> log(mpg)
#> Error in eval(expr, envir, enclos): object 'mpg' not found
mtcars %>% mutate(foo = log(mpg)) %>% head()
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb      foo
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 3.044522
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 3.044522
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 3.126761
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 3.063391
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 2.928524
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 2.895912
mtcars |> head()
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Created on 2023-07-24 with reprex v2.0.2

1 Like

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.