{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
module Text.Pandoc.Filter.Plot.Renderers.PlotlyR (
plotlyRSupportedSaveFormats
, plotlyRCommand
, plotlyRCapture
, plotlyRAvailable
) where
import qualified Data.Text as T
import Text.Pandoc.Filter.Plot.Renderers.Prelude
plotlyRSupportedSaveFormats :: [SaveFormat]
plotlyRSupportedSaveFormats = [PNG, PDF, SVG, JPG, EPS, HTML]
plotlyRCommand :: OutputSpec -> Text -> Text
plotlyRCommand OutputSpec{..} exe = [st|#{exe} "#{oScriptPath}"|]
plotlyRAvailable :: PlotM Bool
plotlyRAvailable = do
mexe <- executable PlotlyR
case mexe of
Nothing -> return False
Just (Executable dir exe) ->
commandSuccess dir [st|#{exe} -e 'if(!require("plotly")) {quit(status=1)}'|]
plotlyRCapture :: FigureSpec -> FilePath -> Script
plotlyRCapture fs fp =
T.unlines [ "pdf(NULL)"
, script fs
, plotlyRCaptureFragment fs fp
]
plotlyRCaptureFragment :: FigureSpec -> FilePath -> Script
plotlyRCaptureFragment spec@FigureSpec{..} fname = case saveFormat of
HTML -> plotlyRCaptureHtml spec fname
_ -> plotlyRCaptureStatic spec fname
plotlyRCaptureHtml :: FigureSpec -> FilePath -> Script
plotlyRCaptureHtml _ fname = [st|
library(plotly) # just in case
library(htmlwidgets)
p <- last_plot()
htmlwidgets::saveWidget(as_widget(p), "#{toRPath fname}")
|]
plotlyRCaptureStatic :: FigureSpec -> FilePath -> Script
plotlyRCaptureStatic _ fname = [st|
library(plotly) # just in case
if (!require("processx")) install.packages("processx")
pdf(NULL)
orca(last_plot(), file = "#{toRPath fname}")
|]