Suppressing unwanted R output

13 views
Skip to first unread message

Randall Pruim

unread,
Jun 13, 2023, 12:22:22 PM6/13/23
to sage-cell
The tail end of output from an R cell often includes things I am not expected and do not want.  I'm wondering (a) if this is a but, and (b) if there is any work-around.

Here are some examples.

x <- LETTERS[1:6]
cat(x)

produces

A B C D E F<rpy2.rinterface_lib.sexp.NULLType object at 0x7f75c8264b80> [RTYPES.NILSXP]

print(x)

produces

[1] "A" "B" "C" "D" "E" "F"
[1] "A" "B" "C" "D" "E" "F"

and

dput(x)

produces

c("A", "B", "C", "D", "E", "F")
[1] "A" "B" "C" "D" "E" "F"

Is there any way for me to have complete control over what is displayed, with no trailing stuff?

For the curious, I'm exploring ways to use the sagecellserver within MyOpenMath to both generate and grade randomized questions.

Thanks in advance for any advice/suggestions.

---rjp

Randall Pruim

unread,
Jun 13, 2023, 12:24:10 PM6/13/23
to sage-cell
Yikes, that first line got off to a bad start.  Let's try that again:

The tail end of output from an R cell often includes things I am not expecting and do not want.  I'm wondering (a) if this is a bug, and (b) if there is any work-around.

Andrey Novoseltsev

unread,
Jun 13, 2023, 1:15:44 PM6/13/23
to sage-cell
This is the way how R interface in SageMath itself functions, it has been this way for years after switching from pexpect interface to library calls. It does not seem like anybody is willing to work on changing it.

I am however very curious about workarounds. Has anyone come up with any? Since I don't use/know R, it is difficult for me do figure out.

Best,
Andrey

Randall Pruim

unread,
Jun 13, 2023, 1:39:54 PM6/13/23
to sage-cell
Thanks for the info.

A little more experimenting has led me to a work-around.  What is needed for my work-around is an object with a print method that prints nothing and returns invisibly.  I created one as a test of the theory.

x <- LETTERS[1:6]
print.nothing <- function(x, ...) {
    }  
nothing <- function() {
    structure("", class = 'nothing')
    }
dput(x)
nothing()

produces

c("A", "B", "C", "D", "E", "F")

as desired.

It also suffices if the last object in your R code has a desirable print method that returns invisibly.  So for example

table(x)

produces the desired output

x
A B C D E F
1 1 1 1 1 1

with nothing extraneous.
Reply all
Reply to author
Forward
0 new messages