[rkward] rkward/plugins: added plugin to export/convert RMarkdown files
m.eik michalke
null at kde.org
Tue Oct 30 12:44:37 GMT 2018
Git commit c1f150cd42402d4bad4a62f36b1ff1e58d2eaa50 by m.eik michalke.
Committed on 30/10/2018 at 12:42.
Pushed by meikm into branch 'master'.
added plugin to export/convert RMarkdown files
A +122 -0 rkward/plugins/00saveload/save/ExportRMarkdown.js
A +40 -0 rkward/plugins/00saveload/save/ExportRMarkdown.rkh
A +104 -0 rkward/plugins/00saveload/save/ExportRMarkdown.xml
M +3 -1 rkward/plugins/import_export.pluginmap
https://commits.kde.org/rkward/c1f150cd42402d4bad4a62f36b1ff1e58d2eaa50
diff --git a/rkward/plugins/00saveload/save/ExportRMarkdown.js b/rkward/plugins/00saveload/save/ExportRMarkdown.js
new file mode 100644
index 00000000..68caaa31
--- /dev/null
+++ b/rkward/plugins/00saveload/save/ExportRMarkdown.js
@@ -0,0 +1,122 @@
+// this code was generated using the rkwarddev package.
+// perhaps don't make changes here, but in the rkwarddev script instead!
+//
+// look for a file called: $SRC/inst/rkward/rkwarddev_rk.RMarkdown_plugin_script.R
+
+// define variables globally
+var markdownVersion;
+var targetFormat;
+
+function setGlobalVars(){
+ markdownVersion = getString("markdownVersion");
+ targetFormat = getString("targetFormat");
+}
+
+
+
+function preprocess(is_preview){
+ setGlobalVars();
+ // add requirements etc. here
+ echo("require(knitr)\n");
+
+ if(markdownVersion == 1) {
+ echo("require(markdown)\n\n");
+ } else {
+ echo("require(rmarkdown)\n");
+ if(targetFormat != "all" && targetFormat != "first" && targetFormat != "html_vignette") {
+ echo("rk.check.for.pandoc(\n stop_if_missing=TRUE,\n output_format=\"" + targetFormat + "\"\n)\n\n");
+ } else {}
+ }
+}
+
+function calculate(is_preview){
+ // read in variables from dialog
+ var markdownFile = getString("markdownFile");
+ var targetFile = getString("targetFile");
+ var targetDir = getString("targetDir");
+
+ // the R code to be evaluated
+ var fileExtension = targetFormat;
+ var renderFormat = "pandoc";
+ if(markdownVersion == 1) {
+ echo("md_tempfile <- tempfile()\n");
+ echo("knit2html(\n");
+ echo(" input=\"" + markdownFile + "\",\n output=md_tempfile,\n quiet=TRUE,\n");
+ echo(")\n");
+ fileExtension = 'html';
+ renderFormat = 'knit2html';
+ } else {
+ if(targetFormat == "all") {
+ renderFormat = 'all';
+ } else if(targetFormat == "first") {
+ renderFormat = 'NULL';
+ } else if(targetFormat == "beamer") {
+ fileExtension = 'pdf';
+ renderFormat = 'beamer_presentation';
+ } else if(targetFormat == "html") {
+ renderFormat = 'html_document';
+ } else if(targetFormat == "html5") {
+ fileExtension = 'html';
+ } else if(targetFormat == "markdown") {
+ fileExtension = 'md';
+ renderFormat = 'md_document';
+ } else if(targetFormat == "gfm" || targetFormat == "markdown_github") {
+ fileExtension = 'md';
+ renderFormat = 'github_document';
+ } else if(targetFormat == "odt") {
+ renderFormat = 'odt_document';
+ } else if(targetFormat == "latex") {
+ fileExtension = 'pdf';
+ renderFormat = 'pdf_document';
+ } else if(targetFormat == "pptx") {
+ renderFormat = 'powerpoint_presentation';
+ } else if(targetFormat == "rtf") {
+ renderFormat = 'rtf_document';
+ } else if(targetFormat == "html_vignette") {
+ fileExtension = 'html';
+ renderFormat = 'rmarkdown::html_vignette';
+ } else if(targetFormat == "docx") {
+ renderFormat = 'word_document';
+ } else {}
+ if(renderFormat == "pandoc") {
+ echo("md_tempfile <- tempfile()\n");
+ echo("suppressMessages(\n pandoc(\n");
+ echo(" input=knit(\n \"" + markdownFile + "\",\n output=md_tempfile,\n quiet=TRUE\n ),\n");
+ echo(" format=\"" + targetFormat + "\"\n");
+ echo(" )\n)\n");
+ } else {
+ echo("render(\n");
+ echo(" input=\"" + markdownFile + "\",\n");
+ if(renderFormat == "NULL") {
+ echo(" output_format=" + renderFormat + ",\n");
+ } else {
+ echo(" output_format=\"" + renderFormat + "\",\n");
+ }
+ if(renderFormat != "all" && renderFormat != "NULL") {
+ echo(" output_file=\"" + targetFile + "\",\n");
+ } else {
+ echo(" output_dir=\"" + targetDir + "\",\n");
+ }
+ echo(" quiet=TRUE\n");
+ echo(")\n");
+ }
+ }
+ if(renderFormat == "pandoc" || renderFormat == "knit2html") {
+ echo("\nfile.copy(\n");
+ echo(" from=paste0(md_tempfile, \"." + fileExtension + "\"),\n");
+ echo(" to=\"" + targetFile + "\",\n");
+ echo(" overwrite=TRUE\n)\n");
+ echo("unlink(paste0(md_tempfile, \"." + fileExtension + "\"))\n\n");
+ } else {}
+}
+
+function printout(is_preview){
+ // printout the results
+ if(targetFormat == "all" || targetFormat == "first") {
+ new Header(i18n("Export RMarkdown file")).addFromUI("markdownFile").addFromUI("targetFormat").addFromUI("targetDir").print();
+ } else {
+ new Header(i18n("Export RMarkdown file")).addFromUI("markdownFile").addFromUI("targetFormat").addFromUI("targetFile").print();
+ }
+
+}
+
diff --git a/rkward/plugins/00saveload/save/ExportRMarkdown.rkh b/rkward/plugins/00saveload/save/ExportRMarkdown.rkh
new file mode 100644
index 00000000..17745c67
--- /dev/null
+++ b/rkward/plugins/00saveload/save/ExportRMarkdown.rkh
@@ -0,0 +1,40 @@
+<!DOCTYPE rkhelp >
+<document>
+ <!-- this code was generated using the rkwarddev package.
+ perhaps don't make changes here, but in the rkwarddev script instead! -->
+ <summary>
+ </summary>
+ <usage>
+ </usage>
+ <!-- <section id="sct_EDITORDE" title="EDIT OR DELETE ME">
+ EDIT OR DELETE ME
+ </section> -->
+ <settings>
+ <caption id="tabSourceAndTarget" />
+ <setting id="markdownFile">
+ Choose the file to be exported.
+ </setting>
+ <setting id="targetFormat">
+ Set the target format for exported files. If you choose either "All formats defined in document" or "First format defined in document", the formats defined via output_format will be used, see the documenation on rmarkdown::render().
+ </setting>
+ <setting id="targetFile">
+ Choose the target file name to export to.
+ </setting>
+ <setting id="targetDir">
+ Choose the target directory to export to, file names are automatically generated according to the formats defined in the document.
+ </setting>
+ <caption id="tabAdvancedOptions" />
+ <setting id="markdownVersion">
+ Set the markdown version to use. Older v1 documents are rendered into HTML format only, using the markdown package. If in doubt, leave it to the newer v2; those documents are rendered using pandoc, which allows you to choose between various output formats. But be aware that some formats may require special configuration or templates to produce proper results.
+ </setting>
+ </settings>
+ <!-- <related>
+ <ul>
+ <li>
+ <link href="rkward://rhelp/..." />
+ </li>
+ </ul>
+ </related> -->
+ <technical>
+ </technical>
+</document>
diff --git a/rkward/plugins/00saveload/save/ExportRMarkdown.xml b/rkward/plugins/00saveload/save/ExportRMarkdown.xml
new file mode 100644
index 00000000..02fb1511
--- /dev/null
+++ b/rkward/plugins/00saveload/save/ExportRMarkdown.xml
@@ -0,0 +1,104 @@
+<!DOCTYPE rkplugin >
+<document>
+ <!-- this code was generated using the rkwarddev package.
+ perhaps don't make changes here, but in the rkwarddev script instead!
+
+ look for a file called: $SRC/inst/rkward/rkwarddev_rk.RMarkdown_plugin_script.R -->
+ <code file="ExportRMarkdown.js" />
+ <help file="ExportRMarkdown.rkh" />
+ <!-- <dependencies>
+ </dependencies> -->
+ <about name="Export RMarkdown" shortinfo="RKWard GUI to export RMarkdown documents in various formats" longinfo="RKWard GUI to export RMarkdown documents in various formats" version="0.01-1" releasedate="2018-10-30" url="https://rkward.kde.org" license="GPL (>= 3)">
+ <author given="Meik" family="Michalke" email="meik.michalke at hhu.de" role="aut, cre" />
+ </about>
+ <logic>
+ <convert id="logicMDVersion" sources="markdownVersion.string" mode="equals" standard="2" />
+ <connect governor="logicMDVersion" client="targetFormat.enabled" />
+ <connect governor="current_filename" client="markdownFile.selection" />
+ <convert id="logicMDAllFormats" sources="targetFormat.string" mode="equals" standard="all" />
+ <convert id="logicMDFirstFormat" sources="targetFormat.string" mode="equals" standard="first" />
+ <convert id="logicMDAllOrFirst" sources="logicMDAllFormats;logicMDFirstFormat" mode="or" />
+ <connect governor="logicMDAllOrFirst.not" client="targetFile.visible" />
+ <connect governor="logicMDAllOrFirst.not" client="targetFile.required" />
+ <connect governor="logicMDAllOrFirst" client="targetDir.visible" />
+ <connect governor="logicMDAllOrFirst" client="targetDir.required" />
+ <script>
+ <![CDATA[
+ gui.addChangeCommand("markdownFile.selection", "dataChanged()");
+ gui.addChangeCommand("targetFormat.string", "dataChanged()");
+ gui.addChangeCommand("markdownVersion.string", "dataChanged()");
+ // this function is called whenever a new source file
+ // is selected or the target format changes
+ dataChanged = function(){
+ var fromFile = gui.getString("markdownFile");
+ var toFile = "";
+ var fileExt = gui.getString("targetFormat");
+ if(fileExt == "all" || fileExt == "first"){
+ toFile = fromFile.substr(0, fromFile.lastIndexOf("/"));
+ gui.setValue("targetDir.selection", toFile);
+ } else {
+ var mdVers = gui.getString("markdownVersion");
+ if(mdVers == "1" || fileExt == "html5" || fileExt == "html_vignette"){
+ fileExt = "html";
+ } else if(fileExt == "beamer" || fileExt == "latex"){
+ fileExt = "pdf";
+ } else if(fileExt == "markdown" || fileExt == "markdown_github" || fileExt == "gfm"){
+ fileExt = "md";
+ } else {};
+ var fex = fromFile.lastIndexOf(".");
+ toFile = fromFile.substr(0, fex < 0 ? fromFile.length : fex) + "." + fileExt;
+ // prevent overwriting source with target
+ if(fromFile == toFile){
+ toFile = fromFile.substr(0, fex < 0 ? fromFile.length : fex) + "_new." + fileExt;
+ } else {}
+ gui.setValue("targetFile.selection", toFile);
+ }
+ }
+ dataChanged (); // initialize
+ ]]>
+ </script>
+ </logic>
+ <dialog label="Export RMarkdown">
+ <tabbook id="exportTabbook" label="Export RMarkdown">
+ <tab label="Source and target" id="tabSourceAndTarget">
+ <row id="row_cRMFFTAFBD">
+ <column id="clm_bRMFFTAFBD">
+ <browser label="RMarkdown file" type="file" id="markdownFile" filter="*.rmd *.md" />
+ <dropdown id="targetFormat" label="Target format">
+ <option label="All formats defined in document" value="all" />
+ <option label="First format defined in document" value="first" checked="true" />
+ <option label="Beamer presentation" value="beamer" />
+ <option label="DokuWiki" value="dokuwiki" />
+ <option label="HTML" value="html" />
+ <option label="HTML 5" value="html5" />
+ <option label="Markdown" value="markdown" />
+ <option label="Markdown (GitHub, old)" value="markdown_github" />
+ <option label="Markdown (GitHub)" value="gfm" />
+ <option label="MediaWiki" value="mediawiki" />
+ <option label="ODT" value="odt" />
+ <option label="PDF" value="latex" />
+ <option label="PowerPoint" value="pptx" />
+ <option label="RTF" value="rtf" />
+ <option label="Vignette (HTML)" value="html_vignette" />
+ <option label="Word" value="docx" />
+ </dropdown>
+ <browser label="Save as" type="savefile" id="targetFile" required="false" />
+ <browser label="Save to" type="dir" id="targetDir" required="false" />
+ <stretch />
+ </column>
+ </row>
+ </tab>
+ <tab label="Advanced options" id="tabAdvancedOptions">
+ <row id="row_cVM1122VM1">
+ <column id="clm_rddmVM1122">
+ <radio id="markdownVersion" label="Markdown version">
+ <option label="v1" value="1" />
+ <option label="v2" value="2" checked="true" />
+ </radio>
+ <stretch />
+ </column>
+ </row>
+ </tab>
+ </tabbook>
+ </dialog>
+</document>
diff --git a/rkward/plugins/import_export.pluginmap b/rkward/plugins/import_export.pluginmap
index 95c736ff..4549be4a 100644
--- a/rkward/plugins/import_export.pluginmap
+++ b/rkward/plugins/import_export.pluginmap
@@ -25,6 +25,7 @@
<component type="standard" id="save_skeleton" file="save/skeleton/description.xml" label="Create package skeleton" />
<component type="standard" id="save_variables" file="save/write.xml" label="Export vector or matrix data" />
<component type="standard" id="save_csv" file="save/write_csv.xml" label="Export Table / CSV files" />
+ <component type="standard" id="save_rmarkdown" file="save/ExportRMarkdown.xml" label="Export RMarkdown" />
<component type="standard" id="import_spss" file="import/import_spss.xml" label="Import SPSS">
<attribute id="format" value="*.sav *.por" label="SPSS data files"/>
@@ -61,7 +62,8 @@
<menu id="export" label="Export">
<entry component="save_r" />
<entry component="save_variables" />
- <entry component="save_csv" />
+ <entry component="save_csv" />
+ <entry component="save_rmarkdown" />
<entry component="save_skeleton" />
</menu>
</menu>
More information about the rkward-tracker
mailing list