我有一个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")
)
)
)
答案 0 :(得分: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经验,但是想看到这个问题回答自己。
答案 1 :(得分: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.
,因此该图可能必须先保存到磁盘。还可以尝试将“数据-uri-路径”(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)