1

I have a ggplot inside of a renderPlot function. This code block works as expected:

output$revenue_channel <- renderPlot({
  ggplot(untrended_data(), aes(x = reorder(Channel, Revenue), y = Revenue), label = Revenue) +
    geom_bar(stat="identity", fill = "#008080", alpha = 0.6) +
    coord_flip() +
    geom_text(aes(label = scales::dollar(Revenue)), hjust= 1.2, color = "white") +
    scale_y_continuous(label = scales::label_dollar(scale = 0.001, suffix = "K")) +
    xlab("") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))
}, height = 300)

This code block as is runs and produces this chart: enter image description here

But, in my I actually have a breakdown selector where the user ca enter one of Channel, Device or UserType.

So, this line:

ggplot(untrended_data(), aes(x = reorder(Channel, Revenue), y = Revenue), label = Revenue)

Would become either this:

ggplot(untrended_data(), aes(x = reorder(Device, Revenue), y = Revenue), label = Revenue)

Or this:

ggplot(untrended_data(), aes(x = reorder(UserType, Revenue), y = Revenue), label = Revenue)

I tried just switching in input$myinput like so

ggplot(untrended_data(), aes(x = reorder(input$breakdown, Revenue), y = Revenue), label = Revenue)

But this gives an error:

Error: arguments must have same length

I then tried aes_ for aes_string():

ggplot(untrended_data(), aes_(x = reorder(input$breakdown, "Revenue"), y = "Revenue"), label = Revenue)

Discrete value supplied to continuous scale

I then tried t make sense of this page on quasi-quotation and tried:

ggplot(untrended_data(), aes(x = reorder(!! input$breakdown, Revenue), y = Revenue), label = Revenue)

Which resulted in:

Error: arguments must have same length

How can I pass input$breakdown into ggplot within renderPlot({})?

3
  • Can you try reorder(!! rlang::sym(input$breakdown), Revenue), Commented Jan 5, 2020 at 22:01
  • It works! Thank you. I was really struggling there. What does rlang::sym() do? Actually, I've struggled to really understand quosures and tidy eval Commented Jan 5, 2020 at 22:06
  • The input is a string, so we are converting to symbol and evaluating Commented Jan 5, 2020 at 22:10

1 Answer 1

1

The input$breakdown stores a string as value, and if we convert it to a symbol (using rlang::sym) and then evaluate (!!) it would work, e.g.

library(ggplot2)
v1 <- "mpg";
ggplot(mtcars, aes(x = reorder(!! rlang::sym(v1), cyl), y = cyl, label = cyl))+
     geom_bar(stat="identity", fill = "#008080", alpha = 0.6) + 
    coord_flip()

In the OP's code block, we need to use reorder(!! rlang::sym(input$breakdown), Revenue)

output$revenue_channel <- renderPlot({
  ggplot(untrended_data(), aes(x = reorder(!! rlang::sym(input$breakdown), Revenue),
          y = Revenue), label = Revenue) +
    geom_bar(stat="identity", fill = "#008080", alpha = 0.6) +
    coord_flip() +
    geom_text(aes(label = scales::dollar(Revenue)), hjust= 1.2, color = "white") +
    scale_y_continuous(label = scales::label_dollar(scale = 0.001, suffix = "K")) +
    xlab("") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))
}, height = 300)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.