0

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)
Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
Finn
  • 31
  • 1
  • 1
  • 7

3 Answers3

1

You can return whatever you want in renderUI as long as it is of the class shiny.tag. For example

# context server
output$ui <- renderUI({
  if (input$evalType == "regular")
    return(actionButton("some_id", "you clicked option regular"))
  else
    return(icon("bolt"))
})
Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • Hi Gregor, thanks for the answer. With this solution I get the same error message as with the "switch" code example. "multi-argument returns are not permitted". Is there a possibility to return two selctInputs into one wellPanel? – Finn Jun 27 '17 at 09:53
  • This error normally occurs when you type something like `return(a,b)`. Are you sure, you are not passing multiple values to the `return` function? – Gregor de Cillia Jun 27 '17 at 10:03
  • you are correct. After returning selectInput() buttons as a list everything works fine now. I will put the complete answer as code below. – Finn Jun 27 '17 at 11:12
1

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).

Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • Thanks this works perfectly. But for some reason it messes up my column layout. The ui is displayed above the graphics rather than next to them. – Finn Jun 27 '17 at 11:59
0

I used the Input from @Gregor de Cillia . The following code ended up working best for me.

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 (input$evalType == "Regular")
      return(
        list(uiWeb1 = selectInput(inputId = "websiteName1", label = "Choose first Website", choices = unique(dfRegular$web)),
             uiWeb2 = selectInput(inputId = "websiteName2", label = "Choose second Website", choices = unique(dfRegular$web)))
      )

    else if(input$evalType == "Reach")
      return(
        list(uiRch1 = selectInput(inputId = "websiteName3", label = "Choose first Website", choices = unique(dfReach$web)),
             uiRch2 = selectInput(inputId = "reach1", label = "Choose Reach", choices = c("daily","weekly","monthly","yearly")),
             uiRch3 = selectInput(inputId = "websiteName4", label = "Choose second Website", choices = unique(dfReach$web)),
             uiRch4 = selectInput(inputId = "reach2", label = "Choose Reach", choices = c("daily","weekly","monthly","yearly"))
             )
      )

    else 
      return(icon("bolt"))
  })

  dfInput1 <- reactive({
    dfRegular %>% filter(web == input$websiteName1 & value == "Yes")
  })

  output$Gender <- 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)
  })      

  dfInput2 <- reactive({
    dfRegular %>% filter(web == input$websiteName2 & value == "Yes")
  })

  output$Gender1 <- renderPlot({
    df1 <- dfInput2()
    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)
Finn
  • 31
  • 1
  • 1
  • 7