生成下拉通知的函数 Shiny R

Function to generate dropdown notifications Shiny R

在 dashboardHeader 中,如何以编程方式生成下拉菜单项?

dropdownMenu(
  type = "notifications",
  notificationItem(
    text = "message",
    icon = icon("welcome"),
    status = "warning"
  ),
  notificationItem(
    text = "message",
    icon = icon("welcome"),
    status = "warning"
  ),

... #Generate lots more messages

R 中生成消息的一般方法是什么,比如从另一个接受参数(消息数)的函数:

GenerateMessages <- function(number.of.messages) {
#Code to generate messages
}

代码是什么,是写在 UI 中还是写在闪亮的仪表板 header 中的服务器功能?

我将回答我自己的问题,因为它超出了基本动态教程的范围。我真的很喜欢这个解决方案,因为我的通知和逻辑可以超出我的代码,它缩短了我的 app.R 数百行。

一般形式为:

UI 函数内的代码:

# Code to create outputs goes in dashboardHeader 
dropdownMenuOutput("messages.type"),
dropdownMenuOutput("notifications.type"),
dropdownMenuOutput("tasks.type")

服务器函数内的代码:

# Code to generate headers
output$messages.type <- renderMenu(
  dropdownMenu(type = "messages", .list = MessageGenerator())
)
output$notifications.type <- renderMenu(
  dropdownMenu(type = "notifications", .list = NotificationsGenerator())
)
output$tasks.type <- renderMenu(
  dropdownMenu(type = "tasks", .list = TasksGenerator())
)

主要逻辑在MessageGenerator()函数中。这些函数生成在输出中呈现所需的列表。数据结构来自 data.frame,其中包含具有适当 headers 的消息信息。

此解决方案可扩展为使用三个函数 MessageGenerator()TasksGenerator()NotificationsGenerator() 生成消息、任务和通知三种类型的消息。