My situation is the following that I import a csv dataset from IMDB into my small Shiny app. It contains in the respective columns the values for runtime, IMDB-Rating, gross, votes, Meta-Score Rating etc...
I have in the UI-section a selectInput where the user can select the available columns as mentioned above.
In the Server-Section (before my output$graph <- renderPlot(...)
) I want to describe/implement that the outputPlot changes depending on the selected column. I tried to solve it using observeEvent like this...
observeEvent(~get(input$type) == "Meta-Score", {ratings <- moviesdb$Meta_score})
observeEvent(~get(input$type) == "IMDB-Rating", {ratings <- moviesdb$IMDB_Rating})
etc...
using the global operator <<-
and even using some type of switch-case logic:
x <- reactive(~get(input$type))
y <- NULL
test <- switch(c(x),
"Meta-Score" = {(ratings <- moviesdb$Meta_score)},
"IMDB-Rating" = {(ratings <- moviesdb$IMDB_Rating)},
"Film-Laufzeit" = {(ratings <- moviesdb$Runtime)})
I really appreciate any help/hints/tips or even mock-up code on how to solve this problem! Since yesterday I am googling but I just dont find any solution or question which seems to be similar to what I want to achieve.
Example Code:
library(shiny)
library(ggplot2)
moviesdb <- read.csv("imdb_top_1000.csv", header=TRUE)
moviesdb <- na.omit(moviesdb) #remove NA from data set
ui <- fluidPage(
selectInput(
inputId = "type",
label = "choose type",
choices = c("Meta-Score", "Votes"),
selected = "Meta-Score",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
sliderInput(inputId = "num", label = "sample size", value = 500, min = 0, max = 800, step = 10),
plotOutput("distribution"),
)
server <- function(input, output) {
ratings <- moviesdb$Meta_score
# x <- reactive(~get(input$type))
# y <- NULL
# test <- switch(c(x),
# "Meta-Score" = {(ratings <- moviesdb$Meta_score)},
# "Votes" = {(ratings <- moviesdb$No_of_Votes)},
# y
# observeEvent(ratings, {
# print(paste0("You have chosen: ", ratings))
# })
output$distribution <- renderPlot(
{
filmratingssample <- (sample(ratings, input$num, replace = FALSE, prob = NULL))
mean_films <- mean(filmratingssample)
films <- data.frame(dnorm(filmratingssample, mean = mean_films, sd = 1, log = FALSE))
ggplot(data = films) + aes(x=filmratingssample) +
geom_histogram(aes(y=..density..), col = "white", binwidth=1)
})
}
shinyApp(ui = ui, server = server)
Dataset from here
Data looks as followed:
Year Runtime Meta_score No_of_Votes Gross
1994 142 min 85,00 2343110 28,341,469
1972 175 min 84,00 1620367 134,966,411
2008 152 min 75,00 2303232 534,858,444