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.