user2300940
user2300940

Reputation: 2385

Function to define variable to plot

I would like make multiple plots by several plot-functions defined as output$myplot1 , output$myplot2 etc. and these plots are currently defined in the parameter selectInput, for instance p1 is Bone_disease. However, I would like to use another function than selectInput to define which parameters to plot for myplot1 and myplot2, since I don't want this to be shown in the sidebar.

How do I replace this line with a similar line that defines the clinical parameter to plot, but don't show it in the sidebar: selectInput("p1", "Clinical parameter", choices = c("Serum_M_component"))

The only parameter I would like to show in the sidebar is MicroRNA.

! Updated with example input data.

data_prep <- structure(list(miRNA = c("hsa-let-7a-3p", "hsa-let-7a-3p", "hsa-let-7a-3p"
), ID = c("86", "175", "217"), value = c(5.57979757386892, 5.21619202802748, 
5.42796072966512), Serum_M_component = c("IgG", "IgG", "Unknown"
), ISS_stage = c("Stage 3", "Stage 1", "Stage 3"), del17 = c("Poor Sample", 
"No", "No"), t4_14 = c("Poor Sample", "No", "Yes"), Bone_disease = c("No", 
"Yes", "Yes")), row.names = c(NA, 3L), class = "data.frame")

ui.miRNA.clinical <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("MicroRNA expression <br/> in Multiple myeloma"))),
  dashboardSidebar(
    selectInput("gene", "MicroRNA", choices = unique(data_prep$miRNA)),
    selectInput("p1", "Clinical parameter", choices = c("Bone_disease"),),
    selectInput("p2", "Clinical parameter", choices = c("Serum_M_component")),
    selectInput("p3", "Clinical parameter", choices = c("ISS_stage")),
    selectInput("p4", "Clinical parameter", choices = c("del17")),
    selectInput("p5", "Clinical parameter", choices = c("t4_14"))),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Plot Bone_disease", plotOutput("myplot1", width = "400px", height = "300px")),
      tabPanel("Plot Serum_M_component", plotOutput("myplot2", width = "400px", height = "300px")),
      tabPanel("Plot ISS_stage", plotOutput("myplot3", width = "400px", height = "300px")),
      tabPanel("Plot del17", plotOutput("myplot4", width = "400px", height = "300px")),
      tabPanel("Plot t4_14", plotOutput("myplot5", width = "400px", height = "300px"))
      
    )
  )
)

server.miRNA.clinical <- function(input, output, session) {
  
  # filter data by Gene
  data_selected <- reactive({
    filter(data_prep, miRNA %in% input$gene)
  })
  
  # Plot. use aes_string to simply use character input p
  #my_comparisons <- list( c("Yes", "No"), c("Stage 1", "Stage 3"))
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p1, "value", fill = input$p1)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "wilcox.test")
      #method ="anova"
  })
  output$myplot2 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p2, "value", fill = input$p2)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot3 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p3, "value", fill = input$p3)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot4 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p4, "value", fill = input$p4)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot5 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p5, "value", fill = input$p5)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
}

shinyApp(ui.miRNA.clinical, server.miRNA.clinical)

Upvotes: 0

Views: 58

Answers (1)

YBS
YBS

Reputation: 21287

One way to do this is to use shinyjs packages' hidden option. Try this

                                                                                                                                                                           "Yes", "Yes")), row.names = c(NA, 3L), class = "data.frame")
library(shinyjs)

ui.miRNA.clinical <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("MicroRNA expression <br/> in Multiple myeloma"))),
  dashboardSidebar(
    useShinyjs(),
    selectInput("gene", "MicroRNA", choices = unique(data_prep$miRNA)),
    shinyjs::hidden(
      selectInput("p1", "Clinical parameter", choices = c("Bone_disease"),),
      selectInput("p2", "Clinical parameter", choices = c("Serum_M_component")),
      selectInput("p3", "Clinical parameter", choices = c("ISS_stage")),
      selectInput("p4", "Clinical parameter", choices = c("del17")),
      selectInput("p5", "Clinical parameter", choices = c("t4_14"))
    )
    
    ),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Plot Bone_disease", plotOutput("myplot1", width = "400px", height = "300px")),
      tabPanel("Plot Serum_M_component", plotOutput("myplot2", width = "400px", height = "300px")),
      tabPanel("Plot ISS_stage", plotOutput("myplot3", width = "400px", height = "300px")),
      tabPanel("Plot del17", plotOutput("myplot4", width = "400px", height = "300px")),
      tabPanel("Plot t4_14", plotOutput("myplot5", width = "400px", height = "300px"))
      
    )
  )
)

server.miRNA.clinical <- function(input, output, session) {
  
  # filter data by Gene
  data_selected <- reactive({
    filter(data_prep, miRNA %in% input$gene)
  })
  
  # Plot. use aes_string to simply use character input p
  #my_comparisons <- list( c("Yes", "No"), c("Stage 1", "Stage 3"))
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p1, "value", fill = input$p1)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "wilcox.test")
    #method ="anova"
  })
  output$myplot2 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p2, "value", fill = input$p2)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot3 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p3, "value", fill = input$p3)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot4 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p4, "value", fill = input$p4)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot5 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p5, "value", fill = input$p5)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "anova") 
    #method ="anova"
  })
}

shinyApp(ui.miRNA.clinical, server.miRNA.clinical)

Upvotes: 1

Related Questions