admin管理员组

文章数量:1122826

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 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.

Share Improve this question edited Nov 30, 2024 at 20:07 Grasshopper_NZ asked Nov 21, 2024 at 22:29 Grasshopper_NZGrasshopper_NZ 7555 silver badges14 bronze badges 3
  • 3 Yes, obviously this works: that’s what shiny::runExample() itself does, after all. You don’t need a directory at all. shiny::runApp() can directly run a shinyApp object. Refer to the documentation (especially the last example). – Konrad Rudolph Commented Nov 21, 2024 at 22:38
  • Thank you, I have read it and tried it for the past few days but it may not relate to what I asked for. I am still having trouble to pass the object defined in the parent function to the nested Shiny app in the same parent function. – Grasshopper_NZ Commented Nov 30, 2024 at 6:44
  • You should include the definition of your function shiny() (or a simplified version of it) so we can tell you what is going wrong. – user2554330 Commented Nov 30, 2024 at 19:51
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: Is it possible to create and launch a Shiny App within an R functionStack Overflow