Closed policybot2020 closed 5 months 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
However, the most recent option is
This allows to additionally store and load model objects from Pandora! Please check the app Resouces for this!
Actually, this is the way to go! And this I will add also to all of my other apps.
Save the extension: naming convention
https://github.com/Pandora-IsoMemo/data-tools/tree/main/inst/app/predefinedModels
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:
matplot(data(), type = 'l', main = input$title, xlab = input$x_label, ylab = input$y_label)
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
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.
names(importedDat())[1]
importType = "data"
importedDat()[[1]]
contains the imported data itself, usually a data.frame
importedDat()[[1]]
your_new_variable <- importedDat()[[1]]
outputAsMatrix = TRUE
(by default this is outputAsMatrix = FALSE
), the output will be a matrix and additional logic is applied to set column and row names of the output. Currently this is only relevant for the app Resources. observe({ ... }) %>%
bindEvent(importedDat())
Please, define your own function validateImport()
to validate your import. This function is not part of the package DataTools
.
I hope, this helped. Let me know if you have more questions.
@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.
@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
@arunge @policybot2020 Antonia, please suggest schedules when we can meet with Jian to solve this. Please see also my emails on this.
@isomemo I can offer a meeting on Monday and sent an invitation to @policybot2020 .
@policybot2020 @arunge thanks, Antonia! At what times can you meet?
@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.
@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 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:
the UI function: https://github.com/Pandora-IsoMemo/iso-app/blob/f7366c574c919bde7430616f00b6cc83980fad23/R/03-modelResults2D.R#L20
the server function: https://github.com/Pandora-IsoMemo/iso-app/blob/f7366c574c919bde7430616f00b6cc83980fad23/R/03-modelResults2D.R#L473C3-L521C34
In action it looks like this:
Please note that the UI might change a bit in the future dependent on the needs. I can let you know about important updates.
githubRepo
, in my casegithubRepo = "iso-app"
, one must define a folder for the model objects under the following path:inst/app/predefinedModels
. A model file (.zip) that was downloaded before must be uploaded to that folder. Than it should be selectable via the UI.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!