【问题标题】:R shinydashboard collapsible menuItem with inputsR shinydashboard 可折叠 menuItem 与输入
【发布时间】:2022-02-09 01:50:53
【问题描述】:

我正在尝试使用library(shinydashboard) 来实现fileInput,以便为用户提供上传文件的选项(就像here 使用基本闪亮的用户界面所做的那样——请在下面找到示例代码)。

我想将fileInput 中的dashboardSidebar 放在一个可扩展的menuItem 中,但不知道它应该放在shinydashboard 结构中的什么位置。

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)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    编辑:我稍微清理了代码以使 the difference 在 childfull 和 childless menuItem 之间更加清晰 - 参数 expandedNamestartExpanded 只能与与 tabNameselected 相比,childfull menuItem 仅与无子 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")
    

    初步答案:

    当然 - 这是可能的(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)
    

    【讨论】:

    • 我刚刚注意到它不会切换到仪表板选项卡,除非我从菜单项中删除文件输入小部件。有没有办法解决它?再次感谢!
    • @tzema 这是expected behaviour。 Childfull menuItems 未与 tabItem 关联。他们通常只是在点击时展开并显示他们的孩子(如果他们没有孩子,可能会连接到tabItem)。但是,here 可以找到解决方法。
    猜你喜欢
    • 2016-11-17
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多