0
votes

I am learning some shiny in order to do a dashboard. I have an idea. I want to create a dashboard that select from an selectinput a variable, group by such variable and plot a barplot or histogram of the total of that variable.

I have generated a sample dataset to generate what I need, however I can´t get what I need.

The UI code is the next one:

library(shiny)

shinyUI(fluidPage(
    titlePanel("Demo dashboard"),

  sidebarLayout(
    sidebarPanel(
       selectInput("variable",
                   "group by",
                   choices = c("City","Country")
                    )
    ),

    mainPanel(
       plotOutput("distPlot")
    )
  )
))

The server code is the next one, Here I aggregate by the variable that is the input and plot the total

library(shiny)
library(dplyr)


shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    sample<-tbl_df(data.frame(c("City1","City2","City3","City1","City2","City3","City2","City3"),
                              c("A","B","C","D","D","A","A","B"),
                              c(12,14,15,12,12,14,8,10)))
    colnames(sample)<-c("City","Country","Amount")
    df1<-sample%>%group_by(input$variable)%>%
    summarise(total=sum(Amount))
    sample%>%group_by(input$variable)%>%summarise(total=sum(Amount))

    x<- df1$total 
    hist(x)
  })

})

A screen capture of my result is the next:

enter image description here

however this is not the expected result. I can´t get the histogram required

1
Hi! You question is a bit vague right now: "I can´t get what I need." is not a lot of information. Could you for instance take a screenshot and annotate what is wrong ?Gabriel Devillers

1 Answers

0
votes

The problem is your usage of dplyr:

Your original code doesn't evaluate input$variable to group by city, rather tries to group by a non-existing column called `input$variable`:

sample %>%
      group_by(input$variable) %>%
      summarise(total=sum(Amount))

Result:

# # A tibble: 1 x 2
# `input$variable` total
# <chr>            <dbl>
#   1 City                97

You can check this yourself easily by adding either a print statement after the statement (e.g.: print(df1)) or adding a browser() before the statement.

This behaviour is because dplyr uses non-standard-evaluation by default. You can read up more about that here.

To use standard (programmable) evaluation you need to unquote input$variable so that the value is passed to dplyr. In the current version you can do that using a combination of !! and sym.

Example:

sample %>%
    group_by(!!sym(input$variable)) %>%
    summarise(total=sum(Amount))

Result:

# # A tibble: 3 x 2
# City  total
# <fct> <dbl>
# 1 City1    24
# 2 City2    34
# 3 City3    39

Histogram: result

Edit: Some more explanation: group_by doesn’t evaluate its input, but rather it quotes it: That's why you're getting `input$variable` as a column name.

On the other hand: the sym function turns the actual value of input$variable into a symbol, then !! can be used to remove the quoting:

What works in dplyr is if you don't have quotes around the input, so: group_by(City)

Let's see what happens step by step:

  1. sym(input$variable) returns "City". group_by("City") would still not work because the input has quoting around it!
  2. That's why we need to use !!: !! sym(input$variable) returns City without quotes. So the expression evaluates to group_by(City), and thus will work as expected.