闪亮的仪表板。动态 UI 每个 wellPanel 有多个 selectInput

Shiny Dashboard. Dynamic UI with more than one selectInput per wellPanel

我有一个数据集,显示一组网站是否定期使用(yes/no 每个网站)以及上次使用时间(yesteraday/last 周/... 每个网站). 我想构建一个带有动态 UI 的 Shiny Dashboard,它显示两个选定网站的社会人口统计网站配置文件,并按网站使用情况或网站覆盖范围进行过滤。

动态结构UI:

选择过滤器类型 (1) 网站使用情况与 (2) 网站覆盖率

在"Website Usage"的情况下:

选择第一个网站 (web1-web5)

选择第二个网站 (web1-web5)

如果网站到达:

选择第一个网站 (web1-web5)

选择到达第一个网站(每日、每周、每月、每年)

选择第二个网站 (web1-web5)

选择到达第二个网站(每日、每周、每月、每年)

我尝试了 Rstudio 的以下解决方案: Dynamic UI Guide from Rstudio

我的问题是,使用 "switch" 的解决方案只允许每个 wellPanel 有一个 selectInput 字段。像这样我不能为第二个网站添加额外的过滤器。是否有不使用 switch 的解决方法或不同的解决方案?

示例数据帧

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

下面的代码是我得到的结果。但是我无法创建一个动态 UI 允许我用第二个网站的图形填充第二个仪表板列。 Switch 不允许我放置两个 selectInput 字段。

示例代码

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)

你可以 return renderUI 中的任何内容,只要是 class shiny.tag 中的任何内容即可。例如

# context server
output$ui <- renderUI({
  if (input$evalType == "regular")
    return(actionButton("some_id", "you clicked option regular"))
  else
    return(icon("bolt"))
})

有几种方法可以帮助您实现所需,例如您可以使用 conditionalPanel

[更新]

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)

if...else statement

您正在使用的 switch 功能当时仅适用于一个小部件,因此您需要创建多个 output$ui(基于 switch)。

我使用了@Gregor de Cillia 的输入。以下代码最终最适合我。

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)