是否可以添加共享按钮以使绘图在Shiny中可共享

问题描述 投票:2回答:2

我有一个Shiny Dashboard,可以输出几个图形。有没有一种方法可以添加“共享社交媒体”,用户可以按此按钮将图形作为帖子上传到他的Facebook或Twitter?]

例如,我想单击一个按钮,它将在Twitter上共享phonePlot,而不是链接...

 # 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")  
    )

  )
)

服务器

# 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")  
    )

  )
)
html r shiny shinydashboard shiny-server
2个回答
2
投票

下面显示了如何张贴包含图片的tweet。对于facebook,我仅找到以下软件包:https://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf,它使您可以更新状态,但似乎不接受媒体参数。因此,此答案中不包括在Facebook上共享。

为了使用twitter api以编程方式发布推文,您将需要一个开发人员帐户和一个(最佳)R包来包装您的发布请求。

根据此页面:https://rtweet.info/index.html,您可以使用library(rtweet)(自参考)或library(twitteR)。在下面使用library(rtweet)

要在R中使用您的Twitter开发者帐户,请遵循以下详细说明:https://rtweet.info/articles/auth.html#rtweet。初始设置就足够了,可以使用rtweet::get_token()进行确认,请参见下面的代码。

小阅览:

rtweet::post_tweet允许您从R内部发布推文。它在media参数中从磁盘拍摄图片。如果在闪亮的应用程序中生成绘图/图片,则使用数据URI方案。意思是,该图没有(必要吗?)保存到磁盘上,而是嵌入到页面中。由于rtweet::post_tweet在media参数中采用了File path to image or video media to be included in tweet.,因此该图可能必须先保存到磁盘。也可以尝试将“ data-uri-path”(data:image/png;base64,i....)切换到rtweet::post_tweet,但我想rtweet::post_tweet将不接受base64编码的绘图数据(未经测试)。

一个可复制的示例(假设您已完成以下步骤: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)

3
投票

是的,有一种方法。看一下这个示例,为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)
               })
}

此处的其他资源:https://shiny.rstudio.com/reference/shiny/1.4.0/bookmarkButton.html

和此处:Add a twitter share button to shiny R navbar

编辑

我无法解决问题,但是我必须在仪表板上显示共享按钮。 OP希望设置“分享”按钮,并希望使其情节可共享。我没有很多Shiny经验,但是想看看这个问题自己回答。

© www.soinside.com 2019 - 2024. All rights reserved.