2

I have a Shiny Dashboard that outputs a couple of graphs. Is there a way to add "share social media" where a user would press to upload the graphs as a post to his facebook or twitter?

for example I want to have a button once clicked it will share phonePlot on twitter not the link...

 # Rely on the 'WorldPhones' dataset in the datasets
 # package (which generally comes preloaded).
 library(datasets)

# Use a fluid Bootstrap layout
fluidPage(    

  # Give the page a title
     titlePanel("Telephones by region"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("region", "Region:", 
                  choices=colnames(WorldPhones)),
      hr(),
      helpText("Data from AT&T (1961) The World's Telephones.")
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("phonePlot")  
    )

  )
)

Server

# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)

# Use a fluid Bootstrap layout
fluidPage(    

  # Give the page a title
  titlePanel("Telephones by region"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("region", "Region:", 
                  choices=colnames(WorldPhones)),
      hr(),
      helpText("Data from AT&T (1961) The World's Telephones.")
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("phonePlot")  
    )

  )
)
CaseebRamos
  • 684
  • 3
  • 18

2 Answers2

3

Yes, there is a method. Take a look at this example to enable a share button for Twitter:

url <- "https://twitter.com/intent/tweet?text=Hello%20world&url=https://shiny.rstudio.com/gallery/widget-gallery.html/"

ui <- fluidPage(

  # Application title
  titlePanel("Twitter share"),

  # Sidebar with an actionButton with the onclick parameter
  sidebarLayout(
    sidebarPanel(

      actionButton("twitter_share",
                   label = "Share",
                   icon = icon("twitter"),
                   onclick = sprintf("window.open('%s')", url)) # Combine text with url variable
      # Use the onclick parameter to open a new window to the twitter url
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
                 # generate an rnorm distribution and plot it
                 dist <- rnorm(1:1000)
                 hist(dist)
               })
}

Additional resources here: https://shiny.rstudio.com/reference/shiny/1.4.0/bookmarkButton.html

and here: Add a twitter share button to shiny R navbar

Edit

I couldn't solve the problem however I got to display share button on the dashboard. OP wants to put share button, and want to make his plot sharable. I don't have a lot of Shiny experience, but wanted to see this question answered myself.

halfer
  • 19,824
  • 17
  • 99
  • 186
CaseebRamos
  • 684
  • 3
  • 18
2

In the following it is shown how a tweet including a picture can be posted. For facebook i only found the following package: https://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf, which enables you to update your status, but does not seem to take media arguments. Therefore, sharing on facebook is not included in this answer.

In order to use the twitter api to programmatically post tweets you will need a developer account and (optimally) an R package that wraps the post request for you.

According to this page: https://rtweet.info/index.html you could use library(rtweet) (self-reference), or library(twitteR) for that. In the following library(rtweet) is used.

To be able to use your twitter developer account from within R, follow this detailed instruction: https://rtweet.info/articles/auth.html#rtweet. An initial setup is enough and can be confirmed by using rtweet::get_token(), see the code below.

Small walk through:

rtweet::post_tweet allows you to post tweets from within R. It takes pictures from disk in the media argument. If you generate a plot/picture in your shiny app, the data-URI scheme is used. Meaning, the plot is not (necessarily?) saved to disk, but embedded in the page. As rtweet::post_tweet takes File path to image or video media to be included in tweet. in the media argument, the plot has probably to be saved to disk first. One could also attempt to handover the "data-uri-path" (data:image/png;base64,i....) to rtweet::post_tweet but i would guess rtweet::post_tweet will not accept base64 encoded plot data (not tested).

A reproducible example (assuming you have fulfilled this steps: https://rtweet.info/articles/auth.html#rtweet):

library(shiny)
library(rtweet)
rtweet::get_token() # check if you have a correct app name and api_key

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(
      actionButton(
        inputId = "tweet",
        label = "Share",
        icon = icon("twitter")
      ),
      sliderInput(inputId = "nr", label = "nr", min = 1, max = 5, value = 2)
    ),

    mainPanel(
      plotOutput("twitter_plot")
    )

  )

)

plot_name <- "twitter_plot.png"
server <- function(input, output, session) {

  gen_plot <- reactive({
    plot(input$nr)
  })

  output$twitter_plot <- renderPlot({
    gen_plot()
  })

  observeEvent(input$tweet, {
    # post_tweet takes pictures from disk in the media argument. Therefore,
    # we can save the plot to disk and then hand it over to this function.
    png(filename = plot_name)
    gen_plot()
    dev.off()
    rtweet::post_tweet("Posted from R Shiny", media = plot_name)
  })

}

shinyApp(ui, server)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • 1
    Thanks Tonio, but would this make every tweet coming from the same user? – MalekTheNoob Jun 03 '20 at 15:09
  • 1
    Thank you Tonio! Good luck Malek. Please select this as answer if you're ok with it. I will grant the bounty – CaseebRamos Jun 03 '20 at 15:24
  • 1
    Good point @MalekTheNoob. Following the docu of the authentication you would have two options. Either the posts come from the same user or you give the current user the options to authenticate with his/her developer account, providing `api_key, api_secret_key, access_token, access_token_secret` in text inputs. But developer accounts would be required,... – Tonio Liebrand Jun 03 '20 at 18:12
  • One last try, you can open the plot by iteself in chrome , so it has a link that points to it directly....would it be possible to copy it to clipboard? – MalekTheNoob Jun 04 '20 at 21:02
  • 1
    You mean programatically i assume. Well I guess, yes. But i would think that would be more of a new question. One can follow: https://stackoverflow.com/questions/33175909/copy-image-to-clipboard and integrate the javascript code with shinyjs and bind it to a button. – Tonio Liebrand Jun 04 '20 at 21:28
  • Thank you Tonio. Granted you the bounty sir :) – CaseebRamos Jun 07 '20 at 13:59