Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rendering R 4.1's native pipe #1157

Closed
edzer opened this issue May 19, 2021 · 14 comments
Closed

rendering R 4.1's native pipe #1157

edzer opened this issue May 19, 2021 · 14 comments
Labels
next to consider for next release
Milestone

Comments

@edzer
Copy link

edzer commented May 19, 2021

I'm not sure whether this is an issue with bookdown, knitr or some other package, but it looks as if the R 4.1 native pipe symbol |> is not rendered correctly:
pipe

The same might be the case for the new => infix function operator and the prefix \(

@cderv
Copy link
Collaborator

cderv commented May 20, 2021

Hi @edzer,

Just to understand correctly, so that we are on the same page: How do you expect |> to look like in the code chunk output of an HTML document ?

Is it because > is highlighted in red ? If so, I believe this would be an issue with the highlighting parser.
Which highlighting configuration are you using ? The default one in gitbook() ? I can reproduce this red color with the default - it is using Pandoc to highlight code chunk and using the pygments theme.

This can be reproduce in a simple document with html_document

---
title: test
output: 
  html_document: 
    highlight: pygments
---

```{r}
f <- function(x) { x + 1 }
2 |> f()
```

```{r}
(\(x) x + 1)(2)
```

image

I don't know if this is a syntax highlight theme issue or if this requires some changes in the Pandoc syntax highlighter. For gitbook() format, only Pandoc syntax highlighting can be use for now.

Or are you expecting ligatures ? If so I believe for that, you would need to use a font for code that supports ligature. It is the same in Terminal or maybe even in IDE if you use a font that does not support ligatures.

For example, now that rmarkdown integrates with bslib, you could easily change fonts this way. For example with Fira Code, this would look different

---
title: test
output: 
  html_document:
    theme:
      version: 4
      code_font:
        google: Fira Code
---

```{r}
f <- function(x) { x + 1 }
2 |> f()
```

image

Does it looks like more what you expect ?

BTW There are several ways to change fonts and using bslib with rmarkdown is not the only option.

@cderv
Copy link
Collaborator

cderv commented May 20, 2021

Note for self:
Looking at the HTML code generated, the Pandoc highlighting gives us this:

<pre class="sourceCode r"><code class="sourceCode r">
<span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>f <span class="ot">&lt;-</span><span class="cf">function</span>(x) { x <span class="sc">+</span> <span class="dv">1</span> }</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="dv">2</span> <span class="sc">|</span><span class="er">&gt;</span> <span class="fu">f</span>()</span>
</code></pre>

So |> gives <span class="sc">|</span><span class="er">&gt;</span> with two different classes.

Currently, themes gives different color to those two classes.

Supporting downlit as R parser instead of Pandoc could help solves this - which is something we are looking into: rstudio/rmarkdown#1881

@edzer
Copy link
Author

edzer commented May 20, 2021

Thanks @cderv for looking into this! I'm looking at the first case - despite the coolness of these ligatures, I think that code sections are most helpful if they show how you would type them. Using this R code:

Sys.setenv("_R_USE_PIPEBIND_"="true") # needs to have it's own code chunk!
f <- function(x) { x + 1 }
2 |> x => sin(x)
2 |> (\(x) { sin(x) })()

using this in your pygments example above, I see
pipe
which shows |> and => in two colors, suggesting they're both not a single operator, which they are. That is the issue I have.

@cderv
Copy link
Collaborator

cderv commented May 20, 2021

Thanks a lot for the example. Then this is indeed due the syntax highlighter used. With gitbook(), Pandoc highlighting will be used and it does not now about this operator. This is a Pandoc issue really for this part. We need to teach the syntax parser in Pandoc about the new operator and / or adapt the highlighting themes.

There would be workaround that have less different colors as mentioned earlier but they are not yet available easily in gitbook().

This is something will definitely need to improve. Thanks for the report on this. I had not tried it yet so I did not know about this issue. 😅

@yihui
Copy link
Member

yihui commented May 20, 2021

We need to file an issue to Pandoc's syntax highlighter. Or if you are able to, send a pull request (which is probably not too hard): https://github.com/jgm/skylighting/blob/master/skylighting-core/xml/r.xml

Before it is fixed upstream, we can definitely also fix it on our side for now (which should not be hard).

@cderv
Copy link
Collaborator

cderv commented May 20, 2021

yes I started looking and asked for confirmation on how to do it.

But the changes has to be done upstream in KDE (https://invent.kde.org/frameworks/syntax-highlighting) and not directly in skylighting.

Also --syntax-definition pandoc flag can be useful if we want to import the changes in previous Pandoc version.

@yihui yihui added the next to consider for next release label May 28, 2021
@yihui yihui added this to the v0.23 milestone May 28, 2021
@yihui
Copy link
Member

yihui commented Jul 28, 2021

This is (temporarily) fixed in rmarkdown. You can test the development version via

remotes::install_github('rstudio/rmarkdown')

Thanks!

@cderv
Copy link
Collaborator

cderv commented Jul 28, 2021

Thanks @yihui ! I'll work on the --syntax-definition file for rmarkdown soon so that we can have a proper fix, and push something on Pandoc for later version. I'll open an issue on rmarkdown side for this.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Sep 5, 2021
rmarkdown 2.10
=========================================================================

- `md_document()` will now handle correctly `preserve_yaml` value for
  all variants and all pandoc versions (#2190).
  * with `preserve_yaml = TRUE`, markdown output will keep the YAML
    metadata block from the Rmd file.
  * with `preserve_yaml = FALSE`, markdown output will have no YAML
    metadata block.

  This fixes a breaking change in Pandoc 2.13 regarding `gfm`,
  `commonmark` and `commonmark_x` which now supports
  `yaml_metadata_block` by default (#2118).

- New supported syntax for Shiny prerendered documents: you can now
  use `server: shiny` or `server: type: shiny`.

- Ability to inject additional functions into Shiny prerendered server
  scope using the "server-extras" context.

- Fixed the syntax highlighting issue with R's pipe operator `|>`
  (thanks, @edzer, rstudio/bookdown#1157).


rmarkdown 2.9
==========================================================================

- Fix a regression in version 2.8 when a url is used in `css` argument
  (thanks, @vnijs, #2163).

- All HTML dependencies are now correctly supported, included those
  with only an `href` component but not `file` component in their
  `src` attribute. Previously, **rmarkdown** would throw the error
  `'path for html_dependency not provided'` when rendering documents
  containing HTML dependencies with `href` components (thanks,
  @crazycapivara, @matthewstrasiotto, #1805, #1948, #2151).

- Fix an error thrown with output format using a `file_scope` function
  (like in **bookdown**) (thanks, @rfaelens, #2149).

- Fix an issue with `copy_ressource = TRUE` in `html_document_base`
  where very long HTML documents were truncated during post processing
  (thanks, @oliviermeslin, #2145).

- When `run()`-ing a `runtime: shiny` document, an extra temp folder
  will be used in the output path. With the extra temp random folder
  in the path, predictable output file names may be used. (#2137)

- When `run()`-ing a `runtime: shiny` document with a `{bslib}` theme,
  the global theme value wasn't being restored properly. (#2160)

- Floating ToC in `html_document` can now hide headings with
  unnumbered and unlisted classes (thanks, @atusy, #1993).

- Fix prefix handling in R Markdown website's navbar for Fontawesome
  V5 and compatibility with V4. For icon only available in V5, the
  full prefix + name should be use, especially with new `fab` prefix
  (e.g. `fab fa-r-project`). If no prefix is used (e.g `fa-home`
  instead of `fas fa-home`), the `fa` prefix will be added for V4
  compatibility as it has been deprecated in V5. We advice to use the
  full prefix + name for icons following Fontawesome
  documentation. (#1994)

- `rmarkdown::site_generator()` can hang session waiting for input
  when the `site` field is not found in the YAML frontmatter of
  `index.Rmd` (thanks, @kevinushey @mirh, #2043).
@eddelbuettel
Copy link

There is a similar issue in pdf output where

data |> select(colA, colB) |> head()

turns into

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data }\SpecialCharTok{|}\ErrorTok{\textgreater{}} \FunctionTok{select}\NormalTok{(colA, colB) }\SpecialCharTok{|}\ErrorTok{\textgreater{}} \FunctionTok{head}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

Is there a known trick to get a ligature in pdf?

@cderv
Copy link
Collaborator

cderv commented Oct 4, 2021

Hi @eddelbuettel,

The fix in bookdown was only for HTML output. We need to fix it more globally and upstream - this is tracked in rstudio/rmarkdown#2196.

Is there a known trick to get a ligature in pdf?

First thing is the correctly syntax highlighting. The known trick to get correct syntax highlighting would be to use a patch KDE XML syntax definition file.
As I said, we still need to PR upstream in KDE with this fix so that it can go inside Pandoc, then we can benefit.

In the meantime I published a gist with a patched xml file you could use: https://gist.github.com/cderv/079b78caefbe45da6d1c03d737970b55

You could download it and use it by adding pandoc_args in your YAML header. e.g with pdf_document()

output: 
  pdf_document:
    pandoc_args: "--syntax-definition=r.xml"

This would lead to

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mtcars }\SpecialCharTok{|\textgreater{}} \FunctionTok{head}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

Including this definition file in rmarkdown directly is part of rstudio/rmarkdown#2196 - Still on the todo list though.

Second thing is the Ligature. To get the ligature in the pdf output, I guess you would need a font that supports it like Fira Code.

Here is an example :

---
title: "Minimal Example"
output: 
  pdf_document:
    latex_engine: lualatex
    highlight: zenburn
    pandoc_args: "--syntax-definition=r.xml"
monofont: 'Fira Code'
---

```{r}
mtcars |> head()
```

image

@eddelbuettel
Copy link

Very nice! The syntax checker is already a winner, I am driving this (as my usual slide desks) via binb's variant of metropolis and while it has a monofont option this doesn't (yet) pass through -- but for the one lecture I am using this for it may also better to not just yet jump to ligatures. But I will try to work out where/if the setup gets in the way and overrides.

@cderv
Copy link
Collaborator

cderv commented Oct 4, 2021

@eddelbuettel this should now been fixed for pdf_document() using a patch that @yihui pushed. Same trick as for html_document() to wait for upstream support of the native pipe.

This is in the dev version of rmarkdown.

@eddelbuettel
Copy link

@cderv Sweet. Given that I have a commonality via my binb package I had contemplated putting the modified XML in there; now I don't need to do that.

The ligature fonts do not come out in pdf if I use binb/metropolis. I pokes a little around the created tex file, and as it goes with such files there are quite a few other packages involved. For now I will keep it at the improved fixed width / non-ligature display in pdf.

Thanks again!

jonathan-g added a commit to jonathan-g/rmarkdown that referenced this issue Oct 30, 2021
Merge remote-tracking branch 'rstudio_origin/main' into jg-tree-fix

# By Christophe Dervieux (2) and Yihui Xie (1)
# Via Christophe Dervieux
* rstudio_origin/main:
  Add class to book cover to size using CSS
  fix the syntax highlighting of |> in LaTeX/PDF output: rstudio/bookdown#1157 (rstudio#2228)
  Add an option to opt-out the header-attrs HTML dependency in `html_document_base()` (rstudio#2227)

# Conflicts:
#	NEWS.md
jonathan-g added a commit to jonathan-g/rmarkdown that referenced this issue Nov 29, 2021
Merge branch 'jg-tree-fix' into jg-devel

* jg-tree-fix:
  Use latest Pandoc  in GHA and update workflow (rstudio#2244)
  Add class to book cover to size using CSS
  fix the syntax highlighting of |> in LaTeX/PDF output: rstudio/bookdown#1157 (rstudio#2228)
  Add an option to opt-out the header-attrs HTML dependency in `html_document_base()` (rstudio#2227)
  Clean up NEWS.md.

# Conflicts:
#	DESCRIPTION
#	NEWS.md
jonathan-g added a commit to jonathan-g/rmarkdown that referenced this issue Nov 29, 2021
* jg-devel: (35 commits)
  Use latest Pandoc  in GHA and update workflow (rstudio#2244)
  Add class to book cover to size using CSS
  fix the syntax highlighting of |> in LaTeX/PDF output: rstudio/bookdown#1157 (rstudio#2228)
  Add an option to opt-out the header-attrs HTML dependency in `html_document_base()` (rstudio#2227)
  Clean up NEWS.md.
  Updated autogenerated documentation.
  Fixed conflicts with rstudio main version.
  Update documentation for html_document.
  Add self (JG) as a contributor in DESCRIPTION.
  Resolve conflicts with new updates to RStudio main branch.
  Clean up a few things from the merge.
  Fix problem building with GitHub Actions because of `mathjax: local` in the testthat tests.
  Revert unnecessary formatting change to code in render.R.
  Revert an unnecessary formatting change to comments in render.R.
  Update NEWS.md.
  revert inadevertent change from accidental merge with jg-tree-fix.
  Removed unnecessary references to `allow_uptree_lib_dir` in `ioslides_presentation` and `slidy_presentation`.
  Prepare for PR to RStudio.
  Updated documentation for new parameter in output_format().
  Removed packages.bib from tests/testthat.
  ...
clrpackages pushed a commit to clearlinux-pkgs/R-rmarkdown that referenced this issue Mar 7, 2022
…n 2.12

Carson Sievert (1):
      Add BS5's data-bs-* attributes where it's needed so dropdowns and navbar collapsing work with Bootstrap 5 (#2317)

Christophe Dervieux (40):
      Add support for devtools loaded package in `draft()` (#2224)
      Add an option to opt-out the header-attrs HTML dependency in `html_document_base()` (#2227)
      Add class to book cover to size using CSS
      Use latest Pandoc  in GHA and update workflow (#2244)
      Update GHA following v2 tag in r-lib/actions (#2260)
      Add Config/Needs/website key
      Add a GHA workflow to generate rmarkdown CITATION.cff file (#2265)
      Add mention of RStudio's Viewer Pane not working for ioslides_presentation()
      Update links to Cheatsheets in vignette
      Only run the update of cff file on main branch
      Rework anchor_sections to change default behavior (#1964)
      Add dir variable to HTML templates (#2271)
      Add a LICENSE.md file in repo (#2275)
      Typo in documentation
      Add more highlighting support for HTML output (#1941)
      Support custom highlight theme in other formats (#2285)
      Support incremental for Powerpoint presentation (#2286)
      Add a available_templates() function to list templates (#2287)
      Add missing topic for pkgdown website
      typo in help page
      Add 2 more functions in `shared.lua`
      Revert "Add 2 more functions in `shared.lua`"
      Add function in `shared.lua` for sharing fixes in Lua filters across packages (#2291)
      Just skipping the test on windows for now (#2292)
      typo in inserted head
      Support custom url for KaTeX as `math_method` (#2296)
      Fix header-includes and in_header in `beamer_presentation` (#2299)
      Pandoc requires a `=` between math flag and its url value (#2300)
      mathjax local needs output_dir and lib_dir (#2302)
      For webtex, defaulting to SVG as this is far better rendering
      Support R KaTeX package as a new `math-method`(#2304)
      Correctly handle preserve_yaml in markdown formats (#2303)
      Update NEWS regarding `md_document()` changes
      display single line summary content inline
      Allow empty server context in shiny prerendered (#2305)
      Support webtex for Github documents format (#2301)
      generate Citation file only at each release
      post-processing tex file in pdf document is not yet possible (#2320)
      Add revdep folder to buildignore
      Update badge link

Garrick Aden-Buie (1):
      Add `$pkgVersion` to shiny prerendered dependencies (#2218)

Gary (1):
      Update CLA links and email alias (#2312)

Yihui Xie (7):
      import bslib (#2154)
      fix the syntax highlighting of |> in LaTeX/PDF output: rstudio/bookdown#1157 (#2228)
      tweak and reorder news (#2321)
      circumvent ThinkR-open/gitdown#17
      add the `highlight` argument to html_vignette() to avoid the issue in molic: mlindsk/molic#30
      remove the test accordingly
      CRAN release v2.12

atusy (3):
      Fix broken links by number-sections.lua on markdown flavors (#2093)
      Support all math rendering engines from Pandoc in HTML documents (#1940)
      add shiny_prerendered special comment via pandoc_include_args (#2249)

cderv (20):
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff
      Update CITATION.cff

christophe dervieux (1):
      Fix typo in comment
@github-actions
Copy link

github-actions bot commented Apr 6, 2022

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
next to consider for next release
Projects
None yet
Development

No branches or pull requests

4 participants