shinyapps.io prevent data loss due to timeout

I am running a Shiny app via shinyapps.io (https://lavaangui.org/). If I leave the browser window open for a while but not in focus, the app "greys out" and shows a "disconnected from server" message. My understanding is that this is the desired behavior, as it prevents consuming many active hours from users having the app open but not using it. However, in my case, this can lead to data loss. I am wondering what the best approach would be to remedy this.

I threw together some nodes in your app and noticed it autogenerates code, e.g.

library(lavaan)
#make sure your data is loaded into the 'data' variable
model <-'
# measurement model
 f1 =~ f2

# regressions
 x2 ~ x1
 f1 ~ x2

# (residual) (co)variances
 f1 ~~ x1
# intercepts
f2 ~ 1

# intercepts
f1 ~ 1


 #  formative factors
 f2 <~ undefined + f1
'

result <- lavaan(model, data, meanstructure = FALSE,
		 int.ov.free = TRUE, int.lv.free = FALSE,
		 estimator = "default", se = "default",
		 missing = "listwise", auto.fix.first = TRUE,
		 auto.fix.single = TRUE, auto.var = TRUE,
		 auto.cov.lv.x = TRUE, auto.cov.y = TRUE,
  		 fixed.x = TRUE)

If it's just the code that matters, and you can reconstruct the diagram from the code (maybe not the same layout, but a neat enough one), then you could consider encoding the model code as a base64 part of the URL slug. This would be like what happens when you create a sharing link from the Shiny for Python playground (Shiny examples).

Maybe implement it as an autosaving feature, after a few seconds of inactivity, update the URL with the encoded model code. When the app times out, the browser will retain the URL. Refresh the page and if your app could read it back in through a GET query of the URL. Users would be able to bookmark models in their browser by the URL, but it would clutter up the forward and back function of the browser.

1 Like

Thank you for the nice suggestion keithn! Unfortunately, it's not just the code. Users can also fit data to the graph they draw. So, I need to also save data, results, etc.

Hello,

I would consider implementing bookmarking

This way users can save the state of the app. You can either just save the input settings via URL, or save the whole app state to the server. In your case I think you should implement the latter.

With this approach there is no need to keep the app alive as a user can always come back to a bookmarked state.

Hope this helps,
PJ

That indeed sounds exactly like what I was looking for. However, there is one serious drawback. It doesn't seem to work with shinyapps.io, or am I mistaken? From the book chapter:

"shinyapps.io doesn’t currently supported server side bookmarking"

A bit of research led me to IndexedDB as a possible solution. This apparantly saves the state locally, like a cookie. Any thoughts on that?