Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用“nearpoints()”显示R中一维(长方体)打印的行信息_R_Shiny - Fatal编程技术网

使用“nearpoints()”显示R中一维(长方体)打印的行信息

使用“nearpoints()”显示R中一维(长方体)打印的行信息,r,shiny,R,Shiny,我正在尝试使用R的nearPoints()函数来显示df中某一行的变量。我希望通过单击输出箱线图中的一个点(例如,异常值)来选择行,在我的情况下,该点是一维的(我根据x=0绘制变量) 以下是我到目前为止编写的代码: # Sample dataset runStats = structure(list(X = 0:41, runStats_ID = 1:42, samplesheet_name = structure(c(1L, 2L, 5L, 4L, 3L, 6L, 7L, 8L, 10L,

我正在尝试使用R的
nearPoints()
函数来显示df中某一行的变量。我希望通过单击输出箱线图中的一个点(例如,异常值)来选择行,在我的情况下,该点是一维的(我根据
x=0
绘制变量)

以下是我到目前为止编写的代码:

# Sample dataset
runStats = structure(list(X = 0:41, runStats_ID = 1:42, samplesheet_name = structure(c(1L, 
2L, 5L, 4L, 3L, 6L, 7L, 8L, 10L, 9L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 34L, 33L, 36L, 35L, 37L, 38L, 40L, 39L, 41L, 
42L), .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, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L), minimal_cluster_density = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 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, 73.62, 81.81, 73.07, 77.36, 76.89, 75.47, 61.7, 
    81.93, 74.43, 74.51, 74.32, 78.03, 73.32, 77.99, 68.12, 59.46, 
    45.17, 72.76, 76.21, 66.34, 66.87, 73.05, 85.08, 78.16, 70.35, 
    76.88, 70.36, 74.21, 81.74, 79, 75.79, 74.81, 78.13, 80.53, 
    69.29, 76.25), percentage_reads_w_quality_greater_than_Q30 = c(94.94, 
    92.86, 93.61, 92.02, 89.44, 94.85, 93.31, 95.55, 92.84, 94.43, 
    94.03, 92.96, 89.73, 92.4, 94.17, 93.26, 94.01, 92.9, 93.77, 
    95.05, 94.53, 94.12, 83.18, 94.03, 94.11, 91.41, 92.21, 93.72, 
    94.85, 94.58, 93.52, 94.09, 93.84, 93.19, 95.14, 94.67, 94.05, 
    94.56, 93.81, 94.29, 91.57, 94.45), percentage_aligned_phiX = c(0.83, 
    0.82, 3.17, 0.51, 1.39, 0.95, 0.7, 0.95, 0.65, 1.18, 1.66, 
    0.73, 0.61, 0.97, 0.81, 0.46, 0.99, 0.65, 0.7, 0.73, 0.49, 
    0.29, 0.71, 0, 0.39, 0.39, 0.46, 0.53, 0.39, 0.53, 0.34, 
    0.34, 0.56, 0.3, 1.34, 0.41, 0.26, 1.22, 0.66, 0.67, 0.73, 
    0.79), error_rate = c(0.09, 0.16, 0.32, 0.24, 0.14, 0.09, 
    0.18, 0.08, 0.2, 0.12, 0.12, 0.19, 0.31, 0.29, 0.11, 0.14, 
    0.12, 0.2, 0.13, 0.09, 0.14, 0.12, 0.71, NA, 0.15, 0.27, 
    0.19, 0.14, 0.17, 0.13, 0.14, 0.13, 0.13, 0.18, 0.09, 0.11, 
    0.17, 0.11, 0.17, 0.15, 0.28, 0.12), run_ID.1 = c(1L, 2L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
    29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
    41L, 42L, 43L), run_name = structure(1:42, .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"), date = structure(c(18170, 
    18172, 18173, 18173, 18176, 18176, 18177, 18178, 18179, 18179, 
    18180, 18180, 18183, 18183, 18184, 18186, 18187, 18187, 18191, 
    18193, 18194, 18197, 18198, 18199, 18200, 18200, 18204, 18206, 
    18207, 18208, 18212, 18214, 18215, 18215, 18218, 18218, 18219, 
    18220, 18221, 18221, 18222, 18225), class = "Date")), class = "data.frame", row.names = c(NA, 
-42L))

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

ui <- fluidPage(
  # add the title panel
  titlePanel(title = "NGS run stats metrics"),

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

      # Input date range
      dateRangeInput(inputId = "in_date", label = "select a date range", min = min(runStats$date), max = max(runStats$date),
                     start = min(runStats$date), end = max(runStats$date))
    ),



    ## MainPanel
    mainPanel(
      tabsetPanel(type = 'tabs',
                  tabPanel(title = 'boxplot', plotOutput(outputId = 'out_boxplot', click = "plot_click"),
                           verbatimTextOutput(outputId = "out_info")))
    )

  )

)


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

server <- function(input, output) {
  # define the reactive table w/o filtering
  df = reactive(runStats) 

  # filter df on the selected runID
  filtered = reactive(df() %>% 
                        filter(run_name==input$in_runID))

  # filter the df based on the selected dates
  df_date = reactive(df() %>% filter(date >= input$in_date[1],
                                         date <= input$in_date[2]))

  # define the threshold value based on input$in_variable
  # more at https://stackoverflow.com/questions/37710358/how-to-create-if-statement-with-reactive-values-in-r-shiny?rq=1
  var = reactive(input$in_variable)
  t = reactive(if (var()=="percentage_cluster_post_filter")
    50 else if (var()=="percentage_reads_w_quality_greater_than_Q30")
      90 else if (var()=="percentage_aligned_phiX")
        0.1 else 0.6)



  # The boxplot
  # here I should have the highlighting dot to show where the value of the selected run is
  output$out_boxplot = renderPlot({
    ggplot(data = df_date(), aes_string(y=input$in_variable)) + geom_boxplot() + 
      # the highlighted dot from a selected run
      geom_point(aes(x=0, y=df_date() %>% filter(run_name==input$in_runID) %>% pull(input$in_variable)), colour="red", size=3.5) + 
      ylab(input$in_variable) +
      theme(axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank()) +
      # threshold line
      geom_hline(yintercept = t(), colour='red', linetype='dashed')
  })

  # the table below to display the information on the clicked point on the boxplot
  output$out_info = renderPrint({
    nearPoints(df = df_date(), coordinfo = input$plot_click, yvar = input$in_variable)
  })

}

shinyApp(ui, server)
如果我用

nearPoints(df = df_date(), coordinfo = input$plot_click, xvar = 0, yvar = input$in_variable)
正如我在箱线图中所看到的,我得到:

Error: nearPoints: `xvar` ('0')  not in names of input
nearPoints()


有人知道解决方案吗?

您是否尝试在数据帧中放置一列零?是的,这是我最后一次尝试,现在可以使用了。但实际上,我的错误语法是
output$out\u info=renderPrint({nearPoints(df=df\u date(),coordinfo=input$plot\u click,xvar=x,yvar=input$in\u variable)}
而我应该用引号将x括起来:
output$out\u info=renderPrint({nearPoints(df=df\u date()),coordinfo=input$plot\u click,xvar='x',yvar=input$in\u variable)})
Error: nearPoints: `xvar` ('0')  not in names of input