Julian Brouwer
Julian Brouwer

Reputation: 15

Not returning the plot and table in R shiny

I'm working on a r shiny project of getting a dashboard of the premier league goals of 2019/2020. I've scraped my data and with the following code trying to make a nice dashboard. The problem is that my "plot" and "table" are not returning in the app. It only gives the columns and not the content of the table and the plot. Someone that can help me out or sees what i'm doing wrong?

library(httr)
library(tidyverse)
library(dplyr)
library(rvest)
library(ggplot2)
library(shiny)
library(shinythemes)

Top_goalscorers <- Top_goalscorers

dput(Top_goalscorers)

ui <- fluidPage(
  titlePanel("Premier league goals 2019/2020"),
  sidebarLayout(
    sidebarPanel(
      selectInput("typeInput", "Club",
                  choices = c("Arsenal", "Liverpool", "Chelsea", "Leicester City", "Southampton","Manchester City", 
                              "Tottenham Hotspur", "Manchester United","Wolverhampton Wanderers","Chelsea","Burnley",
                              "Everton","Norwich City","Watford","West Ham United","Brighton & Hove Albion",
                              "Crystal Palace","Aston Villa","AFC Bournemouth","Sheffield United","Newcastle United"), 
                  selected = "Arsenal"),
      sliderInput("goalInput", "Goals", 0,30, c(0,30)),
      radioButtons("spelerInput", "Speler",
                   choices = c("Jamie Vardy", "Harry Kane"), 
                   selected = "Jamie Vardy")
    ),
    mainPanel(
      tabsetPanel(
                  tabPanel ("plot", plotOutput("myplot")),
                  tabPanel ("tabel", tableOutput("resultaten"))
    )
    )
  )
)

server <- function(input, output) {
  
  output$resultaten <- renderTable({
    filtered <-
      Top_goalscorers %>%
      filter(club == input$typeInput,
             goals >= input$goalInput,
             speler == input$spelerInput)
    filtered
  })
  output$myplot <- renderPlot({
    filtered <-
      Top_goalscorers %>%
      filter(club == input$typeInput,
             goals >= input$goalInput[1],
             goals <= input$goalInput[2],
             speler == input$spelerInput
      )
    ggplot(filtered, aes(x=goals, y=club, fill=goals_eerste_helft)) +
      geom_point(size=2, shape=23)
    
    
  })
  
}

shinyApp(ui = ui, server = server)

Here the structure of the dataset:

Dataset Top_goalscorers

Upvotes: 0

Views: 59

Answers (1)

YBS
YBS

Reputation: 21349

Create filtered data as a reactive object once, and use it in table and plot. Try this

server <- function(input, output, session) {
  filtered <- reactive({
    req(input$typeInput,input$goalInput[1],input$goalInput[2],input$spelerInput)
    Top_goalscorers %>%
    filter(club == input$typeInput,
           goals >= input$goalInput[1],
           goals <= input$goalInput[2],
           speler == input$spelerInput
    )})
  
  output$resultaten <- renderTable({
    filtered()
  })
  
  output$myplot <- renderPlot({
    ggplot(filtered(), aes(x=goals, y=club, fill=goals_eerste_helft)) +
      geom_point(size=2, shape=23)
  })
  
}

Upvotes: 1

Related Questions