有没有一种方法可以添加共享按钮,使绘图可以在 Shiny 中共享
Is there a way to add Share buttons to make plots shareable in Shiny
我有一个 Shiny Dashboard 可以输出几个图表。有没有一种方法可以添加 "share social media",用户可以在其中按下以将图表作为 post 上传到他的 facebook 或 twitter?
例如我想要一个按钮,一旦点击它就会在 twitter 上分享 phonePlot 而不是 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")
)
)
)
服务器
# 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")
)
)
)
是的,有一个方法。查看此示例以启用 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
这里:
编辑
我无法解决问题,但我必须在仪表板上显示共享按钮。 OP 想放分享按钮,并想让他的情节可以分享。我没有太多闪亮的经验,但我想看到这个问题自己得到解答。
下面显示了如何 tweet 包含图片 posted。
对于 facebook,我只找到了以下包:
https://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf,它使您能够更新您的状态,但似乎不接受媒体参数。因此,此答案不包括在 Facebook 上的分享。
为了使用 twitter api 以编程方式 post 推文,您需要一个开发者帐户和(最好)一个为您包装 post 请求的 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 中 post 推文。它在 media 参数中从磁盘中获取图片。如果您在闪亮的应用程序中生成 plot/picture,则使用数据 URI 方案。意思是,情节没有(必然?)保存到磁盘,而是嵌入到页面中。由于 rtweet::post_tweet
在媒体参数中采用 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)
我有一个 Shiny Dashboard 可以输出几个图表。有没有一种方法可以添加 "share social media",用户可以在其中按下以将图表作为 post 上传到他的 facebook 或 twitter?
例如我想要一个按钮,一旦点击它就会在 twitter 上分享 phonePlot 而不是 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")
)
)
)
服务器
# 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")
)
)
)
是的,有一个方法。查看此示例以启用 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
这里:
编辑
我无法解决问题,但我必须在仪表板上显示共享按钮。 OP 想放分享按钮,并想让他的情节可以分享。我没有太多闪亮的经验,但我想看到这个问题自己得到解答。
下面显示了如何 tweet 包含图片 posted。 对于 facebook,我只找到了以下包: https://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf,它使您能够更新您的状态,但似乎不接受媒体参数。因此,此答案不包括在 Facebook 上的分享。
为了使用 twitter api 以编程方式 post 推文,您需要一个开发者帐户和(最好)一个为您包装 post 请求的 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 中 post 推文。它在 media 参数中从磁盘中获取图片。如果您在闪亮的应用程序中生成 plot/picture,则使用数据 URI 方案。意思是,情节没有(必然?)保存到磁盘,而是嵌入到页面中。由于 rtweet::post_tweet
在媒体参数中采用 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)