Category partition in Shiny: R - r

I would like to improve a Shiny application that already appeared in this forum. I would like to achieve such an effect that, for example, by choosing Category1 "a", the category "a, b" was also shown. Similarly, when selecting the "c" Category1, all other categories containing "c" should be visible, in this case "c, b".
Code:
library(shiny)
data.input <- data.frame(
Category1 = rep(sample(c("a,b","a","c,b","b", "c"), 45, replace = T)),
Info = paste("Text info", 1:45),
Category2 = sample(letters[15:20], 45, replace = T),
Size = sample(1:100, 45),
MoreStuff = paste("More Stuff", 1:45)
)
ui <- fluidPage(titlePanel("Test Explorer"),
sidebarLayout(
sidebarPanel(
selectizeInput(
"show_vars",
"Columns to show:",
choices = colnames(data.input), # edit
multiple = TRUE,
selected = c("Category1", "Info", "Category2")
),
actionButton("button", "An action button"),
uiOutput("category1"),
uiOutput("category2"),
uiOutput("sizeslider")
),
mainPanel(tableOutput("table"))
))
server <- function(input, output, session) {
data.react <- eventReactive(input$button, {
data.input[, input$show_vars]
})
observeEvent(input$button, {
output$category1 <- renderUI({
data.sel <- data.react()
selectizeInput('cat1',
'Choose Cat 1',
choices = c("All", sort(as.character(
unique(data.sel$Category1)
))),
selected = "All")
})
df_subset <- eventReactive(input$cat1, {
data.sel <- data.react()
if (input$cat1 == "All") {
data.sel
}
else{
data.sel[data.sel$Category1 == input$cat1,]
}
})
output$category2 <- renderUI({
selectizeInput(
'cat2',
'Choose Cat 2 (optional):',
choices = sort(as.character(unique(
df_subset()$Category2
))),
multiple = TRUE,
options = NULL
)
})
df_subset1 <- reactive({
if (is.null(input$cat2)) {
df_subset()
} else {
df_subset()[df_subset()$Category2 %in% input$cat2,]
}
})
output$sizeslider <- renderUI({
sliderInput(
"size",
label = "Size Range",
min = min(data.input$Size),
max = max(data.input$Size),
value = c(min(data.input$Size), max(data.input$Size))
)
})
df_subset2 <- reactive({
if (is.null(input$size)) {
df_subset1()
} else {
df_subset1()[data.input$Size >= input$size[1] &
data.input$Size <= input$size[2],]
}
})
output$table <- renderTable({
df_subset2()
})
})
}
shinyApp(ui, server)
Expected effect:
Changed version:
I would like the abc not to show up in bc.

One way to do that is using grepl and sapply. You could use:
slt <- sapply(X = data.sel$Category1, FUN = grepl, pattern = input$cat1 )
So you would get all the rows in catergory 1 that has the string.
In your code it would be something like this:
server <- function(input, output, session) {
data.react <- eventReactive(input$button, {
data.input[, input$show_vars]
})
observeEvent(input$button, {
output$category1 <- renderUI({
data.sel <- data.react()
selectizeInput('cat1',
'Choose Cat 1',
choices = c("All", sort(as.character(
unique(data.sel$Category1)
))),
selected = "All")
})
df_subset <- eventReactive(input$cat1, {
data.sel <- data.react()
if (input$cat1 == "All") {
data.sel
}
else{
###########################This part has been added#######################
slt <- sapply(X = data.sel$Category1, FUN = grepl, pattern = input$cat1 )
data.sel[slt,]
##################################################################
# data.sel[data.sel$Category1 == input$cat1,]
}
})
output$category2 <- renderUI({
selectizeInput(
'cat2',
'Choose Cat 2 (optional):',
choices = sort(as.character(unique(
df_subset()$Category2
))),
multiple = TRUE,
options = NULL
)
})
df_subset1 <- reactive({
if (is.null(input$cat2)) {
df_subset()
} else {
df_subset()[df_subset()$Category2 %in% input$cat2,]
}
})
output$sizeslider <- renderUI({
sliderInput(
"size",
label = "Size Range",
min = min(data.input$Size),
max = max(data.input$Size),
value = c(min(data.input$Size), max(data.input$Size))
)
})
df_subset2 <- reactive({
if (is.null(input$size)) {
df_subset1()
} else {
df_subset1()[data.input$Size >= input$size[1] &
data.input$Size <= input$size[2],]
}
})
output$table <- renderTable({
df_subset2()
})
})
}
With this modification your output would look like this
Hope it helps!
EDIT1:
Since comma separated words was you actually wanted I guess this approach would maybe help you.
slt <- sapply(X= data.sel$Category1, FUN = function(x, y){
ele1 <- unique(unlist(strsplit(as.character(x), split = ",")))
ele2 <- unique(unlist(strsplit(y, split = ",")))
if(any(ele1 == ele2))
return(TRUE)
else
return(FALSE)
},y=input$cat1
)
EDIT2:
Here is the full code:
server <- function(input, output, session) {
data.react <- eventReactive(input$button, {
data.input[, input$show_vars]
})
observeEvent(input$button, {
output$category1 <- renderUI({
data.sel <- data.react()
selectizeInput('cat1',
'Choose Cat 1',
choices = c("All", sort(as.character(
unique(data.sel$Category1)
))),
selected = "All")
})
df_subset <- eventReactive(input$cat1, {
data.sel <- data.react()
if (input$cat1 == "All") {
data.sel
}
else{
###########################This part has been added#######################
# slt <- sapply(X = data.sel$Category1, FUN = grepl, pattern = input$cat1 )
slt <- sapply(X= data.sel$Category1, FUN = function(x, y){
ele1 <- unique(unlist(strsplit(as.character(x), split = ",")))
ele2 <- unique(unlist(strsplit(y, split = ",")))
if(any(ele1 == ele2))
return(TRUE)
else
return(FALSE)
},y=input$cat1
)
data.sel[slt,]
##################################################################
# data.sel[data.sel$Category1 == input$cat1,]
}
})
output$category2 <- renderUI({
selectizeInput(
'cat2',
'Choose Cat 2 (optional):',
choices = sort(as.character(unique(
df_subset()$Category2
))),
multiple = TRUE,
options = NULL
)
})
df_subset1 <- reactive({
if (is.null(input$cat2)) {
df_subset()
} else {
df_subset()[df_subset()$Category2 %in% input$cat2,]
}
})
output$sizeslider <- renderUI({
sliderInput(
"size",
label = "Size Range",
min = min(data.input$Size),
max = max(data.input$Size),
value = c(min(data.input$Size), max(data.input$Size))
)
})
df_subset2 <- reactive({
if (is.null(input$size)) {
df_subset1()
} else {
df_subset1()[data.input$Size >= input$size[1] &
data.input$Size <= input$size[2],]
}
})
output$table <- renderTable({
df_subset2()
})
})
}

Related

R shiny datatable with numericinput interactivity issue

I want to create a table in RShiny with numericInput so that user-supplied values can be used immediately. I followed the code HERE, but as the variables (car models) changes, it stops printing the new values. It works fine until the user changes the input.
Here is the code:
library(shiny)
library(DT)
library(tidyverse)
ui <- fluidPage(
tags$head(tags$script(
HTML(
"Shiny.addCustomMessageHandler('unbindDT', function(id) {
var $table = $('#'+id).find('table');
if($table.length > 0){
Shiny.unbindAll($table.DataTable().table().node());
}
})")
)),
title = 'selectInput or numericInput column in a table',
sidebarLayout(
sidebarPanel(
selectizeInput(inputId = "cars", label = "Car model", choices = rownames(mtcars), selected = rownames(mtcars)[1:6], multiple = T )
),
mainPanel(
DT::dataTableOutput('carTable'),
verbatimTextOutput('price')
)
)
)
server <- function(input, output, session) {
rvar <- reactiveValues(
DF = mtcars
)
observeEvent(input$cars,{
for (i in 1:nrow(rvar$DF)) {
rvar$DF$price[i] <- as.character(numericInput(paste0("price", i), "", 0, width = "100px"))
}
rvar$data <- rvar$DF[rownames(mtcars) %in% input$cars, ] %>% select(-price)
})
output$carTable = DT::renderDT({
data <- rvar$DF[rownames(mtcars) %in% input$cars, ] %>% mutate(carmodel = input$cars) %>% relocate(carmodel)
datatable(
data, escape = FALSE, selection = 'none',
options = list(
dom = 't',
paging = FALSE,
ordering = FALSE,
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); }')
),
rownames = FALSE
)
}, server = FALSE)
output$price = renderPrint({
str(sapply(1:nrow(rvar$data), function(i) input[[paste0("price", i)]]))
})
observe({
updatedPrice <- sapply(1:nrow(rvar$DF), function(i) input[[paste0("price", i)]]) %>% Reduce(c,.)
if(is.null(updatedPrice) | length(updatedPrice) != nrow(rvar$data)){
updatedPrice <- 0
}
isolate({
rvar$data$price <- updatedPrice
})
print(sapply(1:nrow(rvar$data), function(i) input[[paste0("price", i)]]) %>% Reduce(c,.))
print(rvar$data)
})
observeEvent(input$cars, {
session$sendCustomMessage("unbindDT", "carTable")
})
}
shinyApp(ui, server)
Works like this. It took me several trials and I don't exactly remember what were the problems...
library(shiny)
library(DT)
library(tidyverse)
ui <- fluidPage(
tags$head(tags$script(
HTML(
"Shiny.addCustomMessageHandler('unbindDT', function(id) {
var $table = $('#'+id).find('table');
if($table.length > 0){
Shiny.unbindAll($table.DataTable().table().node());
}
})")
)),
title = "selectInput or numericInput column in a table",
sidebarLayout(
sidebarPanel(
selectizeInput(
inputId = "cars", label = "Car model",
choices = rownames(mtcars), selected = rownames(mtcars)[1:6],
multiple = TRUE
)
),
mainPanel(
DTOutput("carTable"),
verbatimTextOutput("price")
)
)
)
server <- function(input, output, session) {
rvar <- reactiveValues(
DF = mtcars
)
observeEvent(input$cars, {
rvar$DF <- rvar$DF[rownames(mtcars) %in% input$cars, ]
for(i in 1:nrow(rvar$DF)) {
rvar$DF$price[i] <-
as.character(numericInput(paste0("price", i), "", 0, width = "100px"))
}
rvar$data <- rvar$DF %>% select(-price)
rvar$DTdata <- rvar$DF %>%
mutate(carmodel = input$cars) %>%
relocate(carmodel)
session$sendCustomMessage("unbindDT", "carTable")
})
output$carTable <- renderDT({
data <- rvar$DTdata
datatable(
data,
escape = FALSE, selection = "none",
options = list(
dom = "t",
paging = FALSE,
ordering = FALSE,
preDrawCallback =
JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback =
JS('function() { Shiny.bindAll(this.api().table().node()); }')
),
rownames = FALSE
)
},
server = FALSE
)
output$price <- renderPrint({
str(sapply(1:nrow(rvar$data), function(i) input[[paste0("price", i)]]))
})
observe({
updatedPrice <-
sapply(1:nrow(rvar$DF), function(i) input[[paste0("price", i)]]) %>%
Reduce(c, .)
if(is.null(updatedPrice) || length(updatedPrice) != nrow(rvar$data)) {
updatedPrice <- 0
}
isolate({
rvar$data$price <- updatedPrice
})
print(
sapply(1:nrow(rvar$data), function(i) input[[paste0("price", i)]]) %>%
Reduce(c, .)
)
print(rvar$data)
})
}
shinyApp(ui, server)

Store dynamic R shiny inputs in dataframe

I want to have a UI which allows a user to give certain inputs, and if he requires more inputs he shall be able to click a button which then opens up new inputs. This has worked out fine thanks to this entry.
However, now I want to create a dataframe out of these inputs and I am struggling with that. This is my code:
library(shiny)
ui <- fluidPage(
fluidRow(
column(
width = 6,
uiOutput("selection_ui")
),
column(
width = 3,
uiOutput("amount_ui")
),
column(
width = 3,
uiOutput("year_ui")
)
),
fluidRow(
column(
width = 6,
actionButton(inputId = "addEntry",
label = "Add Entry")
),
column(
width = 6,
actionButton(inputId = "deleteEntry",
label = "Delete Entry")
)
),
br(),
fluidRow(
column(
width = 12,
actionButton(inputId = "Go",
label = "Submit")
)
),
dataTableOutput("Dataframe")
)
server <- function(input, output){
counter <- reactiveValues(value = 1)
AllInputs <- reactive({
x <- reactiveValuesToList(input)
})
observeEvent(input$addEntry, {
counter$value <- counter$value + 1
})
observeEvent(input$deleteEntry, {
req( counter$value >= 2 )
counter$value <- counter$value - 1
})
selection <- reactive({
n <- counter$value
if(n > 0){
isolate({
lapply(seq_len(n), function(i){
selectInput( inputId = paste0("select",i),
label = paste0(i, "-th selection:"),
choices = as.list(c("", "A", "B", "C")),
selected = AllInputs()[[paste0("select",i)]]
)
})
})
}
})
amount <- reactive({
n <- counter$value
if(n > 0){
isolate({
lapply(seq_len(n), function(i){
numericInput(inputId = paste0("number",i),
label = paste0(i, "-th amount:"),
value = AllInputs()[[paste0("number",i)]])
})
})
}
})
year <- reactive({
n <- counter$value
if(n > 0){
isolate({
lapply(seq_len(n), function(i){
numericInput(inputId = paste0("year",i),
label = paste0(i, "-th year:"),
value = AllInputs()[[paste0("year",i)]])
})
})
}
})
output$selection_ui <- renderUI({selection()})
output$amount_ui <- renderUI({amount()})
output$year_ui <- renderUI({year()})
eventReactive(input$Go, {
df <- data.frame(CREATE DATAFRAME HERE)
})
output$dataframe <- renderDataTable(df())
}
shinyApp(ui = ui, server = server)
I think I solved it using sapply():
df <- eventReactive(input$Go, {
n <- counter$value
tmp1 <- sapply(seq_len(n), function(i){
input[[paste0("select",i)]]
})
tmp2 <- sapply(seq_len(n), function(i){
input[[paste0("number",i)]]
})
tmp3 <- sapply(seq_len(n), function(i){
input[[paste0("year",i)]]
})
data.frame(col1 = tmp1,
col2 = tmp2,
col3 = tmp3
)
})

Is there a R function to apply in filter option in R shiny

I have the below code. I need to put a main filter in mainpanel so that i can select the categories that in turn should change the numbers (summary)
# faithful is the dataset
# Iris is the dataset
iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater
than 2.5")
library(shiny)
sample1 <- 1:3
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
"x",
"Operations",
choices = c("summary","stem","typeof","mode","birth"),
multiple = TRUE,
selectize = TRUE
)
),
mainPanel(
h6("Here it is"),
verbatimTextOutput("message")
)
)
)
server <- function(input, output, session) {
output$message <- renderPrint({
if(input$x == "summary"){
summary(iris$Petal.Width)
} else if (input$x == "stem"){
print(stem(faithful$eruptions))
} else if (input$x == "typeof"){
typeof(sample1)
} else if (input$x == "mode"){
mode(sample1)
}
})
}
shinyApp(ui, server)
Can I have a main filter of "Species" from Iris dataset. When I select "setosa",the summary should change accordingly
If we need to change the summary based on the values of 'Species', use renderUI with uiOutput
sample1 <- 1:3
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices =
c("summary","stem","typeof","mode","birth"),
multiple=FALSE,selectize = TRUE)),
mainPanel(h6("Here it is"),
verbatimTextOutput("message"),
uiOutput("Species")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "summary")
{
summary(iris$Petal.Width[iris$Species == input$Species])
} else if (input$x == "stem")
{
print(stem(faithful$eruptions))
} else if (input$x == "typeof")
{
typeof(sample1)
} else if (input$x == "mode")
{
mode(sample1)
}
})
output$message <- renderPrint({r1()})
output$Species <- renderUI({
selectInput("Species", "species",
choices = as.character(unique(iris$Species)), multiple = FALSE)
})
}
shinyApp(ui, server)
-output

Selection of columns for the table in Shiny

I would like to add a new category at the beginning which will select the columns for the table. I can not combine variables with other elements in an application. Could someone explain to me what I'm doing wrong? As you can see on the graphics program does not work well.
My code:
library(shiny)
data <- data.frame(
Category1 = rep(letters[1:3], each = 15),
Info = paste("Text info", 1:45),
Category2 = sample(letters[15:20], 45, replace = T),
Size = sample(1:100, 45),
MoreStuff = paste("More Stuff", 1:45)
)
ui <- fluidPage(titlePanel("Test Explorer"),
sidebarLayout(
sidebarPanel(
selectizeInput(
"show_vars",
"Columns to show:",
choices = colnames(data),
multiple = TRUE,
selected = c("Category1", "Info", "Category2")
),
uiOutput("category1"),
uiOutput("category2"),
uiOutput("sizeslider")
),
mainPanel(tableOutput("table"))
))
server <- function(input, output, session) {
data2 <- reactive({
req(input$table)
if (input$table == "All") {
return(data)
}
data[, names(data) %in% input$show_vars]
})
output$category1 <- renderUI({
selectizeInput('cat1',
'Choose Cat 1',
choices = c("All", sort(as.character(
unique(data$Category1)
))),
selected = "All")
})
df_subset <- eventReactive(input$cat1, {
if (input$cat1 == "All") {
df_subset <- data
}
else{
df_subset <- data[data$Category1 == input$cat1, ]
}
})
df_subset1 <- reactive({
if (is.null(input$cat2)) {
df_subset()
} else {
df_subset()[df_subset()$Category2 %in% input$cat2, ]
}
})
output$category2 <- renderUI({
selectizeInput(
'cat2',
'Choose Cat 2 (optional):',
choices = sort(as.character(unique(
df_subset()$Category2
))),
multiple = TRUE,
options = NULL
)
})
output$sizeslider <- renderUI({
sliderInput(
"size",
label = "Size Range",
min = min(df_subset1()$Size),
max = max(df_subset1()$Size),
value = c(min(df_subset1()$Size), max(df_subset1()$Size))
)
})
df_subset2 <- reactive({
if (is.null(input$size)) {
df_subset1()
} else {
df_subset1()[df_subset1()$Size >= input$size[1] &
df_subset1()$Size <= input$size[2], ]
}
})
output$table <- renderTable({
df_subset2()
})
}
shinyApp(ui, server)
You have few problems with your code
You stored that reactive value of the columns selection in data2(), and displaying table df_subset2(). As with your code, the columns change when you add columns and select Cat1 dropdown, since its values are dependent on the data.react.
Avoid using generic names like data to store data. Sometimes it interfere with R base names
You need to use ObserveEvent and eventReactive, when you expect the change on UI to reflect
Below is what I fixed, you can change accordingly.
Added a submit button
Wrapped the input selections code into an ObserveEvent
By this, your data is displayed only when you click the submit button.
Here is the code.
library(shiny)
data.input <- data.frame(
Category1 = rep(letters[1:3], each = 15),
Info = paste("Text info", 1:45),
Category2 = sample(letters[15:20], 45, replace = T),
Size = sample(1:100, 45),
MoreStuff = paste("More Stuff", 1:45)
)
ui.r
ui <- fluidPage(titlePanel("Test Explorer"),
sidebarLayout(
sidebarPanel(
selectizeInput(
"show_vars",
"Columns to show:",
choices = colnames(data.input),
multiple = TRUE,
selected = c("Category1", "Info", "Category2")
),
actionButton("button", "An action button"),
uiOutput("category1"),
uiOutput("category2"),
uiOutput("sizeslider")
),
mainPanel(tableOutput("table"))
))
server.r
server <- function(input, output, session) {
data.react <- eventReactive(input$button, {
data.input[, input$show_vars]
})
observeEvent(input$button, {
output$category1 <- renderUI({
data.sel <- data.react()
selectizeInput('cat1',
'Choose Cat 1',
choices = c("All", sort(as.character(
unique(data.sel$Category1)
))),
selected = "All")
})
df_subset <- eventReactive(input$cat1, {
data.sel <- data.react()
if (input$cat1 == "All") {
data.sel
}
else{
data.sel[data.sel$Category1 == input$cat1,]
}
})
output$category2 <- renderUI({
selectizeInput(
'cat2',
'Choose Cat 2 (optional):',
choices = sort(as.character(unique(
df_subset()$Category2
))),
multiple = TRUE,
options = NULL
)
})
df_subset1 <- reactive({
if (is.null(input$cat2)) {
df_subset()
} else {
df_subset()[df_subset()$Category2 %in% input$cat2,]
}
})
output$sizeslider <- renderUI({
sliderInput(
"size",
label = "Size Range",
min = min(data.input$Size),
max = max(data.input$Size),
value = c(min(data.input$Size), max(data.input$Size))
)
})
df_subset2 <- reactive({
if (is.null(input$size)) {
df_subset1()
} else {
df_subset1()[data.input$Size >= input$size[1] &
data.input$Size <= input$size[2],]
}
})
output$table <- renderTable({
df_subset2()
})
})
}
shinyApp(ui, server)

Shiny: select variables to table

I have one question to open the topic already. Well, I'm trying to do a similar app to this one Shiny: dynamic dataframe construction; renderUI, observe, reactiveValues. And I would like to add a new category at the beginning which will select the variables from the table. I can not combine variables with other elements in an application. Could someone explain to me what I'm doing wrong?
As you can see on the graphics program does not work well.
Below is a script
#rm(list = ls())
library(shiny)
data <- data.frame(Category1 = rep(letters[1:3],each=15),
Info = paste("Text info",1:45),
Category2 = sample(letters[15:20],45,replace=T),
Size = sample(1:100, 45),
MoreStuff = paste("More Stuff",1:45))
ui <- fluidPage(
titlePanel("Test Explorer"),
sidebarLayout(
sidebarPanel(
selectizeInput("show_vars", "Columns to show:",
choices = colnames(data), multiple = TRUE,
selected = c("Category1","Info","Category2")),
uiOutput("category1"),
uiOutput("category2"),
uiOutput("sizeslider")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output,session) {
data2 <- reactive({
req(input$table)
if(input$table == "All"){
return(data)
}
data[,names(data) %in% input$show_vars]
})
output$category1 <- renderUI({
selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
})
df_subset <- eventReactive(input$cat1,{
if(input$cat1=="All") {df_subset <- data}
else{df_subset <- data[data$Category1 == input$cat1,]}
})
df_subset1 <- reactive({
if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,]}
})
output$category2 <- renderUI({
selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
})
output$sizeslider <- renderUI({
sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
})
df_subset2 <- reactive({
if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],]}
})
output$table <- renderTable({
df_subset2()
})
}
shinyApp(ui, server)
You don't need data2 since you are not using it and instead you can just use the same condition to filter columns with %in% everywhere you are displaying the dataframe.
#rm(list = ls())
library(shiny)
data <- data.frame(Category1 = rep(letters[1:3],each=15),
Info = paste("Text info",1:45),
Category2 = sample(letters[15:20],45,replace=T),
Size = sample(1:100, 45),
MoreStuff = paste("More Stuff",1:45))
ui <- fluidPage(
titlePanel("Test Explorer"),
sidebarLayout(
sidebarPanel(
selectizeInput("show_vars", "Columns to show:",
choices = colnames(data), multiple = TRUE,
selected = c("Category1","Info","Category2")),
uiOutput("category1"),
uiOutput("category2"),
uiOutput("sizeslider")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output,session) {
output$category1 <- renderUI({
selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
})
df_subset <- eventReactive(input$cat1,{
if(input$cat1=="All") {df_subset <- data}
else{df_subset <- data[data$Category1 == input$cat1,names(data) %in% input$show_vars]}
})
df_subset1 <- reactive({
if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,names(data) %in% input$show_vars]}
})
output$category2 <- renderUI({
selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
})
output$sizeslider <- renderUI({
sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
})
df_subset2 <- reactive({
if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],names(data) %in% input$show_vars]}
})
output$table <- renderTable({
df_subset2()
})
}
shinyApp(ui, server)

Resources