Hello,
I'm trying to combine a dplyr::select()
and a dplyr::filter()
to allow reactive columns selection and obs filtering with shiny.
When I tryed separately an app that select columns only (below: App 1 : select()
), it works good. Same thing for the app that filter only (below: App 2 : filter()
).
But, when combining the two (below: App 3 : select() %>% filter()
), I got the error: get(input$x)` must evaluate to column positions or names, not a function.
Note that I would like to filter the columns that have been selected before.
Ps : data are from the "movies" file (http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata)
Does somebody encountred this before?
Any idea about how to handle this?
Many thanks
Yacine
App 1 : select()
if (interactive()){
# Load data
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
ui3 <- shinyUI(
dashboardPage(
dashboardHeader(title = "Data Viewer"),
dashboardSidebar(
# select()
selectInput(inputId = "x",
label = "Choisir ici vos colonnes : ",
choices = c("title", "studio","best_pic_nom",
"top200_box", "title_type",
"critics_rating", "director",
"genre", "best_actor_win", "audience_rating",
"best_actress_win","best_dir_win", "imdb_num_votes",
"thtr_rel_date", "dvd_rel_year",
"thtr_rel_year","dvd_rel_month", "critics_score",
"runtime", "thtr_rel_month", "dvd_rel_day",
"mpaa_rating", "thtr_rel_day", "imdb_rating", "audience_score"),
selected = "title",
multiple = TRUE)
),
dashboardBody(
fluidPage(
box(DT::dataTableOutput(outputId = "T"),
title = "Les colonnes sélectionnées")
)
)
)
)
server3 <- shinyServer(function(input, output, session) {
output$T <- DT::renderDataTable({
selTable <- movies %>%
select(input$x)
DT::datatable(data = selTable,
options = list(pageLength = 5,
row.names = TRUE))
})
})
shinyApp(ui = ui3, server=server3)
}
App 2 : filter()
if (interactive()){
# Load data
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
ui2 <- shinyUI(
dashboardPage(
dashboardHeader(title = "Data Viewer"),
dashboardSidebar(
# Critère du filtre
selectInput(inputId = "y",
label = "Choisir ici le critère du filtre : ",
choices = c("runtime", "thtr_rel_day", "dvd_rel_year","critics_score", "audience_score"),
selected = "runtime"),
# Slider
numericInput(inputId = "numValue",
label = "Choisir une valeur numérique pour le filtre",
value ="500",
min = 1,
max = 1000,
step = 50,
width = '100%')
),
dashboardBody(
fluidPage(
box(DT::dataTableOutput(outputId = "T"), title = "Les données filtrées")
)
)
)
)
server2 <- shinyServer(function(input, output){
output$T <- DT::renderDataTable({
filteredTable <- movies %>%
dplyr::filter(get(input$y) < input$numValue)
DT::datatable(data = filteredTable,
options = list(pageLength = 5,
rownames = TRUE))
})
})
shinyApp(ui = ui2, server = server2)
}
App 3 : select() %>% filter()
if (interactive()){
# Load data
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
ui <- shinyUI(
dashboardPage(
dashboardHeader(title = "Data Viewer"),
dashboardSidebar(
# select()
selectInput(inputId = "x",
label = "Choisir ici vos colonnes : ",
choices = c("title", "studio","best_pic_nom",
"top200_box", "title_type",
"critics_rating", "director",
"genre", "best_actor_win", "audience_rating",
"best_actress_win","best_dir_win", "imdb_num_votes",
"thtr_rel_date", "dvd_rel_year",
"thtr_rel_year","dvd_rel_month", "critics_score",
"runtime", "thtr_rel_month", "dvd_rel_day",
"mpaa_rating", "thtr_rel_day", "imdb_rating", "audience_score"),
selected = "title",
multiple = TRUE),
# Critère du filtre
selectInput(inputId = "y",
label = "Choisir ici le critère du filtre : ",
choices = c("runtime", "thtr_rel_day", "dvd_rel_year","critics_score", "audience_score"),
selected = "runtime"),
# Slider
numericInput(inputId = "numValue",
label = "Choisir une valeur numérique pour le filtre",
value ="500",
min = 1,
max = 1000,
step = 50,
width = '100%')
),
dashboardBody(
fluidPage(
box(DT::dataTableOutput(outputId = "T"),
title = "Les données filtrées")
)
)
)
)
server <- shinyServer(function(input, output){
output$T <- DT::renderDataTable({
filteredTable <- movies %>%
# Select
dplyr::select(get(input$x)) %>%
# Filter
dplyr::filter(get(input$y) < input$numValue)
DT::datatable(data = filteredTable,
options = list(pageLength = 5,
rownames = TRUE))
})
})
shinyApp(ui, server)
}