Tim_Utrecht Tim_Utrecht - 19 days ago 8
R Question

Sourcing dynamic UI gives 'TRUE' in shiny

I used this example to get a dynamic UI for a shiny app. I'm now trying to source the sidebar panel. But when I do this, I get an ugly 'TRUE' in the sidebar. enter image description here Anyone knows how to get rid of the TRUE without getting the sourceme.r text into the app. Maybe

source
is not the function is should use?

require(shiny)
require(shinydashboard)


mainbody <- div(tabItems(
tabItem(tabName = "t_item1", class = "active", box(title = "Item 1 information")),
tabItem(tabName = "t_item2", box(title = "Item 2 information")),
tabItem(tabName = "t_item3", box(title = "Item 3 information"))
)
)

header <- dashboardHeader(title = "dashboard header")

sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))

body <- dashboardBody(uiOutput("body"))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {

output$sidebarpanel <- renderUI({
# if (USER$Logged == TRUE) {

# div(
source('~/sourceme.r')
# sidebarMenu(
# menuItem("Item 1", tabName = "t_item1", icon = icon("line-chart")),
# menuItem("Item 2", tabName = "t_item2", icon = icon("users")),
# menuItem("item 3", tabName = "t_item3", icon = icon("dollar"))
# )
# )
})
output$body <- renderUI({

mainbody
})
}

shinyApp(ui, server)


sourceme.r:

sidebarMenu(
menuItem("Item 1", tabName = "t_item1", icon = icon("line-chart"), selected = TRUE),
menuItem("Item 2", tabName = "t_item2", icon = icon("users")),
menuItem("item 3", tabName = "t_item3", icon = icon("dollar"))
)

Answer

Make the contents of sourceme.r a function that returns sidebarMenu. Then place the source call outside of the server function, and call the new function inside the server function to return the sidebarMenu.