Shinydashboard Genre choice

Hi for a school project I'm building shinydashboard in which i compare the GDP in America with the 'Best picture winning movies' on IMDB. I wanted to make a rangeInput where i can select a particular time segment. But now I only get what I select and whats above. Example: when I select 1973 is get everything from 1973 until 2015(That's the end of the dataset).

I also wanted to join a selectInput where it's possible to choose multiple Genres. In my dataset the multiple genres are in 1 column and i would like it to stay that way. But the filter doesn't work because it says it has to be a logical vector instead of a character.

Does anyone know how i can fix this?

library(shiny)
library(tidyverse)
library(readxl)

Dash <- read.csv("Dashboard_DS.csv")

ui<- fluidPage(
  titlePanel("Dashboard van Floris"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("yearInput", "Years:", 1930,2015, value = 1976),
      selectInput("genreInput", "Genre", choices = c("Drama", "Crime", "History", "Thriller", "Comedy", "War", "Action", 
                  "Musical", "Romance", "Western", "Sport", "Biography", "Family", "Music", "Film-noir", "Mystery"),
      selected = "Drama", multiple = TRUE)),
  mainPanel(
    plotOutput("Correlatie"),
    tableOutput("results")
  )
))

server<- function(input, output){
  
  output$results <- renderTable({
    filtered <-
      Dash %>%
      filter(date >= input$yearInput)%>%
      filter(Genre, grepl(input$genreInput, fixed = TRUE))
    filtered
  })
  output$Correlatie <- renderPlot({
    filtered <-
      Dash %>%
      filter(date >= input$yearInput)
    ggplot(filtered, aes(x = date, y = Rating,)) + geom_smooth()
  })
  
}

shinyApp(ui = ui, server = server)

Schermafbeelding 2020-10-21 om 13.37.13

Since your working on a project, I'll start with a couple of quick questions, hopefully get you towards fixing your problem yourself.

What range do you expect to cover when the app starts?

in my dataset i have data from 1930 till 2015. And i want to make it possible to make your own scope within these dates

Yep, that was my understanding. But I want to know what range you expect to cover on app startup?

oh excuse me for misunderstanding, i think 1934 till 1987 will be fine!

Ok, so where do you specify this in the app?

inside de UI, sliderInput and i have tried that but it only gives me the two dates i select instead of everything in between

In the app, you have specified 1 value. For a range you have to specify both:

image

Also note I use the sep argument here. Check that out too - it will improve presentation

If you have fixed that you might find the filter hasn't worked as expected.
This is because input$yearInput now contains 2 values, so when you say date >= input$yearInput the data is not filtered using both dates, so all values greater than eg 1987 are returned.
You will need to add an extra filter condition.

yes i've fixed the range. What sort of filter doe i need to add?

Well, it's your project, right?
input is a list one element of which is a vector called yearInput.
So, you have to filter on each separate value of yearInput.

If you're not sure what I mean how, try this. How would you access (ie subset - [) each of these two values below

my_list <- list(zz = c(10, 20))

My man,

This is why i asked for help on this website because i don't know how.

And, that's why I am trying to help you understand. I could stop if you like?

It appears you have some understanding of these basics already, since you are already using them, but an excellent resource for learning the basics is

In this case, you can place more than one filter condition in a filter function, and you need to specify what condition you want to meet for each value of yearInput.

To subset fro variables use $ [[ or [ so: input$yearInput[1] can be tested against a condition, just as you already did with filter(date >= input$year)

Just in case you have tired of trying to learn this in what basically amounts to free 1 to 1 advice, I shall apologize for assuming you wanted to learn it, and leave you with two other nuggets.

  1. I'll also say that you should expect a problem with your use of grepl
    • this one is easy to resolve just by reading ?grepl.
  2. As it stands, you create a the same data set called filtered in two places: in renderText and renderPlot.
    • That means that to fix your filter you had to change it in two different places. A recipe for disaster.
    • There is a way to create your filtered data in one place and pass it into each render function. See ?reactive

You are almost there though :smiley:

I am very thankful for you trying to make me understand it and believe me, i want to. But i learn the best from seeing examples and remembering. Now it makes complete sense why i haven't got the range fixed. Ps the date scope is fixed

Excellent, well done, glad you fixed it. The following is my thoughts on why I took this approach to answering, which I blurred out just because I don't want to force my thoughts onto you or be preachy

While I will agree that you will get results faster from seeing and copying examples, I contend you will achieve a deeper and more thorough understanding from fulling interrogating problems and thinking them through.

I do, however, acknowledge that I am not your teacher, and have no right to expect you to treat me as such - I just thought it might be fun to tease the problem apart a bit, without actually even going too deeply into the weeds.

Finally, I reckon you will rarely ever make this type of mistake again, any any of a wide range of similar instances

Thanks for helping me with the first problem. And normally i would've been glad with the way you wanted to teach me this, but is was very irritated by the fact it didn't work. So that's why is reacted this way.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.