我有一个数据集,显示一组网站是否定期使用(每个网站是/否)以及上次使用的时间(昨天/上周/...每个网站)。我想构建一个带有动态 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)