简体   繁体   中英

geom_boxplot with y variable selected from ui in R shiny

I am totally new to shiny and haven't been programming in R for quite some time.

I am trying to build a shiny app in which I will have two tabs (for this question I am only dealing with the plots on the first tab - the boxplot)

  1. In the first tab ( boxplot ) I should have a boxplot ( output$out_boxplot ), whereas the values to be plotted on the y axis should be selected from the UI ( input$in_variable ). The value on the x axis is just x=''

Below is the code that I have written so far, which does not work:

# sample data
runStats = structure(list(X = 0:5, runStats_ID = 1:6, samplesheet_name = structure(c(1L, 
2L, 5L, 4L, 3L, 6L), .Label = c("20191001 1549 NovaSeq NIPT 1278 Samplesheet.csv", 
"20191003 1627 NovaSeq NIPT 1279 Samplesheet.csv", "20191003 Novaseq 63 Samplesheet.csv", 
"20191004 Novaseq 62 Samplesheet.csv", "20191004 NovaSeq 64 Samplesheet.csv", 
"20191007 1558 NovaSeq NIPT 1280 Samplesheet.csv", "20191008 1341 NovaSeq Spike 20 Samplesheet.csv", 
"20191009 1511 NovaSeq NIPT 1281 Samplesheet.csv", "20191010 1405 NovaSeq Spike 21 Samplesheet.csv", 
"20191010 Novaseq 65 Samplesheet.csv", "20191011 1705 NovaSeq NIPT 1282 Samplesheet.csv", 
"20191011 Novaseq 66 Samplesheet.csv", "20191014 NovaSeq 67 Samplesheet.csv", 
"20191014 NovaSeq 68 Samplesheet.csv", "20191015 1544 NovaSeq NIPT 1283 Samplesheet.csv", 
"20191017 1446 NovaSeq NIPT 1284 Samplesheet.csv", "20191018 1530 NovaSeq NIPT 1285 Samplesheet.csv", 
"20191018 NovaSeq 70 Samplesheet.csv", "20191022 1611 NovaSeq NIPT 1286 Samplesheet.csv", 
"20191024 1513 NovaSeq NIPT 1287 Samplesheet.csv", "20191025 NovaSeq 71 Samplesheet.csv", 
"20191028 1539 NovaSeq NIPT 1288 Samplesheet.csv", "20191029 1514 NovaSeq Spike 22 Samplesheet.csv", 
"20191030 1510 NovaSeq NIPT 1289 Samplesheet.csv", "20191031 1457 NovaSeq Spike 23 Samplesheet.csv", 
"20191031 NovaSeq 72 Samplesheet.csv", "20191104 1518 NovaSeq NIPT 1290 Samplesheet.csv", 
"20191106 1532 NovaSeq NIPT 1291 Samplesheet.csv", "20191107 NovaSeq 73 Samplesheet.csv", 
"20191108 1545 NovaSeq NIPT 1292 en pgt 132 Samplesheet.csv", 
"20191112 1517 NovaSeq NIPT 1293 Samplesheet.csv", "20191114 1528 NovaSeq NIPT 1294 Samplesheet.csv", 
"20191115 1119 NovaSeq 75 Samplesheet.csv", "20191115 1547 NovaSeq NIPT 1294 Samplesheet.csv", 
"20191118 1452 NovaSeq NIPT 1295 Samplesheet.csv", "20191118 1755 NIPT NEBNext 4 en 5 Samplesheet.csv", 
"20191119 1528 NovaSeq NIPT 1296 Samplesheet.csv", "20191120 0955 NovaSeq Spike 23 Samplesheet.csv", 
"20191121 1532 NovaSeq NIPT 1297 Samplesheet.csv", "20191121 NovaSeq 77 Samplesheet.csv", 
"20191122 NovaSeq 78 Samplesheet.csv", "20191125 1522 NovaSeq NIPT 1298 Samplesheet.csv"
), class = "factor"), run_ID = c(1L, 2L, 4L, 5L, 6L, 7L), minimal_cluster_density = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "-", class = "factor"), percentage_cluster_post_filter = c(80.88, 
71.97, 78.01, 70.72, 76.75, 81.38), percentage_reads_w_quality_greater_than_Q30 = c(94.94, 
92.86, 93.61, 92.02, 89.44, 94.85), percentage_aligned_phiX = c(0.83, 
0.82, 3.17, 0.51, 1.39, 0.95), error_rate = c(0.09, 0.16, 0.32, 
0.24, 0.14, 0.09), run_ID.1 = c(1L, 2L, 4L, 5L, 6L, 7L), run_name = structure(1:6, .Label = c("191001_A00154_0330_AHFFMWDRXX", 
"191003_A00154_0331_AHFCJKDRXX", "191004_A00154_0333_AHCWCGDRXX", 
"191004_A00154_0334_BHLG2NDMXX", "191007_A00154_0335_BHLF5MDMXX", 
"191007_A00154_0336_AHFFH7DRXX", "191008_A00154_0337_AHCYV5DRXX", 
"191009_A00154_0338_BHFF3WDRXX", "191010_A00154_0339_BHKY72DMXX", 
"191010_A00154_0340_AHFFCYDRXX", "191011_A00154_0341_AHFC2LDRXX", 
"191011_A00154_0342_BHLFGFDMXX", "191014_A00154_0343_AHLFV5DMXX", 
"191014_A00154_0344_BHKH2GDSXX", "191015_A00154_0345_AHGCVJDRXX", 
"191017_A00154_0347_AHGCVLDRXX", "191018_A00154_0348_AHGCNNDRXX", 
"191018_A00154_0349_BHL3M7DSXX", "191022_A00154_0350_AHG3FVDRXX", 
"191024_A00154_0351_BHG3GLDRXX", "191025_A00154_0352_AHGFCKDRXX", 
"191028_A00154_0353_BHGFCJDRXX", "191029_A00154_0354_AHFGN2DRXX", 
"191030_A00154_0355_AHG3F2DRXX", "191031_A00154_0356_AHGWJVDRXX", 
"191031_A00154_0357_BHLFKKDMXX", "191104_A00154_0358_AHGCNJDRXX", 
"191106_A00154_0359_BHGCTNDRXX", "191107_A00154_0361_AHH3L3DRXX", 
"191108_A00154_0362_AHGCTVDRXX", "191112_A00154_0363_BHG3F5DRXX", 
"191114_A00154_0364_AHG3HMDRXX", "191115_A00154_0365_AHH52LDRXX", 
"191115_A00154_0366_BHL3V3DMXX", "191118_A00154_0367_BHH353DRXX", 
"191118_A00154_0368_AHH3HGDRXX", "191119_A00154_0369_AHH3K3DRXX", 
"191120_A00154_0370_BHGWNFDRXX", "191121_A00154_0371_BHH53HDRXX", 
"191121_A00154_0372_AHH2WKDRXX", "191122_A00154_0373_AHL7GHDMXX", 
"191125_A00154_0374_BHH3JWDRXX"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

# imports
library(shiny)
library(ggplot2)
library(dplyr)

# UI ----------------------------------------------------------------------

ui <- fluidPage(
  # add the title panel
  titlePanel(title = "NGS run stats metrics", windowTitle = "Tab title in web browser"),

  # Set the sidebarlayout
  sidebarLayout(

    ## SIdebarpanel
    # select input for the variable
    sidebarPanel(

      selectInput(inputId = 'in_variable', label = 'select a variable', choices = c(
        "percentage_cluster_post_filter",
        "percentage_reads_w_quality_greater_than_Q30",
        "percentage_aligned_phiX",
        "error_rate")),

      # select the input for chosing the runID
      selectInput(inputId = 'in_runID', label = 'select a run ID', choices = sort(unique(runStats$run_name)))
    ),
    ## MainPanel
    mainPanel(
      tabsetPanel(type = 'tabs',
                  tabPanel(title = 'boxplot', plotOutput(outputId = 'out_boxplot')),
                  tabPanel(title = "trend", plotOutput(outputId = 'out_trend')))
    )

  )

)


# SERVER ------------------------------------------------------------------

server <- function(input, output) {
  filtered = reactive(runStats)


  # The boxplot
  output$out_boxplot = renderPlot({
    ggplot(data = filtered(), aes(x='', y=input$in_variable))
  })
}

shinyApp(ui, server)

Though it appears that I am not fetching the input$in_variable column from the ngsRunStats table.

I know that the boxplot() function is quite problematic when using with shiny, so now I am wondering if the geom_boxplot() from ggplot also behaves strangely or am I missing anything in my code?

If you have a look at the description of aes() , you will see that

aes() is a quoting function. This means that its inputs are quoted to be evaluated in the context of the data.

This means that ggplot() expects input$in_variable to be a variable name in your data, but in your case, you want the value of input$in_variable to be the input. For that, you can just use aes_string() instead of aes() , but you have to leave x='' out of it, which is no problem, because it is empty.

Additionally, you also need to actually draw a boxplot with geom_boxplot() .

# SERVER ------------------------------------------------------------------

server <- function(input, output) {
  filtered = reactive(runStats)


  # The boxplot
  output$out_boxplot = renderPlot({
    ggplot(data = filtered(), aes_string(y=input$in_variable)) + geom_boxplot() + 
      geom_point(data = filtered(), x = 0, colour='red', size=2.5)
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM