Reputation: 29
I have created a datatable with renderDT
and reactive functions, in order to change the table with selectInputs
. Now I want to plot a geom_line
graphic with the datatable created and have a reactive dashboard that have to change with the same selectInputs
, but I don't know how. If you have some ideas please share. In addition I want to have no default selection in my selectInputs
.
Here is the code:
library(shiny)
library(DT)
library(dplyr)
library(shinyWidgets)
library(tidyverse)
library(lubridate)
library(timetk)
library(ggplot2)
library(rJava)
library(xlsx)
library(graphics)
data_1 <-mtcars
# User Interface
ui <- fluidPage(
titlePanel("My dashboard"),
sidebarLayout(
sidebarPanel(
selectInput('filter_gear', 'gear', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL),
selectInput('filter_carb', 'carb', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL),
selectInput('filter_cyl', 'cyl', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL)
),
mainPanel(
tabsetPanel(id="mydash", type= "tabs",
tabPanel("Plot", plotOutput("fig"), plotOutput("fig2"), plotOutput("fig3")),
tabPanel("Tables", p(DTOutput('databasedf')))
)
)
)
)
server <- function(input, output, session) {
filterdf <- reactive({
filterdf <- data_1
filterdf <- droplevels.data.frame(filterdf)
return(filterdf)
})
filtergear <- reactive({
unique(as.character(filterdf()$gear))
})
observeEvent(filtergear(), {
updateSelectInput(session,
"filter_gear",
choices = filtergear(),
selected = sort(filtergear()))
})
# # Subset dynamically the previous reactive filter #
datasub1 <- reactive({
data_1[data_1$gear %in% input$filter_gear,]
})
filtercarb <- reactive({
unique(as.character(datasub1()$carb))
})
observeEvent(filtercarb(), {
updateSelectInput(session,
"filter_carb",
choices = sort(filtercarb()),
selected = sort(filtercarb()))
})
# Subset dynamically the previous reactive filter #
datasub2 <- reactive({
# browser()
data_1[data_1$carb %in% input$filter_carb,]
})
filtercyl <- reactive({
unique(as.character(datasub2()$cyl))
})
observeEvent(filtercyl(), {
updateSelectInput(session,
"filter_cyl",
choices = sort(filtercyl()),
selected = sort(filtercyl()))
})
output$databasedf <- DT::renderDT({
# Subset for plotly reactivity
Filter1 <- droplevels.data.frame(data_1)
Filter2 <- filter(Filter1,
Filter1$gear %in% input$filter_gear,
Filter1$carb %in% input$filter_carb,
Filter1$cyl %in% input$filter_cyl)
# Plot
datatable(Filter2,
filter="none",
selection="none",
escape=FALSE,
rownames = FALSE,
# colnames = c("", ""),
autoHideNavigation = TRUE,
style = 'bootstrap4',
options = list(searching = FALSE, # remove search option
ordering = FALSE, # remove sort option
paging = FALSE, # remove paging
info = FALSE # remove bottom information
)) %>%
formatStyle(columns = 1, fontWeight = 'bold', `text-align` = 'left') # text to bold and lign left in first column
})
output$fig <-renderPlot({
plt <- addDataFrame(Filter2, sheet, col.names=TRUE, row.names=TRUE, startRow=1, startColumn=1)
fig <- plt %>% ggplot() + geom_line(aes(x=hp, y=mean(mpg), color=am)) })
}
shinyApp(ui, server)
Upvotes: 1
Views: 484
Reputation: 7106
I defined Filter2 outside renderDT
to allow renderPlot
find it. I left plt
commented. I tried to leave the app without major changes. The req
s before ggplot
are to avoid an error at the start (because the inputs are not updated yet with the select options).
library(shiny)
library(DT)
library(dplyr)
library(shinyWidgets)
library(tidyverse)
library(lubridate)
library(timetk)
library(ggplot2)
library(rJava)
library(xlsx)
library(graphics)
data_1 <- mtcars
# User Interface
ui <- fluidPage(
titlePanel("My dashboard"),
sidebarLayout(
sidebarPanel(
selectInput('filter_gear', 'gear', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL),
selectInput('filter_carb', 'carb', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL),
selectInput('filter_cyl', 'cyl', choices = NULL, selected = NULL, multiple = FALSE, selectize = FALSE, width = NULL, size = NULL)
),
mainPanel(
tabsetPanel(id="mydash", type= "tabs",
tabPanel("Plot", plotOutput("fig"), plotOutput("fig2"), plotOutput("fig3")),
tabPanel("Tables", p(DTOutput('databasedf')))
)
)
)
)
server <- function(input, output, session) {
filterdf <- reactive({
filterdf <- data_1
filterdf <- droplevels.data.frame(filterdf)
return(filterdf)
})
filtergear <- reactive({
unique(as.character(filterdf()$gear))
})
observeEvent(filtergear(), {
updateSelectInput(session,
"filter_gear",
choices = filtergear(),
selected = sort(filtergear()))
})
# # Subset dynamically the previous reactive filter #
datasub1 <- reactive({
data_1[data_1$gear %in% input$filter_gear,]
})
filtercarb <- reactive({
unique(as.character(datasub1()$carb))
})
observeEvent(filtercarb(), {
updateSelectInput(session,
"filter_carb",
choices = sort(filtercarb()),
selected = sort(filtercarb()))
})
# Subset dynamically the previous reactive filter #
datasub2 <- reactive({
# browser()
data_1[data_1$carb %in% input$filter_carb,]
})
filtercyl <- reactive({
unique(as.character(datasub2()$cyl))
})
observeEvent(filtercyl(), {
updateSelectInput(session,
"filter_cyl",
choices = sort(filtercyl()),
selected = sort(filtercyl()))
})
Filter2 <- reactive({
# Subset for plotly reactivity
Filter1 <- droplevels.data.frame(data_1)
filter(Filter1,
Filter1$gear %in% input$filter_gear,
Filter1$carb %in% input$filter_carb,
Filter1$cyl %in% input$filter_cyl)
})
output$databasedf <- DT::renderDT({
datatable(Filter2(),
filter="none",
selection="none",
escape=FALSE,
rownames = FALSE,
# colnames = c("", ""),
#autoHideNavigation = TRUE,
style = 'bootstrap4',
options = list(searching = FALSE, # remove search option
ordering = FALSE, # remove sort option
paging = FALSE, # remove paging
info = FALSE # remove bottom information
)) %>%
formatStyle(columns = 1, fontWeight = 'bold', `text-align` = 'left') # text to bold and lign left in first column
})
output$fig <-renderPlot({
req(input$filter_gear)
req(input$filter_carb)
req(input$filter_cyl)
#plt <- addDataFrame(reac_filter$Filter2, sheet, col.names=TRUE, row.names=TRUE, startRow=1, startColumn=1)
ggplot() + geom_line(aes(x=hp, y=mean(mpg), color=am),data = Filter2()) })
}
shinyApp(ui, server)
Upvotes: 1