boxplot.Rmd 2.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13

### {{ title }}

<!-- Component created on {{ date }} -->

```{r}
{{ env_id }} = readRDS("envs/{{ env_id }}.rds")

is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```

```{r, eval=!is_shiny}
# set variables
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
14
15
16
17
18
19
20
x <- {{ env_id }}$x[1]
group_by <- {{ env_id }}$group_by[1]
if(is.null(group_by)){
  df <- data.frame(x)
} else {
  df <- data.frame(x, group_by)
}
21
22

# Provide data download
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
23
i2dash::embed_var(df)
24
25

# Render plot
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
26
i2dash.scrnaseq::plotly_boxplot(df = df, group_by = {{ env_id }}$group_by, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
```

```{r, eval=is_shiny}
ui_list <- list()

# selection field for x
if ({{ env_id }}$x_selection) {
  ui_list <- rlist::list.append(ui_list,
                                selectInput("select_x_{{ env_id }}", label = "Select observations:",
                                            choices = names({{ env_id }}$x)))
}

# selection field for group_by
if ({{ env_id }}$group_by_selection) {
  ui_list <- rlist::list.append(ui_list,
                                selectInput("select_group_by_{{ env_id }}", label = "Group observations by:",
                                            choices = names({{ env_id }}$group_by)))
}

arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
46
47
48
# download_link
ui_list <- rlist::list.append(ui_list, tags$div(tags$br(), downloadButton('downloadData_{{ env_id }}', 'Download data')))

49
#
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
50
# Create reactive dataframe
51
#
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
52
53
54
55
56
57
58
df_{{ env_id }} <- shiny::reactive({
  
  if( !{{ env_id }}$x_selection ) {
    x <- {{ env_id }}$x[1]
  } else {
    x <- {{ env_id }}$x[input$select_x_{{ env_id }}]
  }
59

arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
60
61
62
63
64
65
66
67
68
69
70
71
  if( !{{ env_id }}$group_by_selection ) {
    group_by <- {{ env_id }}$group_by[1]
  } else {
    group_by <- {{ env_id }}$group_by[input$select_group_by_{{ env_id }}]
  }

  if(is.null(group_by)){
    df <- data.frame(x)
  } else {
    df <- data.frame(x, group_by)
  }
  return(df)
72
  })
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
73
74
75
76
77
78
79
80
81
82

#
# Download
#
output$downloadData_{{ env_id }} <- downloadHandler(
  filename =  paste('data-', Sys.Date(), '.csv', sep=''),
  content = function(file) {
    write.csv(df_{{ env_id }}(), file)
  }
)
83
84
85
86
87

#
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly(
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
88
  i2dash.scrnaseq::plotly_boxplot(df = df_{{ env_id }}(), group_by = {{ env_id }}$group_by, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
89
90
91
92
93
94
95
)

shiny::fillCol(flex = c(NA, 1),
        do.call(shiny::inputPanel, ui_list),
        plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
)
```
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
96