首先,在 Ubuntu 服务器上从终端安装 webshot 包:
sudo su - -c "R -e \"install.packages('webshot')\""
接下来,关注phantomJS的installation description:
sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev -y
sudo apt-get install libfreetype6 libfreetype6-dev -y
sudo apt-get install libfontconfig1 libfontconfig1-dev -y
cd ~
export PHANTOM_JS="phantomjs-2.1.1-linux-x86_64"
wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2
sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
phantomjs --version
有了这个,您应该可以将webshot 和phantomJS 与闪亮的应用程序一起使用。
我已经用一个小样本测试应用对其进行了测试,我将其添加到/srv/shiny-server/:
ui.R 看起来像这样:
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Test App to display webshot"),
# Sidebar layout
sidebarLayout(
sidebarPanel(
#actually useless part
),
# Main panel for displaying outputs ----
mainPanel(
# Output: screenshot ----
imageOutput("webshot_image")
)
)
)
server.R 看起来像这样:
library(shiny)
server <- function(input, output) {
output$webshot_image <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
outfile <- tempfile(fileext = '.png')
# Generate the PNG
webshot::webshot(url = "https://github.com/rstudio/shiny",
file = outfile)
# Return a list containing the filename
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
}
最终结果是这样的: