How can I insert an image into the navbar on a shiny navbarPage()
Solution 1:
I can now answer this question, at least for shiny 0.10.0. The general idea is to set the title=
to a div()
that contains both the image and the text for the the title.
This however, creates a new problem in that the icon=
argument no longer works, and you cannot set a title for the window. To get around this I followed Andy Singleton's advice here.The advice is to create a fluidPage()
above the navbarPage()
that can be used to hold the window title and icon. By making this page 0 pixels in height, it is hidden on the app. Here is the key bits of code.
ui.r:
shnyUI(
fluidPage(
list(tags$head(HTML('<link rel="icon", href="MyIcon.png",
type="image/png" />'))),
div(style="padding: 1px 0px; width: '100%'",
titlePanel(
title="", windowTitle="My Window Title"
)
),
navbarPage(
title=div(img(src="myLogo.gif"), "My Title in the Navbar"),
tabPanel(....
Solution 2:
For those of you who have more than one image in the navbar, the title=
will only work for one of the images, unless you like horrendous formatting.
This code below allows the user to append the header with a new image that is also hyperlinked. I used this to create a GitHub logo and link my repository.
# Create Right Side Logo/Image with Link
tags$script(HTML("var header = $('.navbar > .container-fluid');
header.append('<div style=\"float:right\"><ahref=\"URL\"><img src=\"Logo.png\" alt=\"alt\" style=\"float:right;width:33px;height:41px;padding-top:10px;\"> </a>`</div>');
console.log(header)")
),
header.append
through </a></div>');
NEEDS TO BE IN ONE ROW OF CODE IN R
All we are doing is tagging this section of code as HTML script, therefore everything is passed as a character string to R, and read as HTML code. Luckily we can change the size of the picture, and move it around using padding-left,right,top,bottom: _px
.
Note that container-fluid
is my nav-bar, but it may be different for your app. Additional formatting options can be included in the style portion of the code.
Note that you may also add text, and link it in this manner as well.
# Create Right Side Text
tags$script(HTML("var header = $('.navbar > .container-fluid');
header.append('<div style=\"float:right\"><h3>Follow us on GitHub</h3></div>');
)),
Again make sure header.append
through </div>');
are in the same line of code in R