简体   繁体   中英

How can i make my shiny app reactive to my filters by linking my csv file

I'm trying to make my shiny app reactive to my csv dataset. I want my datatable and my plots to be reactive to the filter, but for some reason when i run my code all i see is that there is "No data available in the table". i have 3 tabs: one for the datatable with all the observations (i want to make this table reactive to the chosen filters, the other tabs have a geom_line plot where i use 2 different variables on the y-as (IMDB rating and $).

This is the code that i used.

library(shiny)
library(tidyverse)
library(shinythemes)

#import the dataset
marvel_dc_movies <- read.csv("marvel_dc_movies.csv", stringsAsFactors = FALSE)
marvel_dc_movies$X <- NULL 

ui <- fluidPage(theme = shinytheme("lumen"),
  #name the page
  titlePanel("Marvel Studios VS. DC Films"),
  sidebarLayout(
    sidebarPanel(
      #create a multiple select input for the release date data
      selectInput(inputId = "release_dateInput", 
                  label = "Select the release year.", 
                  choices = c("Unreleased", "2023", "2022", "2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002", "2000", "1998", "1997", "1995", "1994", "1993", "1992", "1990", "1989", "1987", "1986", "1984", "1983", "1982", "1980", "1978", "1966", "1954", "1951"),
                  selected = c("2019"), 
                  multiple = TRUE),
      #create a multiple select input for all the genres
      selectInput(inputId = "genreInput", 
                  label = "Select the genre.",
                  choices = c("Action", "Adventure", "Animation", "Comedy", "Crime", "Drama", "Family", "Fantasy", "Horror", "Mystery", "Romance", "Sci-Fi", "Short", "Thriller"),
                  selected = c("Action", "Adventure", "Animation"), 
                  multiple = TRUE),
      #create a slider for the runtime 
      sliderInput(inputId = "run_time_in_minInput", 
                  label = "Run time in minutes.", 
                  min = 0, 
                  max = 200,
                  value = c(18, 181)),
      #create radiobuttons for the entertainment company 
      checkboxGroupInput(inputId = "entertainment_companyInput", 
                         label = "Select the entertainment company.",
                         choices = c("Marvel Studios", "DC Films"),
                         selected = c("Marvel Studios", "DC Films")),
      #insert an image 
      img(src = "mvdc.jpg", height = 100, width = 230)
    ),
    #create different tabs
    mainPanel(tabsetPanel(type = "tabs",
                          tabPanel("Movie list", dataTableOutput("table")),
                          tabPanel("Marvel VS. DC in $", plotOutput("plot1")),
                          tabPanel("Marvel VS. DC in IMDB ratings", plotOutput("plot2"))
    )
    )
  )
)
server <- function(input, output) {
  output$table <- renderDataTable({
      filtered <- 
        marvel_dc_movies %>%
        filter(release_date %in% input$release_dateInput,
               genre_1 %in% input$genreInput,
               genre_2 %in% input$genreInput,
               genre_3 %in% input$genreInput,
               run_time_in_min >= input$run_time_in_minInput[1],
               run_time_in_min <= input$run_time_in_minInput[2], 
               entertainment_company == input$entertainment_companyInput
               )      
      filtered
      })
  output$plot1 <- renderPlot({(marvel_dc_movies)
    filtered <-
      marvel_dc_movies %>%
      filter(release_date %in% input$release_dateInput,
             genre_1 %in% input$genreInput,
             genre_2 %in% input$genreInput,
             genre_3 %in% input$genreInput,
             run_time_in_min >= input$run_time_in_minInput[1],
             run_time_in_min <= input$run_time_in_minInput[2],
             entertainment_company == input$entertainment_companyInput
      )
    ggplot(filtered, aes(x = release_date, y = gross_x1.000.000, fill = entertainment_company)) +
      geom_line()
  })
  output$plot2 <- renderPlot({(marvel_dc_movies)
    filtered <-
      marvel_dc_movies %>%
      filter(release_date %in% input$release_dateInput,
             genre_1 %in% input$genreInput,
             genre_2 %in% input$genreInput,
             genre_3 %in% input$genreInput,
             run_time_in_min >= input$run_time_in_minInput[1],
             run_time_in_min <= input$run_time_in_minInput[2],
             entertainment_company == input$entertainment_companyInput
      )
    ggplot(filtered, aes(x = release_date, y = rating, fill = entertainment_company)) +
      geom_line()
  })
  }
shinyApp(ui = ui, server = server)

Perhaps this is what you are looking for.

server <- function(input, output) {
  filtered <- reactive({
    req(input$release_dateInput,input$genreInput,input$run_time_in_minInput,input$entertainment_companyInput)
    marvel_dc_movies %>%
      filter(release_date %in% req(input$release_dateInput),
             genre_1 %in% input$genreInput,
             genre_2 %in% input$genreInput,
             genre_3 %in% input$genreInput,
             run_time_in_min >= input$run_time_in_minInput[1],
             run_time_in_min <= input$run_time_in_minInput[2], 
             entertainment_company == input$entertainment_companyInput
      )
  })
  
  output$table <- renderDataTable({
    filtered()
  })
  
  output$plot1 <- renderPlot({
    ggplot(filtered(), aes(x = release_date, y = gross_x1.000.000, fill = entertainment_company)) +
      geom_line()
  })
  
  output$plot2 <- renderPlot({
    ggplot(filtered(), aes(x = release_date, y = rating, fill = entertainment_company)) +
      geom_line()
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM