scatterplot.Rmd 4.73 KB
Newer Older
arsenij.ustjanzew's avatar
arsenij.ustjanzew 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

### {{ 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}
# Create data.frame
df <- data.frame(matrix(nrow = length({{ env_id }}$x[[1]]), ncol = 4, dimnames = list(c(1:length({{ env_id }}$x[[1]])), c("labels", names({{ env_id }}$x[1]), names({{ env_id }}$y[1]), "colour_by"))))

if (!is.null({{ env_id }}$labels)) {
  df[1] <- {{ env_id }}$labels
  }
df[2] <- {{ env_id }}$x[[1]]
df[3] <- {{ env_id }}$y[[1]]
if (!is.null({{ env_id }}$colour_by)) {
  df[4] <- {{ env_id }}$colour_by[[1]]
  colnames(df)[4] <- names({{ env_id }}$colour_by[1])
}

# Provide data for download
i2dash::embed_var(df)

# Render plot
i2dash.scrnaseq::plotly_scatterplot(df = df, labels = {{ env_id }}$labels, colour_by = {{ env_id }}$colour_by)
```

```{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 data for x axis:",
                                            choices = names({{ env_id }}$x)))
}

# selection field for y
if ({{ env_id }}$y_selection){
  ui_list <- rlist::list.append(ui_list,
                                selectInput("select_y_{{ env_id }}", label = "Select data for y axis:",
                                            choices = names({{ env_id }}$y)))
}

# selection field for colour_by
if ({{ env_id }}$colour_by_selection){
  ui_list <- rlist::list.append(ui_list,
                                selectInput("select_colour_{{ env_id }}", label = "Select colouring:",
                                            choices = names({{ env_id }}$colour_by)))
}

56
57
58
59
60
61
62
63
# Checkbox and selection field for colour by feature
if (!is.null({{ env_id }}$expression)) {
  ui_list <- rlist::list.append(ui_list,
                                tags$div(checkboxInput("expr_checkbox_{{ env_id }}", label = "Colour by feature", value = FALSE),
                                         selectInput("select_feature_{{ env_id }}", label = NULL, choices = rownames({{ env_id }}$expression))
  ))
}

arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
64
65
66
67
68
69
70
71
# Checkbox and selection field for colour by sample name
if (!is.null({{ env_id }}$labels)) {
  ui_list <- rlist::list.append(ui_list,
                                tags$div(checkboxInput("checkbox_{{ env_id }}", label = "Colour by sample name", value = FALSE),
                                         selectInput("select_name_{{ env_id }}", label = NULL, choices = {{ env_id }}$labels)
  ))
}

72
# Download link
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
ui_list <- rlist::list.append(ui_list, tags$div(tags$br(), downloadButton('downloadData_{{ env_id }}', 'Download data')))

#
# Create reactive dataframe
#
df_{{ env_id }} <- shiny::reactive({
  # Set values for 'labels'
  if (!is.null({{ env_id }}$labels)) {
    labels <- {{ env_id }}$labels
  } else {
    labels <- c(1:length({{ env_id }}$x[[1]]))
  }
  # Set values for 'x'
  if( !{{ env_id }}$x_selection ) {
    x <- {{ env_id }}$x[1]
  } else {
    x <- {{ env_id }}$x[input$select_x_{{ env_id }}]
  }
  # Set values for 'y'
  if( !{{ env_id }}$y_selection ) {
    y <- {{ env_id }}y[1]
  } else {
    y <- {{ env_id }}$y[input$select_y_{{ env_id }}]
  }
  # Set values for 'colour_by'
  if (!{{ env_id }}$colour_by_selection){
    colour_by <- {{ env_id }}$colour_by[1]
  } else {
    colour_by <- {{ env_id }}$colour_by[input$select_colour_{{ env_id }}]
  }
  
  # Create a data.frame
  df <- data.frame(labels, x, y)
  
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  if(!is.null(input$expr_checkbox_{{ env_id }})){
    if(input$expr_checkbox_{{ env_id }}){
      df["colour_by"] <- {{ env_id }}$expression[input$select_feature_{{ env_id }},]
    } else {
      if(!is.null({{ env_id }}$colour_by)){
        df["colour_by"] <- colour_by
      } else {
        df["colour_by"] <- c(1:length({{ env_id }}$x[[1]]))
      }
    }
  } else{
    if(!is.null({{ env_id }}$colour_by)){
      df["colour_by"] <- colour_by
    } else {
      df["colour_by"] <- c(1:length({{ env_id }}$x[[1]]))
    }
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  }
  return(df)
})

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

#
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly(
141
  i2dash.scrnaseq::plotly_scatterplot(df = df_{{ env_id }}(), labels = {{ env_id }}$labels, colour_by = {{ env_id }}$colour_by, checkbox = input$checkbox_{{ env_id }}, selected_label = input$select_name_{{ env_id }}, expression = {{ env_id }}$expression, expr_checkbox = input$expr_checkbox_{{ env_id }})
arsenij.ustjanzew's avatar
arsenij.ustjanzew committed
142
143
144
145
146
147
148
149
150
151
152
153
)

#
# Layout of component
#
shiny::fillCol(flex = c(NA, 1),
        do.call(shiny::inputPanel, ui_list),
        plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
)
```