Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do `.sublime-build`s work?

Update:

My problem has evolved after trying various things detailed in the comments to this question. The problems previously described in this question may indeed have been due to bad JSON, as suggested by @apple16, or related to my "$PATH" configuration, as discussed with @RandyLai.

I may have identified the underlying problem: character encoding. Using my build variant in Sublime Text with diacritics in the YAML front-matter of my .Rmd document (which I had been doing when asking the original question here) results in an error different from those described initially, which I no longer seem to get:

Error en yaml::yaml.load(front_matter)

Interestingly, this error is not thrown with the same YAML in RStudio or in Terminal (or from a shell script), where everything renders perfectly. I get no error in ST when I remove the diacritics from the YAML; however, if I put diacritics in the main document, these are rendered as NA in the resulting HTML. This kind of resembles what was happening previously that I described below as "wackily wrong" HTML output.

In the comments to this question I attempt to do various things to my "$PATH" which now, as they were for @RandyLai, appear unnecessary; I am referring specifically to adding the path to pandoc to /etc/launchd.config: after having restored the default path /usr/bin:/bin:/usr/sbin:/sbin, everything works in ST as I am describing it now, whether I use my build variant in SublimeKnitr-knitr's .sublime-build or in R Box-R Markdown's .sublime-build.

Here is my locale information, in case it is of use:

Mi-Mac:~ LeGastronome$ locale
LANG=
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

I am trying to find out how Sublime Text executes its .sublime-build files.

In Sublime Text 3 on my Mac (v10.9.3) I have created a custom build variant for both SublimeKnitr's knitr-Markdown and R Box's R Markdown syntaxes; here is my .sublime-build file for the former, which has the fully functional original/default build followed by my faulty variant:

{
  "selector": "text.html.markdown.knitr",
  "working_dir": "${project_path:${folder}}",
  "cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ],
  "shell": true,

  "variants":
  [
    {
      "name": "Render",
      "working_dir": "$file_path",
      "shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
    }
  ]
}

And here is my .sublime-build for R Box's R Markdown, which is quite similar to SublimeKnitr's knitr-Markdown build:

{
    "selector": "text.html.rmarkdown",
    "working_dir": "${project_path:${folder}}",
    "cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ],
    "shell": true,

  "variants":
  [
    {
      "name": "Render",
      "working_dir": "$file_path",
      "shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
    }
  ]
}

In my custom build variants I have tried using both the pair of keys cmd and shell, as well as shell_cmd as cited in the code above, which I believe is new to ST3. In any case, both do undesirable things that happen neither when I run, in Terminal:

Rscript -e "rmarkdown::render(input = 'path/to/file.Rmd')"

nor when I run, in RStudio:

rmarkdown::render(input = 'path/to/file.Rmd')

I get the same results using SublimeKnitr's build as I do using R Box's build. The output I get is the following:

  1. with the mappings:

    "cmd": [ "Rscript -e \"rmarkdown::render(input = '$file')\"" ],
    "shell": true
    

    I get:

    Error: pandoc version 1.12.3 or higher is required and was not found.
    Ejecuci'on interrumpida
    [Finished in 0.4s with exit code 1]
    [cmd: ['Rscript -e "rmarkdown::render(input = \'/path/to/file.Rmd\')"']]
    [dir: /path/to/pertinent/directory]
    [path: /usr/bin:/bin:/usr/sbin:/sbin]
    

    Note that I do indeed have pandoc installed and fully operative, e.g.:

    Mi-Mac:~ LeGastronome$ pandoc --version
    pandoc 1.12.4.2
    Compiled with texmath 0.6.6.1, highlighting-kate 0.5.8.1.
    Syntax highlighting is supported for the following languages:
        actionscript, ada, apache, asn1, asp, awk, bash, bibtex, boo, c, changelog,
        clojure, cmake, coffee, coldfusion, commonlisp, cpp, cs, css, curry, d,
        diff, djangotemplate, doxygen, doxygenlua, dtd, eiffel, email, erlang,
        fortran, fsharp, gcc, gnuassembler, go, haskell, haxe, html, ini, isocpp,
        java, javadoc, javascript, json, jsp, julia, latex, lex, literatecurry,
        literatehaskell, lua, makefile, mandoc, markdown, matlab, maxima, metafont,
        mips, modelines, modula2, modula3, monobasic, nasm, noweb, objectivec,
        objectivecpp, ocaml, octave, pascal, perl, php, pike, postscript, prolog,
        python, r, relaxngcompact, restructuredtext, rhtml, roff, ruby, rust, scala,
        scheme, sci, sed, sgml, sql, sqlmysql, sqlpostgresql, tcl, texinfo, verilog,
        vhdl, xml, xorg, xslt, xul, yacc, yaml
    Default user data directory: /Users/LeGastronome/.pandoc
    Copyright (C) 2006-2014 John MacFarlane
    Web:  http://johnmacfarlane.net/pandoc
    This is free software; see the source for copying conditions.  There is no
    warranty, not even for merchantability or fitness for a particular purpose.
    
  2. with the mapping:

    "shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
    

    I get the desired type of document, HTML, but the HTML is wackily wrong and different from what I get when running the aforementioned code from Terminal and RStudio; additionally, exclusively in Sublime Text I get the following warning:

    pandoc: YAML header is not an object "source" (line 1, column 1)
    

    Note that if, as in the original build configurations cited above, I use brackets "[" like so:

    "shell_cmd": [ "Rscript -e \"rmarkdown::render(input = '$file')\"" ]
    

    Sublime does not build nor do anything other than save my file when I attempt to run the build variant.

My problem seems rather user-specific for which reason I am looking for more precise information as to how .sublime-build files are executed. I understand from some unofficial documentation that by default a bit of Python is run; however, how can it be possible that, as mentioned above, when I run the same bit of code from Terminal as is included in my build variant, i.e.:

Rscript -e "rmarkdown::render(input = 'path/to/file.Rmd')"

I get such undesirably different results from Sublime Text? Thanks for any help.

like image 803
user109114 Avatar asked Nov 01 '22 22:11

user109114


1 Answers

It seems that your system is not in English. When you run R in terminal, since "LC_CTYPE" is utf-8, R will read documents as UTF-8 documents. However, if the command is launched inside Sublime, "LC_CTYPE" is empty, thus R doesn't know what encoding to use.

There are several solutions.

  1. The easiest but less attractive solution is to switch your system language to English.

  2. Another possible way is to change your R default locale to utf8 by adding LANG=en_US.UTF-8 to ~/.Renviron

  3. Another possible solution is to add the variable LANG in the .sublime-build file. Check the option env here

  4. Or specify UTF8 encoding in the rmarkdown::render function.

like image 62
Randy Lai Avatar answered Nov 14 '22 05:11

Randy Lai