根据R Shiny中输入$ variable的选定值进行变量赋值

时间:2019-11-27 13:38:08

标签: r shiny

我正在构建一个闪亮的应用程序,它将在一个选项卡中显示一个箱线图,在另一个选项卡上显示另一个线图。

下面的代码做了我想要的事情,除了一件事情:我试图在两个图上用geom_hline()添加一条水平线,而yintercept的值取决于将哪个值分配给{{ 1}}

input$in_variable

我认为解决方案与# 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") ### The shiny code # 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( # select input for the variable 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) { # define the reactive table w/o filtering df = reactive(runStats) # filter df on the selected runID filtered = reactive(runStats %>% filter(run_name==input$in_runID)) # assign the values for the threshold depending on input$in_variable # THIS CODE DOES NOT WORK - HERE IS WHERE I AM TRYING TO ASSIGN A VALUE FOR THE VARIABLE threshold based on input$in_variable # IN CASE THIS WAS NOT A SHINY APP I GUESS THE VARIABLE ASSIGNMENT WOULD LOOK LIKE SO: # if(input$in_variable=="percentage_cluster_post_filter") {threshold=50} # else if (inpu$in_variable=="percentage_reads_w_quality_greater_than_Q30") {threshold=90} # else if (input$in_variable=="percentage_aligned_phiX") {threshold=0.1} # else {threshold=0.6} # The boxplot output$out_boxplot = renderPlot({ ggplot() + geom_boxplot(data = df(), aes_string(y=input$in_variable)) + geom_point(data = df(), aes(x=0, y=df() %>% 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()) }) # The trendline output$out_trend = renderPlot({ ggplot(df(), aes_string(x='run_name', y=input$in_variable, group=1)) + geom_line()+ geom_point() + geom_smooth() + theme(axis.text.x = element_text(angle = 90)) + # add the point of the selected run with aes() geom_point(data = filtered(), aes(x=filtered() %>% pull(runStats_ID), y = filtered() %>% pull(input$in_variable)), size=3.5, colour="red") }) } shinyApp(ui, server) 有关,尽管在this example中他们使用的是reactiveVal()而我的actionButtoninput$in_variable

1 个答案:

答案 0 :(得分:0)

server函数中添加以下代码

  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)

然后在需要的地方添加geom_hline()

geom_hline(yintercept = t(), colour='red', linetype='dashed')