For an R Shiny web app, what are some good ways to run code profiling that show the parts of the Shiny code that are taking the most processing time?
I've got a big, fat, complex Shiny app, and I'd like to figure out where in this labyrinth of code I'm slowing my Shiny app down the most. I've tried out Rprof and profr but haven't gotten much insight from them.
You need an R server in order to execute a shiny app. There is no way to convert it into "pure HTML" and run the interactivity via javascript. The reason for that is that shiny apps will have to execute R code at runtime and javascript does not know how to deal with that.
Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair. The source code for both of these components is listed below.
To do this, simply execute the runApp() command inside of profvis . For instance, we can run one of shiny's built-in examples using the runExample command (which is a wrapper for runApp ). Your Shiny application will launch, and after interacting and closing the app a profile will be generated.
I think this question needs a little update, therefore I am adding another answer to it...
You can use the package profvis
to profile shiny apps as well. It will give flame graphs directly for your R code. I.e. no need to use Chrome's flame graphs and guess where the bottleneck is. You will know exactly where to change your code.
Here is how to do it:
Details for certain steps are added below:
Step 1: Run profvis
library(profvis)
profvis({ runApp('directory_of_shiny_app') }
, prof_output = '/directory_to_save_profile')
Step 5: Load your profile
profvis(prof_input = '/path_to_save_output/random_name.Rprof')
N.B. Profvis gives a random name to your file. So you need to change the input path accordingly
Step 6: Convert to html
This step might be needed, if you have a huge app and flame graph gets a little bit longer. You might get an error "Pandoc:... memory"
p <- profvis(prof_input = '/path_to_save_output/file108f93bff877b.Rprof')
htmlwidgets::saveWidget(p, "/path_to_save_output/profile.html")
Then open the html file in your browser.
A few (rough) ideas:
http://vnijs.rady.ucsd.edu:3838/marketing/
From my experiences:
mydebug <- function(msg="[DEBUG]") {
DEBUG <- FALSE
if (DEBUG) {
print(sprintf("%s - %s - %s", msg1, as.character(Sys.time()), as.character(deparse(sys.calls()[[sys.nframe()-1]]))))
}
}
f <- function() {
mydebug()
## your original function definitions .....
mydebug()
return(...) ## the returned value needs to be after mydebug()
}
You can obtain a flame to find out where the time spent (e.g., which JS function? Is it due to layout?).
For details, refer to: https://developers.google.com/web/tools/chrome-devtools/profile/rendering-tools/analyze-runtime?hl=en
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With