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

.cb.run behaves inconsistently between languages in respect to implicit printing. #11

Closed
EmmanuelCharpentier opened this issue Oct 3, 2019 · 2 comments
Labels
doc Documentation

Comments

@EmmanuelCharpentier
Copy link

According to the current state oif the documentation, .cb.run should give "raw" output. This does not happen with "implicit" output, i. e. the result of an evaluation without explicit printing: in this case, most of the interpreters print the result to their stanrd output.

Nothing is printed when an inline expression in Python, Sage and Julia ; however, both R and bash give their raw output. When exported to markdown, the following code :

Language `run`                       `nb`                        `expr`                       `run`+print
------- --------------------------- ---------------------       -----------------------      ------------------------------------
Python   `1+2`{.python .cb.run}      `1+2`{.python .cb.nb}       `1+2`{.python .cb.expr}      `print(1+2)`{.python .cb.run}
Sage     `1+2`{.sage .cb.run}        `1+2`{.sage .cb.nb}         `1+2`{.sage .cb.expr}        `print(1+2)`{.sage .cb.run}
Julia    `1+2`{.julia .cb.run}       `1+2`{.julia .cb.nb}        `1+2`{.julia .cb.expr}       `print(1+2)`{.julia .cb.run}
R        `1+2`{.R .cb.run}           `1+2`{.R .cb.nb}            `1+2`{.R .cb.expr}           `print(1+2)`{.R .cb.run}
bash     `expr 1 + 2`{.bash .cb.run} `expr 1 + 2`{.bash .cb.nb}` `expr 1 + 2`{.bash .cb.expr} `echo $(expr 1 + 2)`{.bash .cb.run}

results in

  Language     `run`        `nb`       `expr`   `run`+print
  ---------- --------- -------------- -------- -------------
  Python                 `3`{.expr}      3           3
  Sage                   `3`{.expr}      3           3
  Julia                  `3`{.expr}      3           3
  R           \[1\] 3    `3`{.expr}      3        \[1\] 3
  bash           3      `3`{.expr}\`     3           3

And the export to PDF via latex gives "teletype" printing in the nb column and normal (i. e. roman) printing in the other columns.

Similarly, the following code:

## Block syntax

### Python

```{.python .cb.run}
1+2
```
### Sage

```{.sage .cb.run}
1+2
```

### Julia

```{.julia .cb.run}
1+2
```

### R

```{.R .cb.run}
1+2
```

### Bash

```{.bash .cb.run}
expr 1 + 2
```

Gives the following result:

Block syntax
------------

### Python

### Sage

### Julia

### R

\[1\] 3

### Bash

3

In both cases (inline and block), Python's, Sage's and Julia's implicit printings do not get in the output, whereas bash's and R's do.

This inconsistency should be fixed or, to the least, documented.

The same problem exists for the implicit printing of figures created by R or Sage. This should at least be documented...

@gpoore
Copy link
Owner

gpoore commented Oct 3, 2019

For .cb.run, the documentation currently has

.cb.run — Run code and interpret any printed content (stdout) as Markdown. Also insert stderr verbatim if it exists. When a Jupyter kernel is used, rich output is also displayed.

The examples you are showing seem consistent with that. Bash and R write to stdout under a lot of circumstances when Python, Julia, and other languages don't. Running bash -c 'expr 1+2' or Rscript -e '1+1' give output in a terminal, but python -c '1+1' and julia -e '1+1' don't. This seems to be just a difference in how languages work. This is also why .cb.expr exists, so that you can get a result without explicit printing. There may be a way to rephrase the documentation to clarify things further.

.cb.nb interprets all output literally (verbatim) rather than as Markdown, which is why LaTeX will display it with teletype fonts. If you think the documentation needs modification here, let me know:

.cb.nb — Execute code in notebook mode. For inline code, this is equivalent to .cb.expr with verbatim output unless a Jupyter kernel is used, in which case rich output will be displayed. For code blocks, this inserts the code verbatim, followed by any printed output (stdout) verbatim. If stderr exists, it is also inserted verbatim. When a Jupyter kernel is used, rich output is also displayed.

Can you say more about implicit figures for R and Sage? I don't have much experience creating figures with those languages.

@EmmanuelCharpentier
Copy link
Author

The examples you are showing seem consistent with that. Bash and R write to stdout under a lot of circumstances when Python, Julia, and other languages don't. Running bash -c 'expr 1+2' or Rscript -e '1+1' give output in a terminal, but python -c '1+1' and julia -e '1+1' don't. This seems to be just a difference in how languages work.

These differences should be underscored in the doc: after using R for more than 20 years and Sage for about 6 years, I ignored this (to be more precise, my everyday use of R and Sage never involved this). I tend to use both R and Sage in interactive session (usually via emacs) to prepare code used in LaTeX or org-mode files. knitr, SageTeX and org-mode manage this (more or less) transparently.

Possible redaction :

.cb.run — Run code in (a script called from the command line) and interpret any printed content (stdout) as Markdown. The implicit printing done by some interpreters in "interactive sessions" only if their script interpreter manage it. Examples : bash and R do manage implicit printing, Sage, Julia and Python do not.

Also insert stderr verbatim (i. e . embedded in ` marks) if it exists. When a Jupyter kernel is used, rich output is also displayed.

This last phrase needs, IMHO, expansion/clarifications...

This is also why .cb.expr exists, so that you can get a result without explicit printing. There may be a way to rephrase the documentation to clarify things further.

Possibly illustrate by examples/counterexamples.

Can you say more about implicit figures for R and Sage? I don't have much experience creating figures with those languages.

In interactive sessions, R and Sage (and, IIRC, Python/matprlotlib) open graphical windows and display the graphs. knitr manages this (silently) for R graphical output ; SageTeX macroes do the same. Ditto for org-mode, at least for R and Sage (and, among other, Graphviz) plots.

This transparency does not exist in codebraid.

I am thinking that a (langiage-dependent) set of helper wrappers might be a bonus for codebraiod users, and have some ideas for R and Sage. Possible targets

  • Verbatim output of results
  • tables (with/without legend and (cross-)reference)
  • figures (ditto)
  • (at least for Sage and Sympy): LaTeX versions thereof...

@gpoore gpoore added the doc Documentation label Feb 6, 2020
@gpoore gpoore closed this as completed in c9db866 May 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation
Projects
None yet
Development

No branches or pull requests

2 participants