I am just wondering if it is possible to create a Shiny App within an R function (i.e., the environment or working directory is temporary while the function is running, and will be abandoned after the Shiny App is closed).
I have tested the idea to run an example Shiny wrapped in a function, and it works:
run_shiny_from_function <- function() {
# 1. Variable defined in the function:
x <- data.frame(name = "Eric", "Bee", "Tui")
# 2. Run an example Shiny app:
shiny::runExample("01_hello")
}
And if you run this function, an example Shiny App will be launched:
run_shiny_from_function()
My question is, is it possible to replace shiny::runExample("01_hello")
with a custom Shiny App that uses the data frame x
defined in the parent function run_shiny_from_function()
?
If so, how could I build the directory so that the nested Shiny App can find the child-functions defined, and data to be used?
The idea is:
run_shiny_from_function <- function () {
# 1. Variable defined in the function:
x <- data.frame(name = "Eric", "Bee", "Tui")
# 2. Pass the object as input data in a nested Shiny app:
nested_shiny(input = x) {
# Define UI for application that draws a histogram
ui <- fluidPage(
tableOutput(outputId = "data_table")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$data_table <- renderTable({
x # <- Here is where the x will go
})
}
shinyApp(ui = ui, server = server)
}
}
Any comments are greatly appreciated.
I don’t really understand the purpose of the nested_shiny
function. At any rate, removing it makes the code work:
run_shiny_from_function <- function () {
x <- data.frame(name = "Eric", "Bee", "Tui")
ui <- fluidPage(
tableOutput(outputId = "data_table")
)
server <- function(input, output) {
output$data_table <- renderTable({
x # <- Here is where the x will go
})
}
shinyApp(ui = ui, server = server)
}
The code would in principle also work if you keep that function (after fixing the syntax error in the function definition), but you would need to call it, which your code doesn’t.