{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Text.Pandoc.Filter.Plot.Renderers.PlotlyR
( plotlyR,
plotlyRSupportedSaveFormats,
)
where
import qualified Data.Text as T
import Text.Pandoc.Filter.Plot.Renderers.Prelude
plotlyR :: PlotM Renderer
plotlyR :: PlotM Renderer
plotlyR = do
Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
plotlyRCmdArgs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
Renderer
{ rendererToolkit :: Toolkit
rendererToolkit = Toolkit
PlotlyR,
rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
plotlyRCapture,
rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
plotlyRCommand Text
cmdargs,
rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -e "if(!require('plotly')) {quit(status=1)}"|],
rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
plotlyRSupportedSaveFormats,
rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
rendererLanguage :: Text
rendererLanguage = Text
"r",
rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"# ",
rendererScriptExtension :: String
rendererScriptExtension = String
".r"
}
plotlyRSupportedSaveFormats :: [SaveFormat]
plotlyRSupportedSaveFormats :: [SaveFormat]
plotlyRSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
JPG, SaveFormat
EPS, SaveFormat
HTML]
plotlyRCommand :: Text -> OutputSpec -> Text
plotlyRCommand :: Text -> OutputSpec -> Text
plotlyRCommand Text
cmdargs OutputSpec {String
FigureSpec
Executable
oCWD :: OutputSpec -> String
oExecutable :: OutputSpec -> Executable
oFigurePath :: OutputSpec -> String
oScriptPath :: OutputSpec -> String
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: String
oExecutable :: Executable
oFigurePath :: String
oScriptPath :: String
oFigureSpec :: FigureSpec
..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}"|]
plotlyRCapture :: FigureSpec -> FilePath -> Script
plotlyRCapture :: FigureSpec -> String -> Text
plotlyRCapture FigureSpec
fs String
fp =
[Text] -> Text
T.unlines
[ Text
"pdf(NULL)",
FigureSpec -> Text
script FigureSpec
fs,
FigureSpec -> String -> Text
plotlyRCaptureFragment FigureSpec
fs String
fp
]
plotlyRCaptureFragment :: FigureSpec -> FilePath -> Script
plotlyRCaptureFragment :: FigureSpec -> String -> Text
plotlyRCaptureFragment spec :: FigureSpec
spec@FigureSpec {Bool
Int
String
[String]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [String]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> String
saveFormat :: FigureSpec -> SaveFormat
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
fsExecutable :: FigureSpec -> Executable
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [String]
dpi :: Int
directory :: String
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
fsExecutable :: Executable
renderer_ :: Renderer
script :: FigureSpec -> Text
..} String
fname = case SaveFormat
saveFormat of
SaveFormat
HTML -> FigureSpec -> String -> Text
plotlyRCaptureHtml FigureSpec
spec String
fname
SaveFormat
_ -> FigureSpec -> String -> Text
plotlyRCaptureStatic FigureSpec
spec String
fname
plotlyRCaptureHtml :: FigureSpec -> FilePath -> Script
plotlyRCaptureHtml :: FigureSpec -> String -> Text
plotlyRCaptureHtml FigureSpec
_ String
fname =
[st|
library(plotly) # just in case
library(htmlwidgets)
p <- last_plot()
htmlwidgets::saveWidget(as_widget(p), "#{toRPath fname}")
|]
plotlyRCaptureStatic :: FigureSpec -> FilePath -> Script
plotlyRCaptureStatic :: FigureSpec -> String -> Text
plotlyRCaptureStatic FigureSpec
_ String
fname =
[st|
library(plotly) # just in case
if (!require("processx")) install.packages("processx")
pdf(NULL)
orca(last_plot(), file = "#{toRPath fname}")
|]