Commit 093f08df authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

cleaned up the if clauses & outsourced scatter plot for shiny and statical mode

parent 77310e60
......@@ -17,12 +17,12 @@ x_title <- names({{ env_id }}$x[1])
y_value <- {{ env_id }}$y[[1]]
y_title <- names({{ env_id }}$y[1])
if(!is.null({{ env_id }}$colour_by)) {
colour_value <- {{ env_id }}$colour_by[[1]]
df <- data.frame({{ env_id }}$x, {{ env_id }}$y, {{ env_id }}$colour_by)
} else {
df <- data.frame({{ env_id }}$x, {{ env_id }}$y)
}
# Create data.frame for download
df_list <- list()
if (!is.null({{ env_id }}$labels)) df_list <- rlist::list.append(df_list, {{ env_id }}$labels)
df_list <- rlist::list.append(df_list, c({{ env_id }}$x, {{ env_id }}$y))
if (!is.null({{ env_id }}$colour_by)) df_list <- rlist::list.append(df_list, {{ env_id }}$colour_by)
df <- do.call("data.frame", df_list)
# devtools::install_github('yihui/xfun')
# embed_file() requires a few more packages
......@@ -33,19 +33,23 @@ embed_var = function(x, ...) {
write.csv(x, f)
xfun::embed_file(f, text = 'Download full data as .csv', ...)
}
# download_link
embed_var(df)
p <- plotly::plot_ly(data.frame(x_value, y_value, colour_value = I("black")),
x = x_value,
y = y_value,
color = colour_value
)
p <- plotly::layout(p,
xaxis = list(title = x_title),
yaxis = list(title = y_title))
p
# set variables
x <- {{ env_id }}$x[1]
y <- {{ env_id }}$y[1]
if (!is.null({{ env_id }}$colour_by)){
colour_by <- {{ env_id }}$colour_by[1]
} else {
colour_by <- NULL
}
################## Test scatter_plot funktion - funktioniert #######################
p <- i2dash.scrnaseq::.scatter_plot(labels = {{ env_id }}$labels, x = x, y = y, colour_by = colour_by)
p
```
```{r, eval=is_shiny}
......@@ -108,85 +112,84 @@ fillCol(flex = c(NA, 1),
)
output$plot_{{ env_id }} <- plotly::renderPlotly({
if (!{{ env_id }}$x_selection){
x_value <- {{ env_id }}$x[[1]]
x_title <- names({{ env_id }}$x[1])
x <- {{ env_id }}$x[1]
} else {
x_value <- {{ env_id }}$x[[input$select_x_{{ env_id }}]]
x_title <- input$select_x_{{ env_id }}
x <- {{ env_id }}$x[input$select_x_{{ env_id }}]
}
if (!{{ env_id }}$y_selection){
y_value <- {{ env_id }}$y[[1]]
y_title <- names({{ env_id }}$y[1])
y <- {{ env_id }}$y[1]
} else {
y_value <- {{ env_id }}$y[[input$select_y_{{ env_id }}]]
y_title <- input$select_y_{{ env_id }}
y <- {{ env_id }}$y[input$select_y_{{ env_id }}]
}
# set variables
if (!{{ env_id }}$x_selection) x <- {{ env_id }}$x[1] else x <- {{ env_id }}$x[input$select_x_{{ env_id }}]
if (!{{ env_id }}$y_selection) y <- {{ env_id }}$y[1] else y <- {{ env_id }}$y[input$select_y_{{ env_id }}]
if (!{{ env_id }}$colour_by_selection){
if (!is.null({{ env_id }}$colour_by)){
colour_value <- {{ env_id }}$colour_by[[1]]
colour <- {{ env_id }}$colour_by[1]
colour_by <- {{ env_id }}$colour_by[1]
} else {
colour_value <- NULL
colour_by <- NULL
}
} else {
colour_value <- {{ env_id }}$colour_by[[input$select_colour_{{ env_id }}]]
colour <- {{ env_id }}$colour_by[input$select_colour_{{ env_id }}]
colour_by <- {{ env_id }}$colour_by[input$select_colour_{{ env_id }}]
}
# create data.frame for plot
plot_list <- list()
if (!is.null({{ env_id }}$labels)) plot_list <- rlist::list.append(plot_list, {{ env_id }}$labels)
plot_list <- rlist::list.append(plot_list, c(x, y))
if (!is.null({{ env_id }}$colour_by)) plot_list <- rlist::list.append(plot_list, colour)
plot_df <- do.call("data.frame", plot_list)
a <- NULL
labels <- NULL
if (!is.null({{ env_id }}$labels)) {
if (input$checkbox_{{ env_id }}) {
colour_value <- NULL
point_index <- match(input$select_name_{{ env_id }}, plot_df[[1]])
point <- plot_df[point_index,]
a <- list(
x = point[,2],
y = point[,3],
text = point[,1],
xref = "x",
yref = "y",
showarrow = T,
arrowhead = 7,
arrowcolor = "red",
ax = 20,
ay = -40
)
}
}
if (!is.null({{ env_id }}$labels)) labels <- plot_df[[1]]
p <- plotly::plot_ly(plot_df,
x = x_value,
y = y_value,
color = colour_value,
text = labels
)
p <- plotly::add_markers(p)
p <- plotly::layout(p,
xaxis = list(title = x_title),
yaxis = list(title = y_title),
annotations = a)
################## Test scatter_plot funktion - funktioniert #######################
p <- i2dash.scrnaseq::.scatter_plot(labels = {{ env_id }}$labels, x = x, y = y, colour_by = colour_by, checkbox = input$checkbox_{{ env_id }}, selected_label = input$select_name_{{ env_id }})
p
})
})
###################### Funktionierende Variante ohne scatter_plot Funktion ############
#
# # create data.frame for plot
# plot_list <- list()
# if (!is.null({{ env_id }}$labels)) plot_list <- rlist::list.append(plot_list, {{ env_id }}$labels) else plot_list <- rlist::list.append(plot_list, c(1:length(x[[1]])))
# plot_list <- rlist::list.append(plot_list, c(x, y))
# if (!is.null({{ env_id }}$colour_by)) plot_list <- rlist::list.append(plot_list, colour_by) else plot_list <- rlist::list.append(plot_list, c(1:length(x[[1]])))
# plot_df <- do.call("data.frame", plot_list)
#
# labels <- plot_df[[1]]
# x_value <- plot_df[[2]]
# y_value <- plot_df[[3]]
# colour_by <- plot_df[[4]]
#
# x_title <- names(plot_df[[2]])
# y_title <- names(plot_df[[3]])
#
# if(is.null({{ env_id }}$labels)) {
# labels <- NULL
# }
# if(is.null({{ env_id }}$colour_by)) {
# colour_by <- NULL
# }
# #find point for annotation
# a <- NULL
# if (!is.null({{ env_id }}$labels)) {
# if (input$checkbox_{{ env_id }}) {
# colour_by <- NULL
# point_index <- match(input$select_name_{{ env_id }}, plot_df[[1]])
# point <- plot_df[point_index,]
# a <- list(
# x = point[,2],
# y = point[,3],
# text = point[,1],
# xref = "x",
# yref = "y",
# showarrow = T,
# arrowhead = 7,
# arrowcolor = "red",
# ax = 20,
# ay = -40
# )
# }
# }
#
# # plotly
# p <- plotly::plot_ly(plot_df,
# x = x_value,
# y = y_value,
# color = colour_by,
# text = labels
# )
# p <- plotly::add_markers(p)
# p <- plotly::layout(p,
# xaxis = list(title = x_title),
# yaxis = list(title = y_title),
# annotations = a)
# p
# })
```
***
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment