了解r闪亮模块:Shiny :: NS(ID)中的错误缺少,没有默认
我试图理解闪亮模块的概念。我正在尝试模块化这个非常简单的ShinyDashboard
应用程序:
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(uiOutput("box"))
)
server <- function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
shinyApp(ui, server)
它可以根据需要起作用。
但是,当模块化时,我会遇到以下错误: Shiny :: NS(ID)中的错误缺少参数“ ID”,没有默认值
。
#### Modules ####
costumizedValueBox <- function(id) {
ns <- shiny::NS(id)
uiOutput(ns("box"))
}
costumizedValueBoxServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
)
}
#### App ####
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox())
)
server <- function(input, output, session) {
costumizedValueBoxServer()
}
shinyApp(ui, server)
我尝试过对模块化代码的不同修改,例如,例如更改输出(toxtOutput
)或常规设置。错误消息暗示了ns
函数的问题,但我无法弄清楚。
谢谢!
I am trying to understand the concept of shiny modules. I am attempting to modularize this very simple shinydashboard
app:
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(uiOutput("box"))
)
server <- function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
shinyApp(ui, server)
which works as desired.
However, when modularizing I keep getting the following mistake:Error in shiny::NS(id) : argument "id" is missing, with no default
.
#### Modules ####
costumizedValueBox <- function(id) {
ns <- shiny::NS(id)
uiOutput(ns("box"))
}
costumizedValueBoxServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$box <- renderValueBox({
valueBox(
subtitle = "Hi",
value = 2
)
})
}
)
}
#### App ####
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(costumizedValueBox())
)
server <- function(input, output, session) {
costumizedValueBoxServer()
}
shinyApp(ui, server)
I have tried different modifications of the modularized code, none of which worked, e. g. changing the output (to textOutput
) or the general setup. The error message suggests a problem with the NS
function but I am not able to figure it out.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是@limey在评论中描述的内容:要详细学习请参阅此处。 rstudio.com/articles/modules.html
This is what @Limey describes in the comments: To learn in detail see here https://shiny.rstudio.com/articles/modules.html
模块(即CostumizedValueboxserver和CodumizedValuebox)是具有所需参数的函数。
Modules (i.e., costumizedValueBoxServer and costumizedValueBox) are functions with a required argument.
是的,只是当您调用模块时,请确保用引号为其分配ID。工作做得很好,我也一直在学习这一点。确保您的ID与应用程序的服务器和UI段中的模块匹配。
这是您可能已经看到的YouTube链接,但是时间戳显示了我在说什么。
https://www.youtube.com/watch?v=OoyahSpxlvs
Yeah just when you call your module, make sure to assign an id for it in quotes. Job well done, I've been learning about this too. Make sure your id matches your module calls in both server and UI sections of your app.
Here is a youtube link, you've probably already seen, but the timestamp shows what I am talking about.
https://www.youtube.com/watch?v=oOYaHsPXLvs