Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

r - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...