I have a dataset that shows for a set of websites if each one is used regularly (yes/no per website) and when it was used last (yesteraday/last week/... per website). I want to build a Shiny Dashboard with a dynamic UI that shows sociodemographic website profiles for two chosen websites next to each other, filtered either by website usage or website reach.
Structure of dynamic UI:
Choose Filter Type (1) Website Usage vs (2) Website Reach
In case of "Website Usage":
Choose 1st Website (web1-web5)
Choose 2nd Website (web1-web5)
In case of Website Reach:
Choose 1st Website (web1-web5)
Choose Reach 1st Website (daily, weekly, monthly, yearly)
Choose 2nd Website (web1-web5)
Choose Reach 2nd Website (daily, weekly, monthly, yearly)
I tried the following solution from Rstudio: Dynamic UI Guide from Rstudio
My problem is, that the solution with using "switch" only allows one selectInput field per wellPanel. Like this I can't put additional filters for the 2nd website. Is there a workaround or a different solution not using switch?
Sample dataframe
gender <- factor(sample(1:2, 5, replace = TRUE),
levels = c(1,2,99), labels = c("Male", "Female", "Missing Value"))
age <- sample(18:55, 5, replace = TRUE)
web1 <- factor(sample(1:2, 5, replace = TRUE), levels = c(1,2,99),
labels = c("Yes", "No", "Missing Value"))
web2 <- factor(sample(1:2, 5, replace = TRUE), levels = c(1,2,99),
labels = c("Yes", "No", "Missing Value"))
web3 <- factor(sample(1:2, 5, replace = TRUE), levels = c(1,2,99),
labels = c("Yes", "No", "Missing Value"))
web4 <- factor(sample(1:2, 5, replace = TRUE), levels = c(1,2,99),
labels = c("Yes", "No", "Missing Value"))
web5 <- factor(sample(1:2, 5, replace = TRUE), levels = c(1,2,99),
labels = c("Yes", "No", "Missing Value"))
web1Rch <- factor(sample(1:4, 5, replace = TRUE), levels = c(1,2,3,4,99),
labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web2Rch <- factor(sample(1:4, 5, replace = TRUE), levels = c(1,2,3,4,99),
labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web3Rch <- factor(sample(1:4, 5, replace = TRUE), levels = c(1,2,3,4,99),
labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web4Rch <- factor(sample(1:4, 5, replace = TRUE), levels = c(1,2,3,4,99),
labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web5Rch <- factor(sample(1:4, 5, replace = TRUE), levels = c(1,2,3,4,99),
labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
popWeight <- sample(1000:1500, 5, replace = TRUE)
df <- data.frame(gender, age, web1, web2, web3, web4, web5, web1Rch,
web2Rch, web3Rch, web4Rch, web5Rch, popWeight)
df
The following code is how far I got. But I can't create a dynamic UI that allows me to populate the second dashboard column with graphics for a second website. Switch doesn't allow me to put two selectInput fields.
Sample Code
library(shiny)
library (tidyr)
library (dplyr)
library(ggplot2)
library(scales)
# Create Two Versions of Data Frame for "Regular Usage" and "Reach"
dfRegular <- df[,c(1:7,13)] %>%
gather(web, value, -age, -gender, -popWeight)
dfReach <- df[,c(1:2,8:13)] %>%
gather(web, value, -age, -gender, -popWeight)
# Code for Shiny App
ui <- fluidPage(
titlePanel ("Website Profile"),
br(),
fluidRow(
column(2,
wellPanel(
selectInput(inputId = "evalType", label = "Choose Evaluation",
choices = c("Regular", "Reach"))
),
wellPanel(uiOutput("ui"))
),
column(5, plotOutput("Gender")),
column(5, plotOutput("Gender1"))
)
)
server <- function(input, output) {
# Output UI
output$ui <- renderUI({
if(is.null(input$evalType))
return()
switch(
input$evalType,
"Regular" = selectInput(
inputId = "websiteName", label = "Choose first Website",
choices = unique(dfRegular$web)),
"Reach" = selectInput(
inputId = "reachWeb", label = "Choose Reach (second Website)",
choices = c("web1Rch", "web2Rch", "web3Rch", "web4Rch", "web5Rch"))
)
})
output$evalTypeText <- renderText({
input$evalType
})
dfInput <- reactive({
dfRegular %>% filter(web == input$websiteName & value == "Yes")
})
output$Gender <- renderPlot({
df1 <- dfInput()
ggplot(df1) +
aes(x = gender, y = popWeight / sum(popWeight)) +
stat_summary(fun.y = sum, geom = "bar") +
scale_y_continuous("Population (%)", labels = scales::percent)
})
dfInput <- reactive({
dfRegular %>% filter(web == input$websiteName & value == "Yes")
})
output$Gender1 <- renderPlot({
df1 <- dfInput()
ggplot(df1) +
aes(x = gender, y = popWeight / sum(popWeight)) +
stat_summary(fun.y = sum, geom = "bar") +
scale_y_continuous("Population (%)", labels = scales::percent)
})
}
shinyApp(ui = ui, server = server)
There are few ways which can help You achieve what You need, You could use for example conditionalPanel
instead:
[UPDATE]
gender <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Male", "Female", "Missing Value"))
age <- sample(18:55, 5, replace=TRUE)
web1 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web2 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web3 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web4 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web5 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web1Rch <- factor(sample(1:4, 5, replace=TRUE), levels = c(1,2,3,4,99), labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web2Rch <- factor(sample(1:4, 5, replace=TRUE), levels = c(1,2,3,4,99), labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web3Rch <- factor(sample(1:4, 5, replace=TRUE), levels = c(1,2,3,4,99), labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web4Rch <- factor(sample(1:4, 5, replace=TRUE), levels = c(1,2,3,4,99), labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
web5Rch <- factor(sample(1:4, 5, replace=TRUE), levels = c(1,2,3,4,99), labels = c("Daily", "Weekly", "Monthly", "Yearly", "Missing Value"))
popWeight <- sample(1000:1500, 5, replace=TRUE)
df <- data.frame(gender, age, web1, web2, web3, web4, web5, web1Rch, web2Rch, web3Rch, web4Rch, web5Rch, popWeight)
df
library(shiny)
library (tidyr)
library (dplyr)
library(ggplot2)
library(scales)
# Create Two Versions of Data Frame for "Regular Usage" and "Reach"
dfRegular <- df[,c(1:7,13)] %>%
gather(web, value, -age, -gender, -popWeight)
dfReach <- df[,c(1:2,8:13)] %>%
gather(web, value, -age, -gender, -popWeight)
# Code for Shiny App
ui <- fluidPage(
titlePanel ("Website Profile"),
br(),
fluidRow(
column(2,
wellPanel(
selectInput(inputId = "evalType", label = "Choose Evaluation", choices = c("Regular", "Reach"))
),
wellPanel(
conditionalPanel(condition="input.evalType == 'Regular'",
selectInput(inputId = "websiteName", label = "Choose first Website", choices = unique(dfRegular$web))),
conditionalPanel(condition="input.evalType == 'Regular'",
selectInput(inputId = "websiteName2", label = "Choose second Website", choices = unique(dfRegular$web))),
conditionalPanel(condition="input.evalType == 'Reach'",
selectInput(inputId = "websiteName3", label = "Choose first Website", choices = unique(dfRegular$web))),
conditionalPanel(condition="input.evalType == 'Reach'",
selectInput(inputId = "reach1", label = "Choose Reach", choices = c("daily","weekly","monthly","yearly"))),
conditionalPanel(condition="input.evalType == 'Reach'",
selectInput(inputId = "websiteName4", label = "Choose first Website", choices = unique(dfRegular$web))),
conditionalPanel(condition="input.evalType == 'Reach'",
selectInput(inputId = "reach1", label = "Choose Reach", choices = c("daily","weekly","monthly","yearly"))))
)
,
column(5,
plotOutput("Gender")
),
column(5,
plotOutput("Gender1")
))
)
server <- function(input, output) {
dfInput <- reactive({
dfRegular %>% filter(web == input$websiteName & value == "Yes")
})
output$Gender <- renderPlot({
df1 <- dfInput()
ggplot(df1) +
aes(x = gender, y = popWeight / sum(popWeight)) +
stat_summary(fun.y = sum, geom = "bar") +
scale_y_continuous("Population (%)", labels = scales::percent)
})
dfInput1 <- reactive({
dfRegular %>% filter(web == input$websiteName2 & value == "Yes")
})
output$Gender1 <- renderPlot({
df1 <- dfInput1()
ggplot(df1) +
aes(x = gender, y = popWeight / sum(popWeight)) +
stat_summary(fun.y = sum, geom = "bar") +
scale_y_continuous("Population (%)", labels = scales::percent)
})
}
shinyApp(ui = ui, server = server)
or if...else statement
.
The switch
function which You are using is working only with one widget at the time, therefore You would need to create more then one output$ui
(based on switch
).