从R的ui中选择y变量的geom_boxplot

时间:2019-11-26 15:52:47

标签: r ggplot2 shiny

我对Shiny完全陌生,并且已经有一段时间没有在R中进行编程了。

我正在尝试构建一个闪亮的应用程序,其中将有两个选项卡(对于这个问题,我仅处理第一个选项卡上的绘图-箱线图)

  1. 在第一个选项卡(boxplot)中,我应该有一个箱线图(output$out_boxplot),而应该从UI(input$in_variable)中选择要在y轴上绘制的值。 。 x轴上的值仅为x=''

下面是我到目前为止编写的代码,该代码不起作用:

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

尽管我似乎没有从input$in_variable表中获取ngsRunStats列。

我知道boxplot()函数在与Shiny一起使用时存在很大问题,所以现在我想知道ggplot中的geom_boxplot()是否也表现异常,或者我的代码中是否缺少任何内容?

1 个答案:

答案 0 :(得分:0)

如果您查看aes()的描述,就会看到

  

aes()是一个引用函数。这意味着引用的输入要在数据的上下文中进行评估。

这意味着ggplot()期望input$in_variable是数据中的变量名,但是在您的情况下,您希望将input$in_variable的值作为输入。为此,您可以仅使用aes_string()而不是aes(),但是必须将x=''留在其中,这没有问题,因为它为空。

此外,您实际上还需要使用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)
  })
}