Steffie Szegedi
Steffie Szegedi

Reputation: 23

Multiple group_by shiny app making a plot

\

I'm a really beginner in R Shiny.

I have a similar problem as at the link below. multiple group_by in shiny app

Instead of making a table which worked out/I managed by following the instructions in the link above.

I would like to make a plot, preferably with hchart. In which i would to switch the information because of the group by. The difficult part / or the thing that doesn't work is putting the group_by on the x-axis.


## hier de tabel versie
df2 <- readRDS("Data.rds")


library(shiny)
library(DT)
library(dplyr)

ui <- fluidPage(

  titlePanel("Dashboard"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      DT::dataTableOutput("summary")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({
    data <- df2 
    data
  })




  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>% 
      dplyr::arrange(desc(aantal)) 
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })



}


shinyApp(ui, server)



The above code works, but i tried to make a plot like this:

df2 <- readRDS("Data.rds")


library(shiny)
library(highcharter) 
library(dplyr)

ui <- fluidPage(

  titlePanel("Dashboard"),

  sidebarLayout(
    sidebarPanel(

      uiOutput("groups")

    ),


    mainPanel(

      highchartOutput("plotje")
    )
  )
)


server <- function(input, output) {
  mydata <- reactive({
    data <- df2  
    data
  })




  output$groups <- renderUI({
    df <- mydata()
    selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
  })




  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>% 
      dplyr::arrange(desc(aantal))
  })

  output$plotje <- renderHighchart({
    data <- summary_data()
    hchart(data, "column", hcaes(x = "grouper" , y = aantal)) # --> de plot zelf komt in het output deel van de UI
  })



}


shinyApp(ui, server)





Could someone help me out?!

Thanks in advance!

Kind regards,

Steffie

Upvotes: 1

Views: 729

Answers (1)

GGamba
GGamba

Reputation: 13680

You have the grouper column in the input$grouper var. It's just a matter of unquoting it.

The line hchart(data, "column", hcaes(x = "grouper" , y = aantal)) should be: hchart(data, "column", hcaes(x = !!input$grouper , y = aantal))

Full example (with iris data as you didn't provide an example of your own data):

library(shiny)
library(DT)
library(highcharter)
library(dplyr)

ui <- fluidPage(titlePanel("Dashboard"),

                sidebarLayout(
                  sidebarPanel(uiOutput("groups")),

                  mainPanel(DT::dataTableOutput("summary"),
                            highchartOutput("plot"))
                ))


server <- function(input, output) {
  mydata <- reactive({
    iris
  })

  output$groups <- renderUI({
    df <- mydata()
    selectInput(
      inputId = "grouper",
      label = "Group variable",
      choices = c("Petal.Length", "Species"),
      selected = "Species"
    )
  })

  summary_data <- reactive({
    req(input$grouper)
    mydata() %>%
      dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
      dplyr::summarise(aantal = n()) %>%
      dplyr::arrange(desc(aantal))
  })

  output$summary <- DT::renderDataTable({
    DT::datatable(summary_data())
  })

  output$plot <- renderHighchart({
    req(input$grouper)
    data <- summary_data()
    hchart(data, "column", hcaes(x = !!input$grouper, y = aantal))
  })

}

shinyApp(ui, server)

screencapture of working shiny app

Upvotes: 1

Related Questions