#r #shiny
#r #блестящий
Вопрос:
Я пытаюсь сделать так, чтобы мое блестящее приложение реагировало на мой набор данных csv. Я хочу, чтобы моя таблица данных и мои графики реагировали на фильтр, но по какой-то причине, когда я запускаю свой код, все, что я вижу, это то, что в таблице «Нет доступных данных». у меня есть 3 вкладки: одна для таблицы данных со всеми наблюдениями (я хочу, чтобы эта таблица реагировала на выбранные фильтры, на других вкладках есть график geom_line, где я использую 2 разные переменные в y-as (рейтинг IMDB и $).
Это код, который я использовал.
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)
Комментарии:
1. Попробуйте использовать
req()
для всех вашихinput$abc
переменных в фильтрах.2. это тоже не работает.
Ответ №1:
Возможно, это то, что вы ищете.
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()
})
}