0

我有一个数据集,显示一组网站是否定期使用(每个网站是/否)以及上次使用的时间(昨天/上周/...每个网站)。我想构建一个带有动态 UI 的闪亮仪表板,显示两个相邻的选定网站的社会人口统计网站配置文件,按网站使用情况或网站覆盖范围进行过滤。

动态 UI 的结构:

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

如果是“网站使用”:

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

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

如果是网站到达率:

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

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

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

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

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

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

示例数据框

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

3 回答 3

1

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"))
})
于 2017-06-27T08:29:41.130 回答
1

有几种方法可以帮助您实现所需的目标,例如,您可以使用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)。

于 2017-06-27T08:38:05.610 回答
0

我使用了来自@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)
于 2017-06-27T11:20:40.513 回答