R Shiny 将服务器调用路由到适当的函数

R Shiny routing the server calls to appropriate functions

我是 Shiny 的新手,正在尝试使用 shiny 模块构建一个完整的网络应用程序。我已经查看了以下内容 https://shiny.rstudio.com/articles/modules.html 但仍然不确定我如何将服务器端请求干净地路由到具有配置的给定服务器功能。例如

如果客户端从一个表单发送输入,该表单有 2 个不同的操作,我试图将相同的处理发送到不同的函数。例如从上面的教程 -

server <- function(input, output, session) {
    datafile <- callModule(csvFile, "datafile",
    stringsAsFactors = FALSE)
  output$table <- renderDataTable({
    datafile()
  })
}

但是,我想根据输入调用不同的模块(callModule)不同的函数。

我也查看了 Appsilon 的 http://blog.appsilondatascience.com/rstats/2016/12/08/shiny.router.html,但我不确定这两种方法是否兼容。

可能包中的一些最新开发可能对您有所帮助。 shiny.router 现在支持明确指定根据您的请求 URI 调用的函数。即

router <- make_router(
  route("/",     root_page,  root_callback),
  route("other", other_page, other_callback),
  route("third", other_page, NA)
)

将在您请求 / 时调用函数 root_callback 并在您请求 /other_page 时调用函数 other_callback 并且在您请求 /third

函数 root_callbackother_callback 应该是接受 3 个参数的函数:

root_callback <- function(input, output, session) {
   # magic happens here
}

然后您的表单输入将被打包到第一个参数中,您可以根据它继续操作。

您可能需要咨询the example stated in the repo or this little demo app