rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
884 stars 300 forks source link

includeHTML in shinydashboard with menuSubItem #228

Open MaximilianBernkopf opened 7 years ago

MaximilianBernkopf commented 7 years ago

Hi, guys!

First of all thanks for the cool stuff you can do with shinydashboard!

I got the following issue. I want to insert an Rmarkdown file into my shinydashboard. Therefore I knitted it to an HTML file and tried to use includeHTML. Somehow, when doing so, menuSubItem doesn't work anymore. When clicking on the corresponding menuItem the subitems won't show up. Here's a minimum reproducible example:

library(shiny)
library(shinydashboard)

if (interactive()) {
  header <- dashboardHeader()

  sidebar <- dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Charts", icon = icon("bar-chart-o"),
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2")
      )
    )
  )

  body <- dashboardBody(
    tabItems(
      tabItem("dashboard",
              "Dashboard tab content"
      ),
      tabItem("subitem1",
              "Sub-item 1 tab content"
      ),
      tabItem("subitem2",
              includeHTML('CopyOfApp/test.html')
      )
    )
  )

  shinyApp(
    ui = dashboardPage(header, sidebar, body),
    server = function(input, output) { }
  )
}

Could anyone help? Thanks in advance!

bborgesr commented 7 years ago

Why not use includeMarkdown? Does that result in the same problem?

MaximilianBernkopf commented 7 years ago

Including a basic markdown file using includeMarkdown works perfectly fine!

The problem is that I want to include an Rmarkdown. Sorry, should have used a capital R:) Therefore using includeMarkdown just puts a basic markdown file in there and doesn't execute the code and show the fancy plots. Therefore I first knitted it into an HTML file and tried using includeHTML...

jthomp1818 commented 6 years ago

I am experiencing, what I think, is the same problem and it effects other aspects of shinydashboard too (see my reprex below). With the "initialreport" commented out and before the "refresh" button on tab "B" is clicked, everything works fine. But when the "refresh" button is clicked, generating the html report using includeHTML(), various dashboard features no longer work.

For me, the following happens when I click the report generating "refresh" button: 1st "refresh" click: the sidebar sliderInput stops displaying 2nd click: same as first 3rd click: dropdown menu and menuSubItems no longer display

Also, if you un-comment out the "initalreport" line of code and run, none of the dashboard features work (including the sidebar menu toggle).

Please help! :-)

library(shiny)
library(shinydashboard)

# Build Test HTML file
rmarkdown::render(
  input = "test.rmd",
  output_format = "html_document",
  output_file = file.path(tempdir(), "Test.html")
)

ui <- dashboardPage(
  dashboardHeader(
    dropdownMenu(
      type = "messages",
      icon = icon("envelope"),
      messageItem(
        from = "George",
        icon = icon("user"),
        message = "how's it hanging?"
      )
    )
  ),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebarmenu",
      menuItem(
        "A", tabName = "a",  icon = icon("group", lib="font-awesome"),
        menuSubItem("AA", tabName = "aa"),
        conditionalPanel(
          "input.sidebarmenu === 'aa'",
          sliderInput("b", "Under sidebarMenu", 1, 100, 50)
        ),
        menuSubItem("AB", tabName = "ab")
      ),
      menuItem(
        "B", tabName = "b", icon = icon("rocket")
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "a", textOutput("texta")),
      tabItem(tabName = "aa", textOutput("textaa")),
      tabItem(tabName = "ab", textOutput("textab")),
      tabItem(
        tabName = "b", 
        actionButton(inputId = "btnRunReport", class = "runbtn", label = "REFRESH", icon = icon("refresh")),

        # Show Reports (Initial and User Generated)
        # div(id = "initialreport", includeHTML(path = file.path(tempdir(), "Test.html"))),
        uiOutput(outputId = "uib")
      )
    )
  ),
  useShinyjs()
)

server <- function(input, output) {
  output$texta <- renderText("showing tab A")
  output$textaa <- renderText("showing tab AA")
  output$textab <- renderText("showing tab AB")

  observeEvent(input$btnRunReport, {
    # Hide Initial Report
    shinyjs::html(id = "initialreport", "")

    # Render Updated Report
    rmarkdown::render(
      input = "test.rmd",
      output_format = "html_document",
      output_file = file.path(tempdir(), "Test.html")
    )
    output$uib <- renderUI({includeHTML(path = file.path(tempdir(), "Test.html"))})
  })
}

shinyApp(ui, server)
jthomp1818 commented 6 years ago

Please see this for a solution