了解r闪亮模块:Shiny :: NS(ID)中的错误缺少,没有默认

发布于 2025-01-31 05:08:27 字数 1181 浏览 3 评论 0原文

我试图理解闪亮模块的概念。我正在尝试模块化这个非常简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苹果你个爱泡泡 2025-02-07 05:08:27

这就是@limey在评论中描述的内容:要详细学习请参阅此处。 rstudio.com/articles/modules.html

library(shiny)

#### Modules ####
costumizedValueBox <- function(id, label = "ValueBox_custom") {
  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("ValueBox_custom1"))
)

server <- function(input, output, session) {
  costumizedValueBoxServer("ValueBox_custom1")
}

shinyApp(ui, server)

This is what @Limey describes in the comments: To learn in detail see here https://shiny.rstudio.com/articles/modules.html

library(shiny)

#### Modules ####
costumizedValueBox <- function(id, label = "ValueBox_custom") {
  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("ValueBox_custom1"))
)

server <- function(input, output, session) {
  costumizedValueBoxServer("ValueBox_custom1")
}

shinyApp(ui, server)

阳光①夏 2025-02-07 05:08:27

模块(即CostumizedValueboxserver和CodumizedValuebox)是具有所需参数的函数。

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(costumizedValueBox("my_id")) # pass in an ID
)

server <- function(input, output, session) {
  costumizedValueBoxServer("my_id") # pass in the same ID
}

Modules (i.e., costumizedValueBoxServer and costumizedValueBox) are functions with a required argument.

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(costumizedValueBox("my_id")) # pass in an ID
)

server <- function(input, output, session) {
  costumizedValueBoxServer("my_id") # pass in the same ID
}
望笑 2025-02-07 05:08:27

是的,只是当您调用模块时,请确保用引号为其分配ID。工作做得很好,我也一直在学习这一点。确保您的ID与应用程序的服务器和UI段中的模块匹配。
这是您可能已经看到的YouTube链接,但是时间戳显示了我在说什么。

https://www.youtube.com/watch?v=OoyahSpxlvs

  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(costumizedValueBox("VBox_id_1")) # pass in an ID
)

server <- function(input, output, session) {
  costumizedValueBoxServer("VBox_id_1") # pass in the same ID
}```

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

  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(costumizedValueBox("VBox_id_1")) # pass in an ID
)

server <- function(input, output, session) {
  costumizedValueBoxServer("VBox_id_1") # pass in the same ID
}```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文