Wednesday, June 13, 2018

sidebarMenu does not function properly when using includeHTML

1 comment

I am using Rshinydashboard and I have ran into an issue when I try and include a html document in my app using includeHTML. Once the menuItems & menSubItems are expanded, they can not be retracted. I have explored other solutions and have found none. If you have any idea what may be the problem or have another way of including a html report in an app I would appreciate your help. Please see the code below and help if you can!

Create a RMD file to create a html report (if you don't have one lying around)

--- title: "test" output: html_document --- ## Test HTML Document This is just a test. 

Build a Test html Report

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

Build Test App

ui <- dashboardPage(   dashboardHeader(),   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")       )     )   ),   dashboardBody(     tabItems(       tabItem(tabName = "a", textOutput("texta")),       tabItem(tabName = "aa", textOutput("textaa"), uiOutput("uia")),       tabItem(tabName = "ab", textOutput("textab"))     )   ) )  server <- function(input, output) {   output$texta <- renderText("showing tab A")   output$textaa <- renderText("showing tab AA")   output$textab <- renderText("showing tab AB")   output$uia <- renderUI(includeHTML(path = file.path(tempdir(), "Test.html"))) }  shinyApp(ui, server) 

2 Answers

Answers 1

That is because you included a complete HTML file in the shiny UI, and you should only include the content between <body> and </body> (quoted from yihui)

A solution could be to run an extra line to fix your Test.html automatically after running rmarkdown::render():

xml2::write_html(rvest::html_node(xml2::read_html("Test.html"), "body"), file = "Test2.html")

and then have

output$uia <- renderUI(includeHTML(path = file.path(tempdir(), "Test2.html")))

Answers 2

You just forget about curly brackets - renderUI need an expression as argument.

renderUI({ includeHTML(...) })

Code

  output$uia <- renderUI({includeHTML(path = file.path(tempdir(), "Test.html"))}) 

works fine.

Or you can use this code

output$uia <- renderUI(includeMarkdown(path = file.path("test.rmd"))) 

In this case you need to specify the path to the file test.rmd, here it is located in the same directory with source file.

If You Enjoyed This, Take 5 Seconds To Share It

1 comment: