Reputation: 39
when I select a CSV file in menuItem ("File Loading"), the file is selected but not not displayed in dashboardBody's tabItem (tab1). Its trying to display only the 2nd tabItem "tab2". I believe I am missing something here to connect. Could someone help me out.
ui <- dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(
sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
menuItem("File Loading", tabName = 'tab1', icon = icon('line-chart'),
fileInput("file1", "Select CSV File", accept = c("text/csv","text/comma-
separated- values,text/plain",".csv"))),
menuItem('Data', tabName = 'tab2',icon = icon('line-chart')),
menuItem('c',tabName = 'tab3',icon = icon('line-chart')))),
dashboardBody(
tabItems(
tabItem("tab1", DT::dataTableOutput("contents"),style = "height:500px; overflow-y:
scroll;overflow-x: scroll;",
title = "Dashboard example"),
tabItem("tab2", "Sub-item 2 tab content")
)
)
)
server <- function(input, output, session) {
observe(input$sbar)
output$text1 <- renderText(print(input$sbar))
output$contents <- renderDataTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath) })
}
shinyApp(ui, server)
Upvotes: 0
Views: 340
Reputation: 21297
I have commented out multiple tabs within each menuitem. You can add more, if necessary. Try this
ui <- dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(
shinyjs::useShinyjs(),
sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
menuItem("File Loading", tabName = 'page1', icon = icon('line-chart'),
fileInput("file1", "Select CSV File",
accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
menuSubItem(actionButton(inputId="next1", label="NEXT"),
tabName="next", icon=""),
hidden(menuSubItem("My tab1", tabName="tab1",icon = icon("line-chart")))
),
menuItem('Data', tabName = 'tab2',icon = icon('line-chart')),
menuItem('c',tabName = 'tab3',icon = icon('line-chart'))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "tab1",
fluidRow(
tabBox(id = "tabset1", height = "650px", width=12, title = "Dashboard example",
# The id lets us use input$tabset1 on the server to find the current tab
tabPanel(" ", value="tab11", " ",
fluidRow(DT::dataTableOutput("contents1"))
)#,
# tabPanel("Plot", value="tab12", " ",
# fluidRow(plotOutput("plot1"))
# ),
# tabPanel("tab3 title", value='tab13', " ",
# valueBoxOutput('tab3_valuebox'))
)
)
),
tabItem(tabName="tab2",
fluidRow(
tabBox(id = "tabset2", height = "650px", width=12, # title = "Sub-item 2 tab content",
# The id lets us use input$tabset1 on the server to find the current tab
tabPanel("Data", value="tab21", " ",
fluidRow(DT::dataTableOutput("contents2"))
)#,
# tabPanel("Plot", value="tab22", " ",
# fluidRow(plotOutput("plot2"))
# )
)
)
)
)
)
)
server <- function(input, output, session) {
#observe(input$sbar)
observeEvent(input$next1, {
updateTabItems(session, "sbar", "tab1")
req(input$next1)
if (input$next1 == 0) {
return(NULL)
}else if (input$next1 == 1 & is.null(input$file1)) {
return(NULL)
}else {
inFile <- input$file1
myfile <- read_csv(inFile$datapath)
output$contents1 <- renderDataTable({
myfile
})
output$contents2 <- renderDataTable({
myfile
})
}
})
output$text1 <- renderText(print(input$sbar))
output$plot1 <- renderPlot({hist(rnorm(10))})
output$plot2 <- renderPlot({hist(rnorm(20))})
output$tab3_valuebox <- renderValueBox({
valueBox('2020',subtitle = "blah blah blah",icon = icon("car"),
color = "red"
)
})
}
shinyApp(ui, server)
Upvotes: 1