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
453 views
in Technique[技术] by (71.8m points)

css - Add a twitter share button to shiny R navbar

Trying to add a twitter share button to my navbar. The closet I can get is to place it in the header, but this leaves too much white space. I would like it to go in the top right of the navbar, but I cannot figure out which (if any) is the appropriate argument in the navbarPage function to use.

enter image description here

library(shiny)

runApp(launch.browser = TRUE, 
  list(
  ui = shinyUI(navbarPage(
    title=" ", fluid=FALSE,
    header = HTML("<div style='float:right'>
                  <a href='https://twitter.com/share' 
                     class='twitter-share-button' 
                     align='middle' 
                     data-url='www.mywebsite.com' 
                     data-text='Visit www.mywebsite.com' 
                     data-size='large'>Tweet
                  </a>
                  <script>!function(d,s,id){
                     var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
                     if(!d.getElementById(id)){
                       js=d.createElement(s);
                       js.id=id;
                       js.src=p+'://platform.twitter.com/widgets.js';
                       fjs.parentNode.insertBefore(js,fjs);
                     }
                  }(document, 'script', 'twitter-wjs');
                  </script>
                  </div>"),
    tabPanel(
      title = "Data",
      h1("Data"),
      br(),
      tabsetPanel(
        type = "tabs", 
        tabPanel("Selection"),
        tabPanel("View")
      )
    ),
    tabPanel(
      title = "Plots"
    )
  )),
  server = function(input, output) {
  }
))
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could try adding the div directly to the navbar using jQuery by adding this after your last tabPanel:

tags$script(HTML("var header = $('.navbar > .container');
                       header.append('<div style="float:right"><a href="https://twitter.com/share" class="twitter-share-button" aling="middle" data-url="www.mywebsite.com" data-text="Visit www.mywebsite.com" data-size="large">Tweet</a></div>');
                       console.log(header)")),
    tags$script(HTML("!function(d,s,id){
            var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
            if(!d.getElementById(id)){
                    js=d.createElement(s);
                    js.id=id;
                    js.src=p+'://platform.twitter.com/widgets.js';
                    fjs.parentNode.insertBefore(js,fjs);
            }
    }(document, 'script', 'twitter-wjs');"))

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

...