r - dashboardBody 没有显示来自 Shinydashboard 的 conditionalPanel 的正确图形
问题描述
也许有人可以帮我解决这个问题。我四处寻找,似乎无法弄清楚我哪里出错了。
我有一个shinydashboard
代表两组的数据,以及这些组中的人是否接受了治疗。随着时间的推移,我正在制作每个主题的线图。
我正在尝试创建选项以conditionalPanel
从sideBar
. 我已经正确设置了一些(我认为),以便我可以选择该组,然后选择哪些人接受了治疗。我的问题是,在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)
希望有人有这方面的经验,可以阐明我的错误。谢谢。
解决方案
推荐阅读
- r - 将 pROC 包与 h2o 一起使用
- c# - 将元素添加到字典中的列表会产生意想不到的结果 c#
- android - Update and Delete documents from two different collections on single click
- python - Optimizing code for grouping by unique person
- python - Backup gsuite users contacts (with service account)
- react-native - 无法实现 react-native-elements 表单?
- android - 在 Android 上使用 Unity3D 读取 Midi 文件
- python-3.x - Pygame is not installing in macOS
- laravel - Undefined variable: category in edit.blade
- c - C中静态函数的优先级