Run multiple render functions on user uploaded file simultaneously in R shiny - r

I have multiple render functions within the R shiny code performing independent tasks (multiple output plots) on the user uploaded file. I would like all these render functions to start running in parallel when the user uploads the file. How could I do that in R shiny?

You can use reactive with file upload button to store a data, and call it in every renderPlot function :
And you should paste your code what you have done so far on Stack Overflow, it will be more easier to help:
df<-reactive({ read.csv(path) })
# call the csv file with df() rather than df when you use reactive to store it
output$plot1<-renderPlot({plot(df()[,1])})
output$plot2<-renderPlot({plot(df()[,2])})
Something like this

Related

Load a csv file in R and share in all sessions?

I have a shiny application that uses a csv file to generate different figures. I upload my application in my personal linux server using shiny-server.
I use this structure for my application
global.R
ui.R
server.R
Inside my global.R file I have this line, which help me to load and read my csv file
df <- read_csv("../Desktop/covid_2021-02-15.csv")
But my application is very slow, I read that objects in the global.R script, are read only one time and are share in all sessions.
Is there other way to load this data frame to have a more efficient application?
In addition to the comments from Gregor and HubertL:
To load large CSV files can slow down. I had the same issue and changed to r binary file (rds) with saveRDS() and readRDS(). As a first step you can try rds file and see if the issue is solved.
To check whether there is a performance difference you can use system.time(). Returns the time taken to evaluate any R expression.
In your case:
df <- read_csv("../Desktop/covid_2021-02-15.csv")
# Save an object to a file
saveRDS(df, file = "my_data.rds")
# Restore the object
readRDS(file = "my_data.rds")

How to work with parametrised Shiny R HTML output element

In my shiny app I am using multiple parametrised variables that depend on a csv configuration file that lists all the IDs needed for the shiny HTML output elements.
In my ui the process is very easy. I do as follows:
htmlOutput(outputId = paste0("Variable",y))
where y is the looping variable over the IDs
The problem is on the server side of the shiny app when i want to render this HTML element. The way how I am currently doing so is using the eval parse method. The biggest drawback of that solution is that it consumes a lot of time (in addition to a higher difficulty in reading the R code). An example of rendering the above object:
eval(parse(text=paste0("output$Variable",y,"<-renderText({return(paste(\"<div style=\\\"text-align: center;\\\">\",",result,",\"</div>\"))})")))
Is there any other more efficient way to render parametrised variable names on the server side of my Shiny App?
I tried to use get and assign functions but neither of them worked.
Thanks for your help!

Is there a way to create an RMarkdown document from within an R function?

As part of my data analysis workflow, I want to create a function that takes data as input and returns an Rmarkdown document with the output of a bunch of analyses. I don't understand how to do this very directly, though.
I guess I could create a second r script or .rmd scipt with the code to prepare the html output file and then have the r function write the data to disk with a specific filename to be used in the analysis and then have the function knitr::spin or knitr::knit the 2nd script into the output file. But is there a more direct way of doing this from within the same script?

Web application which runs R

I have written my R script with some functions to do some calculations. On the output of my calculations, I have created plots in shiny(4 tab panels in that as of now, which is using the data specified in my global.R). Now I am trying to automate the process and deliver it to non technical users. So something like, users feed in 2 csv files in web UI and click a button in the web UI, which will then call my r scripts to do the calculations, then my server.R script should build plots on the output of it. Any similar examples? I tried different links, but nothing seems to be relevant.
If I add my R script using source(**.R) and use the functions mentioned in there. Or Do I still have to save output in data folder and specify something in global.R or just use the output of the function in my plot construction?
If I have the fileInput option in first tab in my tabpanels, how can I make stop the user from viewing the other tabs(tabs with graphs) without loading the input csv files and clicking the "Go" button?
Can I do this whole thing in Shiny ? Or better to go for rook or some other framework which calls my r script just for calculations?

R shiny updateSelectInput in observeEvent not working

In my Shiny app my users can upload a file from the server to view the results. I have the user select the file using a selectInput, and then they click an actionButton to load the file. I also want users to be able to delete files and add new files, and I've set that up successfully using separate actionButtons. When the user deletes or adds a file I want to update the selectInput so the deleted files aren't there anymore, and the added files are. I tried this using updateSelectInput in my observeEvent code but its not working. Here's my observeEvent for the delete file portion:
#Delete parameter files
observeEvent(input$delete,{
file.remove(input$input_file) #deletes the file selected in the input_file widget
choices <- list.files("C:/Users/shiny/testapp/", pattern="*.Rdata")
updateSelectInput(session,"input_file",choices) #supposed to update the input_file widget
})
When I run the app with this code in it, two things happen. In the App window, I get this text right above the input_file widget: [object Object
And in my R console I get:
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future
version of jsonlite, this option will not be supported, and named
vectors will be translated into arrays instead of objects. If you want
JSON object output, please use a named list instead. See ?toJSON.
I have included the session parameter in my shinyServer call:
shinyServer(function(input, output, session)
Any advice would be appreciated.
I found a corner case that also causes this bug. Upon putting tabPanel() in a renderUI(), an updateSelectInput in an unrelated area of my code just stopped working.
To resolve, leave the tabPanel in ui.R, and use renderUI() to adjust within the function instead of bringing it all into server.R, like here: updateSelectInput order of operations/race condition

Resources