首页 > 解决方案 > dashboardBody 没有显示来自 Shinydashboard 的 conditionalPanel 的正确图形

问题描述

也许有人可以帮我解决这个问题。我四处寻找,似乎无法弄清楚我哪里出错了。

我有一个shinydashboard代表两组的数据,以及这些组中的人是否接受了治疗。随着时间的推移,我正在制作每个主题的线图。

我正在尝试创建选项以conditionalPanelsideBar. 我已经正确设置了一些(我认为),以便我可以选择该组,然后选择哪些人接受了治疗。我的问题是,在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)

希望有人有这方面的经验,可以阐明我的错误。谢谢。

标签: rshinyshinydashboard

解决方案


在里面根据选项卡选择来分隔绘图,它只会在特定选项卡中的任何给定时间为您提供 1 个绘图bodytabItemtabItems

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

在此处输入图像描述


推荐阅读