sagemath / sagecell

The Sage Cell Server---providing a way to embed Sage computations into any web page.
Other
201 stars 70 forks source link

Suppress printing of NULL values in output from R #565

Closed danm0nster closed 2 years ago

danm0nster commented 2 years ago

If the language is set to R the output of the last command is always shown. This makes is quite hard to get clean output because of the automatic printing. In addition, if the output of the last command is NULL it will result in sagecell printing this:

<rpy2.rinterface_lib.sexp.NULLType object at 0x7f0954e16a00> [RTYPES.NILSXP]

Quite often I just want the output to be a plot and nothing else. But this seems to be impossible.

As an example the following code gives the output shown below. Note the grey box above the plot which at least it empty, but I would prefer not to have at all.

library(ggplot2)
f <- function(x) sin(x) + cos(2*x)
ggplot(data.frame(x = range(0, 100) / 100 * 2 * pi), aes(x)) +
  geom_function(fun = f) +
  theme_classic()

Screenshot 2022-08-05 at 17 17 43

IMO it would be preferable to suppress printing of the last command, since it is very easy to add a cat()or print() whenever output is needed. Also, since many R functions return the value NULL the only way to get rid of the rather verbose NULL message above is to print something, even if it is not needed. But to exacerbate things this will then be printed twice with the current handling of output from R.

As an example, the code input

cat("Hello, world!\n")

results in the output

Hello, world!
<rpy2.rinterface_lib.sexp.NULLType object at 0x7f4bdce1ea40> [RTYPES.NILSXP]

This is because cat()returns the value NULL. So, instead we can use print, which returns the same string that is being printed, so that it will be printed twice. See below for input and output:

Input:

print("Hello, world!")

Output:

[1] "Hello, world!"
[1] "Hello, world!"

Perhaps there is s clever workaround that I haven't found. But if there isn't, it would be nice to either remove this automatic printing of the last command or at least suppress printing of NULL values.

novoselt commented 2 years ago

Thank you for a thorough list of examples! Supressing the last output would be against the behaviour with other languages and in general not desirable, but catching NULL should be doable. I'll look into it once I am back from the mountains ;-)

danm0nster commented 2 years ago

Thanks for the quick reply! Enjoy the mountains!

novoselt commented 2 years ago

OK, I've taken a closer look. Printing the output of the last command is definitely desirable and will continue. The fact that using print leads to double printing is a bit unfortunate, but would be rather tricky to fix correctly, so it will stay as well. As for handling NULL - I am a bit surprised that this is happening, but it has to be fixed in SageMath itself. I've created a ticket for that: https://trac.sagemath.org/ticket/34378#ticket