'How can I insert an image into the navbar on a shiny navbarPage()
I am builidng a shiny application using a navbarPage() layout. I would like to insert an image to be on the right hand side of the screen, in the navigation bar. It would look like the navigation bar at the top of, for instance, the stackoverflow sites, but with an logo at the far right. I have tried:
shinyUI(
navbarPage (title="test Page" ,
img(src="mylogo.gif", style="float:right; padding-right:25px"),
tabPanel(title="Panel 1",...)
))
However all this does is display the image in the far right below the navigation bar, instead of the content of the first tab (Note - the image is in the www directory as required).
I can use the icon= argument, but that put the icon on the tab in the browser.
Any ideas on how to put the image on the navigation bar itself?
Solution 1:[1]
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\"><a href=\"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
Solution 2:[2]
This is based on @John Paul's answer but seems simpler to me. First put your page title in a variable since we will use it twice -- as the window title and on our page:
PAGE_TITLE <- "My great title"
Below in your fluid page:
titlePanel(windowTitle = PAGE_TITLE,
title =
div(
img(
src = "my_logo.png",
height = 100,
width = 100,
style = "margin:10px 10px"
),
PAGE_TITLE
)
),
Solution 3:[3]
Here is a solution I made based on the previous great answers:
ui <-
fluidPage(
list(
tags$head(
HTML('<link rel="icon" href="favicon.png"
type="image/png" />'))),
navbarPage("App user name", windowTitle = "App name",
...
The place for saving images is "www" folder on the Shiny app server:
shinyApp/app.R
shinyApp/www/favicon.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zephryl |
| Solution 2 | Community |
| Solution 3 | Andrii |

