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 |1 Answer
Reset to default 1I 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
版权声明:本文标题:Is it possible to create and launch a Shiny App within an R function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306810a1933094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
shiny::runExample()
itself does, after all. You don’t need a directory at all.shiny::runApp()
can directly run ashinyApp
object. Refer to the documentation (especially the last example). – Konrad Rudolph Commented Nov 21, 2024 at 22:38shiny()
(or a simplified version of it) so we can tell you what is going wrong. – user2554330 Commented Nov 30, 2024 at 19:51