I am trying to develop a shiny app using reactive expressions. Initially on load the values on my table do argument is of length zero
. Similarly, I have two sets of dropdowns. The values in the second set of drops down (POPULATION FILTER) loads only after I click on the the first group of dropdown(CATEGORY). Could someone tell me if the way I am using reactive expressions is correct.
SERVER.R
library(shiny)
library(shinydashboard)
library(shinyBS)
library(knitr)
library(kableExtra)
library(plyr)
library(tidyverse)
library(DT)
library(dplyr)
shinyServer(function(input, output) {
# Drop-down selection box for which Wave and User Type bracket to be selected
output$choose_wave <- renderUI({
# This can be static: it is the highest level and the options won't change
selectInput(
"selected_wave",
"Wave",
choices = list(
"Wave 1 Adult" = "wave1youthFALSE",
"Wave 1 Youth" = "wave1youthTRUE",
"Wave 2 Adult" = "wave2youthFALSE",
"Wave 2 Youth" = "wave2youthTRUE"
)
)
})
wave <- reactive({
as.integer(gsub("wave(\\d)youth.*", "\\1", input$selected_wave))
})
youth <- reactive({
as.logical(gsub("wave\\dyouth(.+)$", "\\1", input$selected_wave))
})
with_demo_vars <- reactive({
data_selector(wave(), youth()) %>%
mutate(
ethnicity = !!ethnicity(),
age = !!age_group(),
gender = !!gender()
)
})
# Drop-down selection box for which Category bracket to be selected
output$choose_category <- renderUI({
selectInput("selected_category",
"Category",
choices = list(
"111" = "MC",
"222" = "EV",
"333" = "TC",
))
})
# Drop-down selection box for which Gender bracket to be selected
output$choose_ethnicity <- renderUI({
selectInput("selected_ethnicity", "Ethnicity", as.list(levels(with_demo_vars()$ethnicity)))
})
# Drop-down selection box for which Age bracket to be selected
output$choose_age <- renderUI({
selectInput("selected_age", "Age", as.list(levels(with_demo_vars()$age)))
})
# Drop-down selection box for which Gender bracket to be selected
output$choose_gender <- renderUI({
selectInput("selected_gender", "Gender", as.list(levels(with_demo_vars()$gender)))
})
output$selected_var <- renderText({
paste("You have selected", input$selected_wave)
})
myData <- reactive({
wave_selected <- input$selected_wave
category_selected <- input$selected_category
age_selected <- input$selected_age
gender_selected <- input$selected_gender
ethnicity_selected <- input$selected_ethnicity
df <- with_demo_vars() %>%
filter(age == age_selected) %>%
filter(gender == gender_selected) %>%
filter(ethnicity == ethnicity_selected) %>%
pct_ever_user(type = category_selected)
df
})
output$smoke <-
renderTable({
head(myData())
})
})
UI.R
library(shiny)
library(shinydashboard)
library(shinyBS)
library(shinythemes)
dashboardPage(
dashboardHeader(disable = F, title = "PATH Study"),
dashboardSidebar(
sidebarMenu(menuItem(
"Category",
uiOutput("choose_wave"),
uiOutput("choose_category")
)),
sidebarMenu(menuItem(
"Population Filter",
uiOutput("choose_ethnicity"),
uiOutput("choose_age"),
uiOutput("choose_gender")
)),
conditionalPanel(condition = "input.tabBox_next_previous == 'product_use'",
sidebarMenu(
menuItem(
"Product Category",
selectInput(
"flavor",
h4("Flavor"),
choices = list(
"Total" = 1,
"Flavored" = 2,
"Non-Flavored" = 3
),
selected = 1
),
selectInput(
"use_level",
h4("User Level"),
choices = list(
"Total" = 1,
"Experimental" = 2,
"Established" = 3,
"No Tobacco Use" = 4
),
selected = 1
)
)
))
),
#S dashboardPage(header = dashboardHeader(), sidebar = dashboardSidebar(),body,title = NUll, skin = "yellow"),
dashboardBody(box(
width = 12,
tabBox(
width = 12,
id = "tabBox_next_previous",
tabPanel("Initiation",
fluidRow(
box(
title = "Wave 1 Ever Tried and % 1st Product Flavored",
width = 7,
solidHeader = TRUE,
status = "primary",
tableOutput("smoke"),
collapsible = F,
bsTooltip(
"bins",
"The wait times will be broken into this many equally spaced bins",
"right",
options = list(container = "body")
)
),
box(
title = "Wave 1 Ever Tried and % 1st Product Flavored",
width = 7,
solidHeader = TRUE,
status = "primary",
tableOutput("selected_var"),
collapsible = F
)
)),
tabPanel("product_use", p("This is tab 4"))
),
uiOutput("Next_Previous")
))
)