Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

r - dashboardBody not showing proper figure from conditionalPanel for shinydashboard

Maybe someone can help me out with this. I've searched around and can't seem to figure out where I am going wrong.

I have a shinydashboard for data representing two groups and whether the people in those groups received treatment or not. I'm producing a line plot of each subject over time.

I'm trying to create the option to select one group or the other from the conditionalPanel in the sideBar. I have some of it set up correctly (I think) to where I can select the group and then select down to which people had treatment or not. My issue is that in the dashboardBody I keep getting two plots, one for group 1 and one for group 2. I'd like to have the body of the dashboard change the plot between groups and I can't figure out where I am going wrong.

Here is a workable example:

Load data & packages

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

Build the sidebar, dashboard body, and 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
)

Create the server

##### 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 the app

#### run app
shinyApp(ui, server)

Hopefully someone has had experience with this and can shed some light on my error. Thanks.

question from:https://stackoverflow.com/questions/65948603/dashboardbody-not-showing-proper-figure-from-conditionalpanel-for-shinydashboard

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In body put tabItem inside tabItems to separate the plots based on tab selection and it will give you only 1 plot at any given time in a particular tab.

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...