Workflow for maintaining different versions of a Shiny app - r

Lately I have been making several very similar Shiny apps for different clients and hosting them on shinyapps.io.
Each app has a different title, different data, some differences in branding etc. but otherwise the code is very similar.
I'm having trouble maintaining these apps. When find and fix a bug I currently have to go through 5 different apps and make the change each time.
Does anyone have good suggestions on how to handle this? Git branches? I know the best solution would be to have one app and upload different data, but that's not possible unfortunately.
I'd like to keep using shinyapps.io, but I'm open to hosting the apps somewhere else if it makes my workflow better.

As I wrote in the comment shinyModules() will help you: https://shiny.rstudio.com/articles/modules.html
Shiny modules are to shiny functions, like ordinary functions are to repeating code.
Or to put it differently:
Repeating code --> function
repeating shiny function --> shiny module
As the documentation is a bit complicated here and there, i wrote a simplified example here:
Create a reactive function outside the shiny app.
You could store all the shiny modules in a file modules.R and add a global.R script to each of the apps that loads the modules (source("../modules.R"). Then you only have to update the functions within modules.R. That change of structure might take a while in the beginning. But, i think in the long run it pays off for more complex apps.

I ended up making a library that contained most of the code I needed for the apps, as suggested by Adam Spannbauer in the comments.
It's not perfect; I still have some duplication and I have to have the library on GitHub so that it will work with shinyapps.io. However, it's a big improvement on what I was doing previously.

Related

Interactive Document (takes input gives output) in Rpubs

Is it possible to publish an RMarkdown file to Rpubs that takes input and gives out put from the Rpubs site? I want to create a simple calculator related to my job that takes a few inputs and gives an output and, if possible, publish to Rpubs for people to view and use.
SHINY: I know this is what shiny is for, creating interactive apps, but I dont know it very well or how to implement an app I create for multiple people to use, or how to imbed the link to the app, and so on. Just trying to see if this is possible in things I already know how to use
RPubs is for HTML documents that don't require R calculations on the backend. In some instances, there can be some degree of interactivity e.g., brushing and linking plots, filtering data and having the filter propagate to a plot. These happen through the crosstalk package. However, it sounds like you need a shiny app that can take inputs and have R do some calculation of those on the back end. You could host your app on shinyapps.io
It's also possible, depending on how complicated the calculations are, that the entire app could be written in native javascript, which wouldn't require a server-side computation, so could be hosted on any website.

Improve profvis performance for profiling shiny app

I have a very large and complex R Shiny app and I am trying to find bottlenecks or parts of the code to improve via the profvis package.
The problem is that profvis itself is performing very slowly (because of the processes and functions in my shiny app) so that it's lagging and almost not possible to properly view & navigate through the profile or the flame graph.
Starting the profvis profile via R or via browser (firefox & chrome tested) doesn't really make a big difference here.
I am only testing one (the main) feature/calculation in my shiny app which is initiated by one action button, thus I can't really test less features or make the profile "shorter".
Any help or tips are appreciated. Especially ways to run profvis faster. Another option I tried was to only wrap parts of my code inside the shiny app with profvis, but I didn't find a way to get this work.
Thank you!

Shiny apps in R: how to structure them correctly

I created my first Shiny app, which runs perfectly in my laptop.
However, I need to submit it to my professor and I want to make sure he will be able to run it.
I have a UI file, a server file, a global file and a process file.
The process file stores the data preparation.
The global file reads two RDS files which are the datasets that I use in the server.
Where should my libraries be loaded? For example, the app does not run without leaflet, how can I ensure the libraries are run automatically?
My RDS files are saved to my local drive, which means that my professor will need to change the path in order to use them, how can avoid this?
Shall I put the UI, server and global into one R script or is it ok to have them onto two different scripts?
Thank you!
As you describe your current set up, the most obvious place to load your libraries is at the start of your global file. (Or at the start of app.R if you move to a single file configuration.) Though not exactly what the reprex package is designed for, you could probably use reprex to make sure that your code is reproducible and independent of anything you may have overlooked. (You've already identified the obvious issue with data files.) Look here for more information on reprex.
Indeed. This is a problem. If you must load the data from files, you need to find a way of providing them to your professor. Telling him to edit your code by hand is not a good way to start. Exactly how to do this depends on the infrastucture your institution has set up, so it's difficult to advise you what to do. Do you not have a shared area for your course? Ask your fellow students - or even your professor.
Whether you bundle the app in a single file or in seperate files is really a matter of choice. I don't think there's a wroing way and a right way. For me, the deciding factor is usually the size of the app. Large apps get several files. For small apps, separation is just an unnecessary complication. Another factor - that doesn't seem to be an issue here - is whether I see the app as a potential front end for some methodology that might be worthy of its own package. In that case, I develop the app as a front end, and the package itself as separate entities.
Question 1 and 3 depend on criteria for code quality like performance and maintainability. When the code grows, there will come the point when it is more difficult to handle all code in one file. Once it grows even further you will encounter the point when you need split the app into modules to keep your code easy to maintain.
Regarding libraries I advise declaring them at the entrance point of an app (though, basically, that is a matter of taste and style). That way, you provide maximum clarity regarding the dependencies of the app. Again, if the app becomes very large and not all parts of the app rely on the same packages, it could improve performance and maintainability that each part loads the packages as required. It could give you a performance advantage when you do not need to load all packages at once. However, that is probably only true for very large apps.
However, since this all seems to be an exercise at university, I doubt that your app will reach higher levels of complexity.
Question 2: In a shiny app you can provide fileInput widget. This SO question shows you how.

R/Shiny: View full stack/execution log

I'm debugging a Shiny web app, and would like to see the entire control flow/execution path over the course of rendering and updating the generated website.
Is there a way to capture/print/dump-to-file every line of code that is executed in the process of rendering/updating a Shiny app? It would also be good (maybe better?) to see every line of R code parsed by the running R interpreter instance; I'm not concerned about length of this output, and would prefer to get things as verbosely as possible.
I have looked into the stack tracing Shiny functions but these seem to be intended for error catching/handling/reporting. The app is not generating errors/warnings, just setting some variables to NULL at some point when they shouldn't be, so I'm not sure if this is the right approach. These stack tracing functions also seem to be more localized, designed to operate within a given reactive variable/function/render rather than following the control/execution flow across differing reactives/rendering functions in an app.
This app is a large, company-internal app so I cannot give a MRE/MWE.
I finally found the profvis package that does more-or-less exactly what I want by taking a snapshot of the execution stack at a fixed time interval (default 10ms). I'm still working on using this tool to debug, but I believe this will get me there and would also be useful to others debugging Shiny apps that need more than browser() and/or reactlog.
Specifically, I have been doing:
#install.packages("profvis")
library(profvis)
exec_log <- profvis(runApp("myShinyApp"))
...interact with the myShinyApp web page enough to trigger the bug, then interrupt execution...
print(exec_log)

Start project as Shiny app or Shiny markdown document?

I'm an experienced programmer but new to R, working on a visualization project that would be months of work in D3.js (but client knows R, not JavaScript.) I may have to start from a clean slate again once I figure out what I'm doing, but at the moment, I'm just trying to decide what kind of object to start with. If I make a new R Markdown document (in RStudio), Shiny is one of my output options; or I can make a new Shiny app without markdown.
I can guess what some of the trade offs might be:
R Markdown might be a good playground for trying stuff, documenting it
as I go along, and sharing it with clients.
But the quoting and embedding options add an extra layer of stuff I'm
not proficient with.
Working on a plain Shiny app might allow me to focus more on learning
Shiny, but it might tempt me to over-design the app before I know what
I'm doing, and I should probably be focusing more on learning R and
ggplot2 and experimenting with the data and possible Shiny widgets (which
R Markdown should be good for.)
The question of what I should do is opinion-based, and I understand that's not what StackOverflow is for. But just writing out the question has helped me understand the differences between these options. So my real question is: have I understood the relationship between Shiny and R Markdown correctly or am I missing something important?
Edit. Based on clarifying questions in comments, my needs are: a good playground for learning R, exploring the data and ways of letting users interact with it, documenting these experiments, and serving as a scratchpad for components that will form a more coherent application eventually. Based on this question, R Notebook might be more appropriate for a first pass than regular R Markdown, but it doesn't have a prepackaged Shiny option, so R Markdown with Shiny is probably best for now.

Resources