简体   繁体   中英

R Shinydashboard dynamic tabItem does not work

There are some examples available for Shinydashboard dynamic menuItems. 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.

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).

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 ...

# 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. I could not find any other UI-side function for creating dynamic UI elements in the documentation.

How can I add UI items dynamically and keep them inside their respective menu items?

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") .

Now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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