rarefactionCurve-server.R 2.69 KB
Newer Older
Cedric Midoux's avatar
Cedric Midoux committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
output$rarefactionCurve <- renderPlot({
  validate(need(data16S(),
                "Requires an abundance dataset"))
  p <- ggrare(
    physeq = data16S(),
    step = 100,
    #step = input$rarefactionStep,
    color = checkNull(input$rarefactionColor),
    label = checkNull(input$rarefactionLabel),
    se = FALSE
  )
  if (!is.null(checkNull(input$rarefactionGrid))) {
    p <- p + facet_grid(paste(".", "~", input$rarefactionGrid))
  }
  
  if (input$rarefactionMin) {
    p <-
      p + geom_vline(xintercept = min(sample_sums(data16S())),
                     color = "gray60")
  }
  return(p + ggtitle(input$rarefactionTitle))
})

output$rarefactionCurveUI <- renderUI({
  validate(need(data16S(), ""))
  box(
    title = "Setting : " ,
    width = NULL,
    status = "primary",
    # sliderInput(
    #   "rarefactionStep",
    #   label = "Etapes de calcul : ",
    #   min = 1,
    #   max = 1000,
    #   value = 100
    # ),
    checkboxInput("rarefactionMin", label = "Show min sample threshold", value = FALSE),
    textInput("rarefactionTitle",
              label = "Title : ",
              value = "Rarefaction curves"),
    selectInput(
      "rarefactionColor",
      label = "Color : ",
      choices = c("..." = 0, sample_variables(data16S()))
    ),
    selectInput(
      "rarefactionLabel",
      label = "Label : ",
      choices = c("..." = 0, sample_variables(data16S()))
    ),
    selectInput(
      "rarefactionGrid",
      label = "Subplot : ",
      choices = c("..." = 0, sample_variables(data16S()))
    ),
    collapsedBox(verbatimTextOutput("rarefactionCurveScript"), title = "RCode")
  )
})

output$rarefactionCurveScript <- renderText({
  scriptArgs <- c("physeq = data",
                  "step = 100",
                  "se = FALSE")
  if (!is.null(checkNull(input$rarefactionColor))) {
    scriptArgs <-
      c(scriptArgs,
        glue("color = \"{input$rarefactionColor}\""))
  }
  if (!is.null(checkNull(input$rarefactionLabel))) {
    scriptArgs <-
      c(scriptArgs,
        glue("label = \"{input$rarefactionLabel}\""))
  }
  script <- c(
    scriptHead,
    "# Plot rarefaction curves",
    glue("p <- ggrare({glue_collapse(scriptArgs, sep=', ')})")
  )
  if (!is.null(checkNull(input$rarefactionGrid))) {
    script <- c(script,
                glue("p <- p + facet_grid(\". ~ {input$rarefactionGrid}\")"))
  }
  if (input$rarefactionMin) {
    script = c(
      script,
      "p <- p + geom_vline(xintercept = min(sample_sums(data)), color = \"gray60\")"
    )
  }
  if (!is.null(checkNull(input$rarefactionTitle))) {
    script <- c(script,
                glue("p <- p + ggtitle({input$rarefactionTitle})"))
  }
  script <- c(script, "", "plot(p)")
  
  return(glue_collapse(script, sep = "\n"))
})