For my package klmr/modules I need to find out if code is being run from inside Shiny(Apps) or directly in R/via a script. Ultimately, this is necessary in order to find the path of the R source file that is calling the code.
At the moment, I am implementing this check by walking up the call stack to find out whether the code is being called by shiny::runApp. I’m wondering whether this is a good (enough) test, whether it’s robust to future changes, and whether there’s a better way. In particular, for Shiny Server the function serverInfo
seems to accomplish what I want but no equivalent seems to exist for running Shiny without Shiny Server.
Code of my current implementation:
shiny_path = function () {
# Look for `runApp` call somewhere in the call stack. frames = sys.frames() calls = lapply(sys.calls(), `[[`, 1) call_name = function (call) if (is.function(call)) '<closure>' else deparse(call) call_names = vapply(calls, call_name, character(1))
target_call = grep('^runApp$', call_names)
if (length(target_call) == 0) return(NULL)
target_frame = frames[[target_call]] namespace_frame = parent.env(target_frame) if(isNamespace(namespace_frame) && environmentName(namespace_frame) == 'shiny') getwd()}
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/8533cad5-e0bb-43df-8580-67e54f08d9f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CAF_jjKp6V7wMqUo%3DYwSio00YJCfSV_xJdmk5U14z0QSdXtStHw%40mail.gmail.com.
shinyApp( ui = fluidPage( ), server = function(input, output, session) { cat(shiny_path()) } )
This comment probably won't be too useful, but I just want to get it out there:From experience, I've noticed it's also possible to run an app by "print"-ing a `shinyApp(...)` function.
[…]
If you run this code, `runApp` is never used, but the shiny app will get launched. Similarly, if you put this code inside a function as the last statement in a function and then call the function, the shiny app will launch without a `runApp`. I've never seen any documentation for this behaviour so my guess is that this is just a convenience behaviour that's not meant to be used in production (Joe can maybe comment on whether this is true or not?). So in this case your method will not work if my understanding is correct, but I don't think you should worry about it
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/4f1c19e7-9c83-4329-ba2b-0ef1b2c7056b%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/78023bd4-222c-4fe1-819e-f994ada0d2d8%40googlegroups.com.