tzema
tzema

Reputation: 461

R shinydashboard collapsible menuItem with inputs

I am trying to implement a fileInput using library(shinydashboard) to provide the user with the option to upload files (as it was done here with a basic shiny UI - please find the example code below).

I would like to place the fileInput in the dashboardSidebar in an expandable menuItem, but don't know where it should go into the shinydashboard structure.

library(shiny)

ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),
      radioButtons("quote", "Quote",
                   choices = c(None = "",
                               "Double Quote" = '"',
                               "Single Quote" = "'"),
                   selected = '"'),
      tags$hr(),
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    req(input$file1)
    df <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)
    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
  })
}

shinyApp(ui, server)

Upvotes: 2

Views: 1314

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

Edit: I cleaned up the code a little to make the difference between childfull and childless menuItem's more clear - the parameters expandedName and startExpanded can only be used with a childfull menuItem in contrast tabName and selected is only used with childless menuItem's.

library(shiny)
library(shinydashboard)

ui <- function(req) {
  dashboardPage(
    dashboardHeader(title = "Simple tabs"),
    dashboardSidebar(sidebarMenu(
      id = "sidebarItemSelected",
      menuItem(
        "Childfull menuItem",
        menuItem(
          "Childless menuItem 1",
          tabName = "childlessTab1",
          icon = icon("dashboard"),
          selected = TRUE
        ),
        fileInput("upload", "Upload"),
        bookmarkButton(),
        expandedName = "childfullMenuItem",
        startExpanded = TRUE
      ),
      menuItem(
        "Childless menuItem 2",
        icon = icon("th"),
        tabName = "childlessTab2",
        badgeLabel = "new",
        badgeColor = "green"
      )
    )),
    dashboardBody(tabItems(
      tabItem(tabName = "childlessTab1",
              h2("Dashboard tab content")),
      
      tabItem(tabName = "childlessTab2",
              h2("Widgets tab content"))
    ))
  )
}

server <- function(input, output, session) {
  observe({
    cat(
      paste(
        "\nsidebarItemSelected:",
        input$sidebarItemSelected,
        "\nsidebarItemExpanded:",
        input$sidebarItemExpanded,
        "\nsidebarCollapsed:",
        input$sidebarCollapsed,
        "\n"
      )
    )
  })
}

shinyApp(ui, server, enableBookmarking = "url")

Initial answer:

Sure - this is possible (modified version of this example):

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", fileInput("upload", "Upload"), tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", icon = icon("th"), tabName = "widgets",
               badgeLabel = "new", badgeColor = "green")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
              h2("Dashboard tab content")
      ),
      
      tabItem(tabName = "widgets",
              h2("Widgets tab content")
      )
    )
  )
)

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

shinyApp(ui, server)

result

Upvotes: 4

Related Questions