Skip to content

Commit

Permalink
fixed setting aesthetics
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Mar 29, 2024
1 parent 8c0e385 commit c1bb0c6
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions R/esquisse-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ esquisse_server <- function(id,
import_from = c("env", "file", "copypaste", "googlesheets", "url"),
drop_ids = TRUE,
notify_warnings = NULL) {

moduleServer(
id = id,
module = function(input, output, session) {

ns <- session$ns
ggplotCall <- reactiveValues(code = "")
data_chart <- reactiveValues(data = NULL, name = NULL)
geom_rv <- reactiveValues(possible = "auto", controls = "auto", palette = FALSE)

# Settings modal (aesthetics choices)
observeEvent(input$settings, {
showModal(modal_settings(aesthetics = input$aesthetics))
})

if (is.reactivevalues(data_rv)) {
observeEvent(data_rv$data, {
data_chart$data <- data_rv$data
Expand All @@ -69,7 +69,7 @@ esquisse_server <- function(id,
data_chart$data <- data_rv
data_chart$name <- if (is.character(name)) name
}

# Launch import modal if no data at start
if (!is.null(import_from)) {
observe({
Expand All @@ -82,7 +82,7 @@ esquisse_server <- function(id,
}
})
}

# Launch import modal if button clicked
observeEvent(input$launch_import_data, {
datamods::import_modal(
Expand All @@ -91,31 +91,32 @@ esquisse_server <- function(id,
title = i18n("Import data to create a graph")
)
})

# Data imported and update rv used
data_imported_r <- datamods::import_server("import-data", return_class = "tbl_df")
observeEvent(data_imported_r$data(), {
data <- data_imported_r$data()
data_chart$data <- data
data_chart$name <- data_imported_r$name() %||% "imported_data"
})

# show data if button clicked
show_data_server("show_data", reactive(controls_rv$data))




res_geom_aes_r <- select_geom_aes_server(
id = "geomaes",
data_r = reactive(data_chart$data)
id = "geomaes",
data_r = reactive(data_chart$data),
aesthetics_r = reactive(input$aesthetics)
)
aes_r <- reactive(res_geom_aes_r()$aes_1)
observeEvent(res_geom_aes_r()$geom_1, {
geom_rv$controls <- res_geom_aes_r()$geom_1
})


# Module chart controls : title, xlabs, colors, export...
# paramsChart <- reactiveValues(inputs = NULL)
controls_rv <- controls_server(
Expand Down Expand Up @@ -156,55 +157,55 @@ esquisse_server <- function(id,
height = reactive(rv_render_ggplot$plot_height),
drop_ids = drop_ids
)


rv_render_ggplot <- render_ggplot(
id = "plooooooot",
id = "plooooooot",
{
req(input$play_plot, cancelOutput = TRUE)
req(data_chart$data)
req(controls_rv$data)
req(controls_rv$inputs)
geom_ <- req(res_geom_aes_r()$geom_1)

aes_input <- make_aes(aes_r())

req(unlist(aes_input) %in% names(data_chart$data))

mapping <- build_aes(
data = data_chart$data,
.list = aes_input,
geom = geom_
)

geoms <- potential_geoms(
data = data_chart$data,
mapping = mapping
)
req(geom_ %in% geoms)

data <- controls_rv$data

scales <- which_pal_scale(
mapping = mapping,
palette = controls_rv$colors$colors,
data = data,
reverse = controls_rv$colors$reverse
)

if (identical(geom_, "auto")) {
geom <- "blank"
} else {
geom <- geom_
}

geom_args <- match_geom_args(
geom_,
geom_,
controls_rv$inputs,
mapping = mapping,
add_mapping = FALSE
)

if (isTRUE(controls_rv$smooth$add) & geom_ %in% c("point", "line")) {
geom <- c(geom, "smooth")
geom_args <- c(
Expand Down Expand Up @@ -232,20 +233,20 @@ esquisse_server <- function(id,
mapping$ymin <- NULL
mapping$ymax <- NULL
}

scales_args <- scales$args
scales <- scales$scales

if (isTRUE(controls_rv$transX$use)) {
scales <- c(scales, "x_continuous")
scales_args <- c(scales_args, list(x_continuous = controls_rv$transX$args))
}

if (isTRUE(controls_rv$transY$use)) {
scales <- c(scales, "y_continuous")
scales_args <- c(scales_args, list(y_continuous = controls_rv$transY$args))
}

xlim <- if (isTRUE(controls_rv$limits$x)) {
controls_rv$limits$xlim
}
Expand All @@ -271,27 +272,27 @@ esquisse_server <- function(id,
xlim = xlim,
ylim = ylim
)

ggplotCall$code <- deparse2(gg_call)
ggplotCall$call <- gg_call

ggplotCall$ggobj <- safe_ggplot(
expr = expr((!!gg_call) %+% !!sym("esquisse_data")),
data = setNames(list(data, data), c("esquisse_data", data_chart$name)),
show_notification = notify_warnings %||% input$notify_warnings %||% "once"
)
ggplotCall$ggobj$plot
},
},
filename = "esquisse-plot",
width = reactive(controls_rv$width),
width = reactive(controls_rv$width),
height = reactive(controls_rv$height),
use_plotly = reactive(controls_rv$plotly)
)


# Close addin
observeEvent(input$close, shiny::stopApp())

# Ouput of module (if used in Shiny)
output_module <- reactiveValues(code_plot = NULL, code_filters = NULL, data = NULL)
observeEvent(ggplotCall$code, {
Expand All @@ -301,9 +302,9 @@ esquisse_server <- function(id,
output_module$code_filters <- controls_rv$code
output_module$data <- controls_rv$data
}, ignoreInit = TRUE)

return(output_module)
}
)

}

0 comments on commit c1bb0c6

Please sign in to comment.