dreamRs / esquisse

RStudio add-in to make plots interactively with ggplot2
https://dreamrs.github.io/esquisse
Other
1.77k stars 229 forks source link

Can't handle both GlobalEnv and ImportFile dataModule options in the same app #105

Closed clbenoit closed 4 years ago

clbenoit commented 4 years ago

Hi,

I would like to let the user choose himself between using example datasets and import his proper one.

I tried something like this :

server <- function(input, output, session) {

  observeEvent(input$dataType, {

    if (input$dataType == "Upload my datasets") {
      callModule(module = esquisserServer, id = "esquisse", dataModule = 'ImportFile')
    } else if (input$dataType == "Example datasets") {

      callModule(module = esquisserServer, id = "esquisse", dataModule = "GlobalEnv")    
    }
  })

}

ui <- fluidPage(

  selectInput(inputId = "dataType",label = "dataType", choices = c("Example datasets","Upload my datasets"), selected = 'Upload my datasets'),

  esquisserUI(
      id = "esquisse", 
      header = FALSE, # dont display gadget title
      choose_data = TRUE, # dont display button to change data,
      container = esquisseContainer(height = "700px")
    )

)

shinyApp(ui, server)

Both options are working separately if i use them just after launching the app. But If i have already updated one of this dataType, I can't switch to the other option then....

Thank you so much for you help, Does anyone have and idea ?

Clément

clbenoit commented 4 years ago

I get the following error Warning: Error in [.data.frame: undefined columns selected

pvictor commented 4 years ago

Hello, You should avoid using callModule in reactive context such as observeEvent, you're trying to register the module twice, that's why it dont work. You can do something like this outside the module, and only send to the module the reactive dataset chosen. Here an example:

library(shiny)
library(esquisse)

server <- function(input, output, session) {

  r_example_data <- callModule(
    module = chooseDataServer, 
    id = "example_data",
    launchOnStart = FALSE,
    dataModule = "GlobalEnv"
  )

  r_upload_data <- callModule(
    module = chooseDataServer, 
    id = "upload_data",
    launchOnStart = FALSE,
    dataModule = "ImportFile"
  )

 # r_data will be passed to esquisse module, its value contain the last dataset chosen by user 
  r_data <- reactiveValues(data = data.frame(), name = "")
  observeEvent(r_example_data$data, {
    r_data$data <- r_example_data$data
    r_data$name <- r_example_data$name
  })
  observeEvent(r_upload_data$data, {
    r_data$data <- r_upload_data$data
    r_data$name <- r_upload_data$name
  })

  callModule(module = esquisserServer, id = "esquisse", data  = r_data)    

}

ui <- fluidPage(
  # To align the 2 buttons
  tags$div(
    class = "btn-group btn-group-justified",
    tags$div(
      class = "btn-group",
      chooseDataUI(id = "example_data", label = "Example datasets")
    ),
    tags$div(
      class = "btn-group",
      chooseDataUI(id = "upload_data", label = "Upload my datasets")
    )
  ),
  esquisserUI(
    id = "esquisse", 
    header = FALSE, 
    choose_data = FALSE, 
    container = esquisseContainer(height = "700px")
  )

)

shinyApp(ui, server)

Victor

clbenoit commented 4 years ago

It is wonderfully working, thanks a lot Victor =)

Clément