简体   繁体   English

R Shinydashboard动态tabItem不起作用

[英]R Shinydashboard dynamic tabItem does not work

There are some examples available for Shinydashboard dynamic menuItems. 有一些适用于Shinydashboard动态menuItem的示例。 The most useful ones are here , here and here . 最有用的是在这里这里这里

However, when I try to render dynamically the content inside of each menuItem, I cannot make it work. 但是,当我尝试动态呈现每个menuItem内部的内容时,我无法使其正常工作。

You will notice from my example that the menuItem "main" is created dynamically on the server side, but the UI items inside it are not (textInput, passwordInput and actionButton). 您将从我的示例中注意到,menuItem“ main”是在服务器端动态创建的,但其中的UI项不是(textInput,passwordInput和actionButton)。

Here is my code. 这是我的代码。

library(shiny)
library(shinydashboard)

# Define UI for app
header1 <- dashboardHeader(
  title = "My Dynamic Menu"
) #dashboardHeader

# DYNAMIC UI
sidebar1 <- dashboardSidebar(
  sidebarMenu(
    menuItemOutput("menuitems")
  ) #sidebarMenu
) #dashboardSidebar
#
body1 <- dashboardBody(
  tabItems(
    menuItemOutput("tabitems")
  ) #tabItems
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

# Define server logic
server <- function(input, output, session) {

  output$menuitems <- renderMenu({
    menuItem("Main", tabName = "main", icon = icon("key"))
  }) #renderMenu

  output$tabitems <- renderUI({
    tabItem(tabName = "main",
            h2("Login"),
            textInput(inputId = "username1", label = "User name:", value = ""),
            passwordInput(inputId = "password1", label = "Password:"),
            actionButton(inputId = "loginbutton1", label = "Login")
    ) #tabItem
  }) #renderUI

} #server

# Run the application 
shinyApp(ui = ui, server = server)

The UI elements should appear as if I replaced the # DYNAMIC UI part with this ... UI元素应该看起来像我将#DYNAMIC UI部分替换为此一样...

# STATIC UI
sidebar1 <- dashboardSidebar(
  sidebarMenu(
    menuItem("Main", tabName = "main", icon = icon("key"))
  ) #sidebarMenu
) #dashboardSidebar
#
body1 <- dashboardBody(
  tabItems(
    tabItem(tabName = "main",
            h2("Login"),
            textInput(inputId = "username1", label = "User name:", value = ""),
            passwordInput(inputId = "password1", label = "Password:"),
            actionButton(inputId = "loginbutton1", label = "Login")
    ) #tabItem
  ) #tabItems
) #dashboardBody

I know that it is not rendering the individual UI elements inside because I am using menuItemOutput for ("tabitems") inside dashboardBody. 我知道它不会在其中呈现单个UI元素,因为我在dashboardBody ("tabitems") menuItemOutput用于("tabitems") I could not find any other UI-side function for creating dynamic UI elements in the documentation. 在文档中找不到其他用于创建动态UI元素的UI端功能。

How can I add UI items dynamically and keep them inside their respective menu items? 如何动态添加UI项并将其保留在各自的菜单项中?

I will appreciate it a lot if you can help me out with this! 如果您能帮我解决这个问题,我将不胜感激! Any ideas? 有任何想法吗?

Instead of menuItemOutput("tabitems") , it should say uiOutput("tabitems") . 代替menuItemOutput("tabitems") ,它应该说uiOutput("tabitems")

Now it works. 现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM