r - How can I call my shinyapp through functions?
问题描述
I deployed a shiny app as a library. I want to know how to open a file in my shiny app but through an R function.
I mean; I want to have two different functions:
open(fileName) This function will open the shiny app with the file that the user will send as a parameter.
create() This function will open the shiny app with a new file
I am confused about how can I organize my application. For example, if the user types open(Test1), then deploy my app with the file Test1 loaded or what happened if the user types create().
Do I need to create different scripts of app.R and then inside each function call the app.R that will help me with the behavior expected?
解决方案
一个可重复的示例将有助于理解您的确切需求(例如,“打开文件”究竟是什么意思),但我认为这里有一些内容可以帮助您入门。
这是基于此处提供的第一个 Shiny 示例。基本上,整个闪亮的应用程序是在一个名为 的函数内定义的gadget_runner()
,然后应用程序在函数的末尾运行shiny::runGadget(ui, server)
。您可以根据传递的参数定义函数以不同方式操作。然后您可以定义您的函数open(fileName)
并create()
根据需要调用该函数。
gadget_runner <- function(fileName) {
if (missing(fileName)) {
x <- tempfile()
}
ui <- shiny::fluidPage(
shiny::titlePanel(x),
shiny::sidebarLayout(
shiny::sidebarPanel(
shiny::sliderInput(
inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30
)
),
shiny::mainPanel(
shiny::plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- shiny::renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x,
breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
})
}
shiny::runGadget(ui, server)
}
open <- function(fileName) {
gadget_runner(fileName)
}
create <- function() {
gadget_runner()
}
推荐阅读
- javascript - 正则表达式使用 match() 在 javascript 中提取字符串数组
- javascript - 反应本机函数返回未定义
- angular - 如何将 Angular ngFor 与 Observable 一起使用
- spring - Spring 引起:org.springframework.core.serializer.support.SerializationFailedException:无法反序列化有效负载?集群模式
- javascript - 如何使它在我选择错误答案时弹出“重试”按钮
- git - 无法将我的 git 存储库连接到 Jenkins
- architecture - 微服务架构 - 每个服务是否需要 API
- javascript - 如何在 React 项目中正确使用/导入 JavaScript 文件?
- java - CompletableFuture:在抛出异常之前获取对象
- angular - VS Code - Angular:对装饰器的实验性支持警告