dashboardBody 未显示来自 shinydashboard 的 conditionalPanel 的正确数字
dashboardBody not showing proper figure from conditionalPanel for shinydashboard
也许有人可以帮我解决这个问题。我四处搜索,似乎无法弄清楚我哪里出错了。
我有一个 shinydashboard
代表两组的数据以及这些组中的人是否接受治疗。随着时间的推移,我正在制作每个主题的线图。
我正在尝试为 sideBar
中 conditionalPanel
的一组或另一组创建 select 选项。我有一些正确设置(我认为)到我可以 select 组的位置,然后 select 到哪些人接受过治疗。我的问题是,在 dashboardBody
中,我不断得到两个图,一个用于第 1 组,一个用于第 2 组。我想让仪表板的主体更改组之间的图,但我无法弄清楚我哪里错了。
这是一个可行的例子:
加载数据和包
library(tidyverse)
library(shiny)
library(shinydashboard)
library(gt)
theme_set(theme_light())
##### data
subject <- rep(LETTERS[1:10], each = 10)
group <- rep(c("grp1", "grp2"), each = 50)
treatment <- c(rep(c("yes", "no"), each = 25), rep(c("yes", "no"), each = 25))
day <- rep(1:10, times = 10)
var <- round(rnorm(n = length(subject), mean = 350, sd = 50), 0)
df <- data.frame(subject, group, treatment, day, var)
df
构建侧边栏、仪表板正文和 UI
## sidebar with two panels
side <- dashboardSidebar(
sidebarMenu(id = "sidebarID",
menuItem(text = "Group 1", tabName = "grp1"),
menuItem(text = "Group 2", tabName = "grp2"),
# tab 1 selections
conditionalPanel(
'input.sidebarID == "grp1"',
selectizeInput(inputId = "treatment1",
label = "Choose whether the subject had the treatment:",
choices = unique(df$treatment[df$group == "grp1"]),
selected = "yes",
multiple = FALSE),
selectizeInput(inputId = "subject1",
label = "Chose a Subject:",
choices = unique(df$subject[df$group == "grp1"]),
selected = NULL,
multiple = FALSE)
),
# tab 2 selections
conditionalPanel(
'input.sidebarID == "grp2"',
selectizeInput(inputId = "treatment2",
label = "Choose whether the subject had the treatment:",
choices = unique(df$treatment[df$group == "grp2"]),
selected = "yes",
multiple = FALSE),
selectizeInput(inputId = "subject2",
label = "Chose a Subject:",
choices = unique(df$subject[df$group == "grp2"]),
selected = NULL,
multiple = FALSE)
)
)
)
## dashboard body
body <- dashboardBody(
## page 1
tabItem(tabName = "grp1",
"Group 1",
br(), br(),
plotOutput(outputId = "plt_grp1")),
## page 2
tabItem(tabName = "grp2",
"Group 2",
plotOutput(outputId = "plt_grp2"))
)
## user interface
ui <- dashboardPage(
dashboardHeader(title = "Study 1"),
side,
body
)
创建服务器
##### server
server <- function(input, output, session){
## get data for group 1 tab
dat_grp1 <- reactive({
d <- df %>%
filter(group == "grp1",
treatment == input$treatment1)
updateSelectizeInput(session,
"subject1",
choices = unique(d$subject))
d
})
## get data for group 2 tab
dat_grp2 <- reactive({
d <- df %>%
filter(group == "grp2",
treatment == input$treatment2)
updateSelectizeInput(session,
"subject2",
choices = unique(d$subject))
d
})
## plot group 1
output$plt_grp1 <- renderPlot({
dat_grp1() %>%
ggplot(aes(x = day, y = var)) +
geom_line()
})
## plot group 2
output$plt_grp2 <- renderPlot({
dat_grp2() %>%
ggplot(aes(x = day, y = var)) +
geom_line()
})
}
运行 应用
#### run app
shinyApp(ui, server)
希望有人有过这方面的经验,可以阐明我的错误。谢谢。
在 body
中将 tabItem
放在 tabItems
中以根据选项卡选择分隔绘图,并且在特定选项卡中的任何给定时间只会给您 1 个绘图。
body <- dashboardBody(
tabItems(
## page 1
tabItem(tabName = "grp1",
"Group 1",
br(), br(),
plotOutput(outputId = "plt_grp1")),
## page 2
tabItem(tabName = "grp2",
"Group 2",
plotOutput(outputId = "plt_grp2"))
))
也许有人可以帮我解决这个问题。我四处搜索,似乎无法弄清楚我哪里出错了。
我有一个 shinydashboard
代表两组的数据以及这些组中的人是否接受治疗。随着时间的推移,我正在制作每个主题的线图。
我正在尝试为 sideBar
中 conditionalPanel
的一组或另一组创建 select 选项。我有一些正确设置(我认为)到我可以 select 组的位置,然后 select 到哪些人接受过治疗。我的问题是,在 dashboardBody
中,我不断得到两个图,一个用于第 1 组,一个用于第 2 组。我想让仪表板的主体更改组之间的图,但我无法弄清楚我哪里错了。
这是一个可行的例子:
加载数据和包
library(tidyverse)
library(shiny)
library(shinydashboard)
library(gt)
theme_set(theme_light())
##### data
subject <- rep(LETTERS[1:10], each = 10)
group <- rep(c("grp1", "grp2"), each = 50)
treatment <- c(rep(c("yes", "no"), each = 25), rep(c("yes", "no"), each = 25))
day <- rep(1:10, times = 10)
var <- round(rnorm(n = length(subject), mean = 350, sd = 50), 0)
df <- data.frame(subject, group, treatment, day, var)
df
构建侧边栏、仪表板正文和 UI
## sidebar with two panels
side <- dashboardSidebar(
sidebarMenu(id = "sidebarID",
menuItem(text = "Group 1", tabName = "grp1"),
menuItem(text = "Group 2", tabName = "grp2"),
# tab 1 selections
conditionalPanel(
'input.sidebarID == "grp1"',
selectizeInput(inputId = "treatment1",
label = "Choose whether the subject had the treatment:",
choices = unique(df$treatment[df$group == "grp1"]),
selected = "yes",
multiple = FALSE),
selectizeInput(inputId = "subject1",
label = "Chose a Subject:",
choices = unique(df$subject[df$group == "grp1"]),
selected = NULL,
multiple = FALSE)
),
# tab 2 selections
conditionalPanel(
'input.sidebarID == "grp2"',
selectizeInput(inputId = "treatment2",
label = "Choose whether the subject had the treatment:",
choices = unique(df$treatment[df$group == "grp2"]),
selected = "yes",
multiple = FALSE),
selectizeInput(inputId = "subject2",
label = "Chose a Subject:",
choices = unique(df$subject[df$group == "grp2"]),
selected = NULL,
multiple = FALSE)
)
)
)
## dashboard body
body <- dashboardBody(
## page 1
tabItem(tabName = "grp1",
"Group 1",
br(), br(),
plotOutput(outputId = "plt_grp1")),
## page 2
tabItem(tabName = "grp2",
"Group 2",
plotOutput(outputId = "plt_grp2"))
)
## user interface
ui <- dashboardPage(
dashboardHeader(title = "Study 1"),
side,
body
)
创建服务器
##### server
server <- function(input, output, session){
## get data for group 1 tab
dat_grp1 <- reactive({
d <- df %>%
filter(group == "grp1",
treatment == input$treatment1)
updateSelectizeInput(session,
"subject1",
choices = unique(d$subject))
d
})
## get data for group 2 tab
dat_grp2 <- reactive({
d <- df %>%
filter(group == "grp2",
treatment == input$treatment2)
updateSelectizeInput(session,
"subject2",
choices = unique(d$subject))
d
})
## plot group 1
output$plt_grp1 <- renderPlot({
dat_grp1() %>%
ggplot(aes(x = day, y = var)) +
geom_line()
})
## plot group 2
output$plt_grp2 <- renderPlot({
dat_grp2() %>%
ggplot(aes(x = day, y = var)) +
geom_line()
})
}
运行 应用
#### run app
shinyApp(ui, server)
希望有人有过这方面的经验,可以阐明我的错误。谢谢。
在 body
中将 tabItem
放在 tabItems
中以根据选项卡选择分隔绘图,并且在特定选项卡中的任何给定时间只会给您 1 个绘图。
body <- dashboardBody(
tabItems(
## page 1
tabItem(tabName = "grp1",
"Group 1",
br(), br(),
plotOutput(outputId = "plt_grp1")),
## page 2
tabItem(tabName = "grp2",
"Group 2",
plotOutput(outputId = "plt_grp2"))
))