First, install the webshot
package from the terminal on the Ubuntu server:
sudo su - -c "R -e \"install.packages('webshot')\""
Next, follow the installation description of phantomJS
:
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
With this, you should be able use webshot
and phantomJS
with a Shiny App.
I have tested it with a small sample test app, which I added to /srv/shiny-server/
:
the ui.R
looks like this:
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")
)
)
)
the server.R
looks like this:
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)
}
The final result is this:
