TODAY learn how to bring Drag & Drop to Dash with Dashboard Engine. 🌠 Sign up for the live event!

"Argument "p" is missing, with no default" error when using add_pie()

I have this frequency table displaying the answers of a survey question and their frequencies.

x freq
1 0 23
2 1 87
3 97 1
4 98 7

I have multiple of that kind (one for each question) and I want to plot them as an all-in-one facet plot which is why I’m using the following code:

fig <- plotly::plot_ly()
fig <- plotly::add_pie(data = df_count, labels = ~x, values = ~freq)

This results in the following error:
“Error in add_trace_classed(p, class = “plotly_pie”, values = values, labels = labels, :
argument “p” is missing, with no default”

Everything works fine when I try to generate the plot as follows:

fig <- plotly::plot_ly(data = df_count, labels = ~x, values = ~freq, type='pie')

In other forum threads similar errors occured when two packages were loaded which were sharing a common function. But since I’m writing a package, I’m already specifying all external functions that I use with the [pckg]::[fun] syntax…

Thx for your help!

That is because argument “p” is missing :)…
add_pie needs to “know” to which plotly object it should add a trace - see ?add_pie

library(plotly)

df_count <- data.frame(x = c(0L, 1L, 97L, 98L), freq = c(23L, 87L, 1L, 7L))
fig <- plotly::plot_ly()
fig <- plotly::add_pie(p = fig, data = df_count, labels = ~x, values = ~freq)
fig

Oh wow, not my brightest moment! I totally overlooked that I didn’t pipe the trace…
Thanks a lot!