我有一个数据集,显示一组网站,如果每个网站定期使用(每个网站是/否)和最后一次使用(是每天/上周/ ...每个网站)。 我想构建一个带有动态UI的Shiny Dashboard,它显示两个彼此相邻的所选网站的社会人口统计网站配置文件,通过网站使用或网站覆盖率进行过滤。
动态用户界面的结构:
选择过滤器类型(1)网站使用情况vs(2)网站覆盖率
如果是“网站使用”:
选择第一个网站(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)
答案 0 :(得分:1)
只要属于renderUI
类,您就可以在shiny.tag
中返回任何内容。例如
# context server
output$ui <- renderUI({
if (input$evalType == "regular")
return(actionButton("some_id", "you clicked option regular"))
else
return(icon("bolt"))
})
答案 1 :(得分:1)
有几种方法可以帮助您实现所需,您可以使用例如conditionalPanel
代替:
<强> [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)
或if...else statement
。
您正在使用的switch
函数当时只能处理一个窗口小部件,因此您需要创建多个output$ui
(基于switch
)。
答案 2 :(得分: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)