1

I'm trying to use my dataframe which has a column name "release_year" as a slider input so whatever timeline I choose, let's say 1960 to 1970 I only see data from that particular timeline on my scatter plot. Right now my slider is acting pretty strange and not really doing anything except for move a few points. How can I fix this? Something like this https://shiny.rstudio.com/gallery/movie-explorer.html Do you see the year release slider? I want that exact thing.

  • Attaching images of my DataSet.
  • [df] (https://i.stack.imgur.com/GrlnO.jpg)

     structure(list(id = c(135397L, 135397L, 76341L, 76341L, 262500L, 
    140607L, 140607L, 140607L, 168259L, 168259L), budget = c(150000000L, 
    150000000L, 150000000L, 150000000L, 110000000L, 200000000L, 200000000L, 
    200000000L, 190000000L, 190000000L), revenue = c(1513528810, 
    1513528810, 378436354, 378436354, 295238201, 2068178225, 2068178225, 
    2068178225, 1506249360, 1506249360), title = structure(c(3L, 
    3L, 4L, 4L, 2L, 5L, 5L, 5L, 1L, 1L), .Label = c("Furious 7", 
    "Insurgent", "Jurassic World", "Mad Max: Fury Road", "Star Wars: The Force Awakens"
    ), class = "factor"), homepage = structure(c(2L, 2L, 3L, 3L, 
    5L, 4L, 4L, 4L, 1L, 1L), .Label = c("http://www.furious7.com/", 
    "http://www.jurassicworld.com/", "http://www.madmaxmovie.com/", 
    "http://www.starwars.com/films/star-wars-episode-vii", "http://www.thedivergentseries.movie/#insurgent"
    ), class = "factor"), director = structure(c(1L, 1L, 2L, 2L, 
    5L, 3L, 3L, 3L, 4L, 4L), .Label = c("Colin Trevorrow", "George Miller", 
    "J.J. Abrams", "James Wan", "Robert Schwentke"), class = "factor"), 
        runtime = c(124L, 124L, 120L, 120L, 119L, 136L, 136L, 136L, 
        137L, 137L), vote_average = c(6.5, 6.5, 7.1, 7.1, 6.3, 7.5, 
        7.5, 7.5, 7.3, 7.3), release_year = c(2015L, 2015L, 2015L, 
        2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L), genre = structure(c(1L, 
        2L, 1L, 2L, 2L, 1L, 2L, 4L, 1L, 3L), .Label = c("Action", 
        "Adventure", "Crime", "Fantasy"), class = "factor"), breakeven = c(1363528810, 
        1363528810, 228436354, 228436354, 185238201, 1868178225, 
        1868178225, 1868178225, 1316249360, 1316249360), AerageVotesCat = structure(c(2L, 
        2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Excellent", 
        "Good"), class = "factor")), row.names = c(NA, 10L), class = "data.frame" 
    
  • [Slider that needs to control the data displayed] (https://i.stack.imgur.com/NqhZK.jpg)

I have almost spent a week trying everything. I can't seem to figure this out problems out. I know the problem is somewhere around my condition in the reactive bracket? But being new I don't know what to pass in.

UI:
library(gifski)
library(gganimate)
library(dplyr)
library(DT)
library(shinythemes)
library(scales)
library(shiny)
library(ggplot2)
library(plotly)



df <- read.csv("C:/Users/XXX/Downloads/movie1.csv")
n_total <- nrow(df)

ui <- fluidPage(theme = shinytheme("united"),
                titlePanel("Movie browser, 1960 - 2014", windowTitle = "Movies"),   

                # Sidebar layout with a input and output definitions
                sidebarLayout(
                  # Inputs
                  sidebarPanel(
                    wellPanel(

                      # Select variable for y-axis
                      selectInput(inputId = "y", 
                                  label = h4("Y-axis:"),
                                  choices =c("Budget" ="budget", "Revenue" = "revenue", "Runtime" = "runtime", "Vote average" = "vote_average", "Year released" = "release_year", "Profit" = "breakeven"), 
                                  selected = "revenue"),

                  sliderInput("SectorTime", h4("Select a time period:"), min = 1960, max = 2015,
                                value = c(1960,2015), step = 5),


                    textInput("Director", h4("Director name contains (e.g., Miyazaki)")),
                    numericInput(inputId = "n",
                                 label = h4("Sample size:"),
                                 value = 30,
                                 min = 1, max = n_total,
                                 step = 1),


                    radioButtons(inputId = "filetype",
                                 label = "Select filetype:",
                                 choices = c("csv", "tsv"),
                                 selected = "csv"),

                    # Select variables to download
                    checkboxGroupInput(inputId = "selected_var",
                                       label = "Select variables:",
                                       choices = names(df),
                                       selected = c("title"))
                  ),

                  # Outputs
                  mainPanel(
                    tabsetPanel(
                      tabPanel(h4("PLOT"), plotlyOutput("plot"),
                                            tabPanel(h4("DATA"), DT::dataTableOutput(outputId = "moviestable", width = 500)






                  )
                )
)
SERVER:

# Define server function required to create the scatterplot
server <- function(input, output) {

  dataset <- reactive({
    df[sample(nrow(df), input$SectorTime),]
  })


  # Create scatterplot object the plotOutput function is expecting
  output$plot <- renderPlotly({
    point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)


    p <- ggplot(data = dataset(), aes_string(x = input$x, y = input$y, col = input$z)) +
      geom_point(alpha = input$alpha, size = 2, shape = 1)  +  theme_minimal() +
      ggtitle("Scatter plot between various variables") +scale_x_continuous(labels = point) + scale_y_continuous(labels = point)
    p +  theme(axis.text.x = element_text(angle = 30)) 

  })
   output$moviestable <- DT::renderDataTable({
    movies_sample <- df %>%
      sample_n(input$n) %>%
      select(title: AerageVotesCat)
    DT::datatable(data = movies_sample, 
                  options = list(pageLength = 30), 
                  rownames = FALSE)
  })

 }

# Create the Shiny app object
shinyApp(ui = ui, server = server)

This is obviously not my full code. I know the problem is somewhere here. Would appreciate your help.

  • Hasan, please do a little research on how to ask questions in a helpful way. The index page for StackOverflow's [R page](https://stackoverflow.com/questions/tagged/r) specifically says *"Use `dput()` for data and specify all non-base packages with `library()` calls. Do not embed pictures for data or code, use indented code blocks instead."*. So **please** use `dput(head(x))`, as many (including me) will not transcribe an image of data into something to test. Other refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans May 12 '19 at 05:10
  • ... but my first guess would be for you to read up on what the second argument of `sample` does. It does *not* guess that you want to sample the rows of data for a particular column to contain one or more values within it. Perhaps replace `sample(nrow(df),input$SectorTime),]` with `df[ df$yr >= input$SectorTime[1] & df$yr <= input$SectorTime[2],,drop=FALSE]`. – r2evans May 12 '19 at 05:14
  • 1
    Hi. Apologies. I'm working on to get my data sorted. This code that you mentioned gives me a blank output. So just a white screen – Hasan Tahir May 12 '19 at 05:26
  • It might have given a white screen because I didn't know what your year-column was named (now that you provided data, I see it is `$release_year`). I had used `$yr` as a placeholder. Did you change that and it still failed? – r2evans May 12 '19 at 05:43
  • 2
    Brilliant! Works! Have been stuck on this for the past week. Gracias! – Hasan Tahir May 12 '19 at 05:49

1 Answers1

0

Your issue is that you are mis-using sample: the second argument is typically the sample-size, not a field from which to filter. Given your data, it should work with:

  dataset <- reactive({
    df[input$SectorTime[1] <= df$release_year &
        df$release_year <= input$SectorTime[2]  ,,drop=FALSE]
  })

If you are already using dplyr or data.table packages in your workflow, both have a between function which simplifies the code for the above (though they are no faster at it).


Update

I have fixed much of your code and now have your "director filter" working. In the reduction, I removed things that had nothing to do with the question (extra packages, plots not needed to effect any filtering) and came up with this reduced set. (I'll leave it up to you to back-fill the removed components.)

# library(gifski)
# library(gganimate)
library(dplyr)
library(DT)
#library(shinythemes)
# library(scales)
library(shiny)
# library(ggplot2)
# library(plotly)

n_total <- nrow(df)

ui <- fluidPage(
  titlePanel("Movie browser, 1960 - 2014", windowTitle = "Movies"),   
  # Sidebar layout with a input and output definitions
  sidebarLayout(
    # Inputs
    sidebarPanel(
      wellPanel(
        sliderInput("SectorTime", h4("Select a time period:"), min = 1960, max = 2015,
                    value = c(1960,2015), step = 5),
        textInput("Director", h4("Director name contains (e.g., Miyazaki)")),
        numericInput(inputId = "n",
                     label = h4("Sample size:"),
                     value = 30,
                     min = 1, max = n_total,
                     step = 1)
      )
    ),
    # Outputs
    mainPanel(
      tabsetPanel(
        tabPanel(h4("PLOT"), #plotlyOutput("plot"),
                 tabPanel(h4("DATA"), DT::dataTableOutput(outputId = "moviestable", width = 500))
                 )
      )
    )
  )
)

server <- function(input, output) {
  dataset <- reactive({
    req(input$SectorTime, !is.null(input$Director))
    df[input$SectorTime[1] <= df$release_year &
         df$release_year <= input$SectorTime[2] &
         grepl(input$Director, df$director, ignore.case = TRUE),, drop=FALSE]
  })
  output$moviestable <- DT::renderDataTable({
    req(input$n, dataset())
    movies_sample <- dataset() %>%
      sample_n(min(input$n, n())) %>%
      select(title: AerageVotesCat)
    DT::datatable(data = movies_sample, 
                  options = list(pageLength = 30), 
                  rownames = FALSE)
  })
}

Some things you were still not doing:

  • all tables and plots must work off of the filtered data, your table was working off of the original df data
  • sampling must not exceed the size of the frame, you would happily allow the user to set input$n above the effective size of the data
  • I encourage you to use the req(...) function so that blocks to not fire before the prerequisites are stable
  • (Typo: AverageVotesCat as AerageVotesCat)

Further, I have assumed that your "director filter" is pattern-like and not an exact match (ergo the use of grepl).


<rant>

Commentary about asking questions on SO, not needed to answer this specific question:

  1. For future questions, it is important that you provide a complete working example: above, your code is missing parentheses, both in the middle and at the end. (For both your code and your dput output.) It helps to take your question and try it in a new R session and see what happens. In so doing, you'll see the unrelated problems answerers are having to deal with when trying to help you. When I try somebody else's code, if I find typos in the question code, I often dismiss the question immediately as either "they don't know what the problem really is", or "OP does not care about our time, asking us to do more than needed". The latter seems quick-to-judge, but my time is valuable to me, so I must apportion it appropriately.

  2. Further, it really helps to provide a minimal working example. In this case, the problem was related to basic filtering, so neither plots (ggplot2, plotly) nor themes (shinythemes) were necessary. Similarly, gifski, gganimate, and scales are completely unused. I will often skip questions when I see that "required packages" are not installed on my machine, if for no other reason than I don't want to install cruft I don't need. Reduce your code. If code is not immediately visible because it scrolls out of the code-pane, you should have to justify to yourself why the extra code is necessary for your question.

  3. "does not work" suggests to me that it needs more time and attention, certainly. But without direction (e.g., an error message, a warning, "missing data", something), you are asking for end-to-end debugging. Often this is not difficult, as the first warning/error that pops up is generally apparent, but often the warning/error is obvious enough (to some) that it can be resolved in a comment vice having to load all code/changes, fix the code (that was pasted incorrectly), and then try to hunt down what I believe is the cause of the asker's vague "does not work".

Remember, to get an answer, the onus is on you to provide a good/brief/complete question, not on us to work around incomplete or unnecessarily-long questions.

</rant>

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Last question how can I use my textinput "director" to filter out my data frame? Example by timing in the directors name the DF automatically updates on SHINY! – Hasan Tahir May 12 '19 at 10:00
  • `... & (df$director == "" | df$director == input$Director)` ? – r2evans May 12 '19 at 14:19
  • Something like this in the server? `output$moviestable <- DT::renderDataTable({ movies_sample <- df %>% sample_n(input$n) & (df$director == "" | df$director == input$Director) %>% select(title: AerageVotesCat) DT::datatable(data = movies_sample, options = list(pageLength = 30), rownames = FALSE)` – Hasan Tahir May 13 '19 at 00:21
  • 1
    Or do you mean something like this? both are not working though `dataset <- reactive({ df[df$release_year >= input$SectorTime[1] & df$release_year <= input$SectorTime[2] & (df$director == "" | df$director == input$Director),,drop=FALSE] })` – Hasan Tahir May 13 '19 at 00:33
  • *"both are not working"* does me **no good**. *How?* What error message are you receiving? Understand my frustration, you are giving nothing to work off of (not to mention that your code above is incomplete/incorrect, missing parens, not all of which appear to be "at the end"). – r2evans May 13 '19 at 14:56
  • thank you I honestly do appreciate all that you've done here. Somehow I'm scared to ask anything on STACKOVERFLOW till I master the art of 'asking questions' but I truly do appreciate this. My app is working all thanks to you. – Hasan Tahir May 14 '19 at 00:40
  • Asking questions "well" is not an obvious or easy trait, and I'm confident my initial foray into SO had weak questions. I don't want you to be afraid of asking questions, but it will help you get faster/better answers when you learn to refine how you ask. Good luck, and don't be discouraged. – r2evans May 14 '19 at 04:20
  • I have another question regarding almost the same thing, would you prefer I ask it here or open a new question on Stackoverflow? It's related to the same code. You're the only one who has been able to fully understand shiny, plots etc so that's why I'm asking you. – Hasan Tahir May 26 '19 at 05:50
  • It is probably better to ask a new question. Feel free to leave a link to it here as a comment, I'll try to take a look at it later. – r2evans May 26 '19 at 13:43
  • Hi. I tried my best to ask it properly https://stackoverflow.com/questions/56324204/need-help-improving-this-gif-appearance-while-it-runs-inside-shiny – Hasan Tahir May 27 '19 at 10:28