Pandora-IsoMemo / CausalR

GNU General Public License v3.0
0 stars 0 forks source link

Function to save all model selection into cache (ask Antonia) #20

Closed policybot2020 closed 5 months ago

arunge commented 1 year ago

@policybot2020 Similar to the importModule, there is a module for downloading and uploading model results (or only the model inputs):

You can find an example on how to use it here:

In action it looks like this:

image

Please note that the UI might change a bit in the future dependent on the needs. I can let you know about important updates.

Please, try to test this module and let me know about any issues. I am not sure if errors are thrown if the folder on github is missing, or if than the input in (2) is just empty. I do not remember if I tested this.

It would be great if you first try to use the module and let me know about any detailed questions. Thanks!

arunge commented 1 year ago

It is also possible to use only individual parts of this module either (1), (2), or (3). You can find all respective (sub) modules in this file: https://github.com/Pandora-IsoMemo/data-tools/blob/main/R/02-downAndUpload.R

arunge commented 1 year ago

However, the most recent option is

This allows to additionally store and load model objects from Pandora! Please check the app Resouces for this!

image

Actually, this is the way to go! And this I will add also to all of my other apps.

policybot2020 commented 1 year ago

Save the extension: naming convention

policybot2020 commented 11 months ago

Antonia Runge Links to test import model function

https://github.com/Pandora-IsoMemo/resources/blob/7f3b3d6824285e8f08f909d4a8f01b5e499403a0/R/01-server.R#L99C3-L113C35

https://github.com/Pandora-IsoMemo/resources/blob/7f3b3d6824285e8f08f909d4a8f01b5e499403a0/R/02-modules-downAndUploadModel.R#L35

https://github.com/Pandora-IsoMemo/data-tools/tree/main/inst/app/predefinedModels

policybot2020 commented 11 months ago

Hi @arunge ,

I have tried to implement the Pandora Platform Connection Module importDataServer. But still does not function.

Here is my original code that works that is a react function and returns df:

Code (1):

 data <- reactive({
    req(input$file)
    inFile <- input$file
    if (endsWith(inFile$name, ".csv")) {
      df <- read.csv(inFile$datapath, header = input$header)
    } else if (endsWith(inFile$name, ".xlsx") || endsWith(inFile$name, ".xls")) {
      df <- read_excel(inFile$datapath)
    } else {
      return(NULL)
    }
    if (input$treat_dates){
      return(df)
    } else {
      df <- subset(df, select = c(y, x1))
      return(df)
    }
  })

here is the code you helped me develop which uses the importDataServer module which is inside Observe() function: Code (2):


  importedDat <- importDataServer("pandora_dat")
  fileImport <- reactiveVal(NULL)
  observe({
    if (length(importedDat()) == 0 ||  is.null(importedDat()[[1]])) fileImport(NULL)
    req(length(importedDat()) > 0, !is.null(importedDat()[[1]]))
    df <- reactive({importedDat()[[1]]})
    valid <- validateImport(data, showModal = TRUE)
    if (!valid){
      showNotification("Import is not valid.")
      fileImport(NULL)
    } else {
      fileImport(df)
    }
    fileImport(df)
  }) %>% bindEvent(importedDat())

I have issues implementing this code above, because my original code (1) is a reactive function, and I don't know how to turn your code into a reactive function and return the dataframe, which I use later in server function data() to graph plots outputs: For example:

So, I need to turn the code (2) into a reactive function, so that I can use it in other server functions.

I tried after the }) %>% bindEvent(importedDat()): using this....data <- reactive({return(df)})

I have tried many times and cannot do it on my own, Can you give me some advice or recommendations?

please advise me,

Jian

arunge commented 11 months ago

Hi @policybot2020

The object importedDat() that is returned by the module is a reactiveVal(). If you set a browser() and debug the

  observe({ ... }) %>% 
    bindEvent(importedDat())

you can easily check what kind of data is returned by the module.

In order to understand how to transform reactive expressions into reactiveVal() or reactiveValues or back, please check: https://mastering-shiny.org/reactivity-objects.html

If the import does not fail, than the object importedDat() is a named list. It currently contains only one element.

Import of data files: use the default option: importType = "data"

I hope, this helped. Let me know if you have more questions.

arunge commented 11 months ago

@policybot2020 Please let me know of any progress regarding the import module. If you managed to use the module to import a data file, I would continue to explain how model objects can be downloaded and uploaded with modules from the DataTools package.

policybot2020 commented 11 months ago

@isomemo @arunge I have no progress, I have tried many ways. but still have no progress. here is a video that explains my issue: https://www.youtube.com/watch?v=efBVI1HapRM&t=50s

maybe you can help me review my code. This is under the branch feat/pandoraUpload

isomemo commented 11 months ago

@arunge @policybot2020 Antonia, please suggest schedules when we can meet with Jian to solve this. Please see also my emails on this.

arunge commented 11 months ago

@isomemo I can offer a meeting on Monday and sent an invitation to @policybot2020 .

isomemo commented 11 months ago

@policybot2020 @arunge thanks, Antonia! At what times can you meet?

arunge commented 11 months ago

@policybot2020 I opened a pull request regarding the import of data. Please test and merge the changes.

As I mentioned, understanding the differences between reactive(), reactiveVal() and reactiveValues() is crucial when developing in Shiny. Please, check https://mastering-shiny.org/reactivity-objects.html to become more familiar with the concept of reactivity.

In the meeting on Monday we can discuss how to proceed with the download of models and their upload.

arunge commented 11 months ago

@policybot2020 I opened a pull request for down- and uploading model objects. Feel free to test and merge the changes.

@isomemo and @policybot2020 I noticed that this shiny app is not an R package. We usually work only with R package when developing shiny apps. It offers version control, simpler debugging, testing of previous versions, and applying tests to basic functionalities of the package (which is automated and runs before every deployment). I would recommend to create an R package that contains this shiny app. It saves a lot of time to deploy an R package and having tests that detect errors or missing dependencies in advance.

policybot2020 commented 5 months ago

23 this pull request to as solved this saving model issue . now all models can be saved. @isomemo