Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you silently save an inspect object in R's tm package?

Tags:

r

inspect

tm

When I save the inspect() object in R's tm package it prints to screen. It does save the data that I want in the data.frame, but I have thousands of documents to analyze and the printing to screen is eating up my memory.

library(tm)
data("crude")
matrix <- TermDocumentMatrix(corpus,control=list(removePunctuation = TRUE,
                                             stopwords=TRUE))
out= data.frame(inspect(matrix))

I have tried every trick that I can think of. capture.output() changes the object (not the desired effect), as does sink(). dev.off() does not work. invisible() does nothing. suppressWarnings(), suppressMessages(), and try() unsurprisingly do nothing. There are no silent or quiet options in the inspect command.

The closest that I can get is

out= capture.output(inspect(matrix))
out= data.frame(out)

which notably does not give the same data.frame, but pretty easily could be if I need to go down this route. Any other (less hacky) suggestions would be helpful. Thanks.

Windows 7 64- bit R-3.0.1 tm package is the most recent version (0.5-9.1).

like image 458
Jim Crozier Avatar asked Mar 23 '23 11:03

Jim Crozier


1 Answers

Assign inside the capture then:

capture.output(out <- data.frame(inspect(matrix))) -> .null # discarding this

But really, inspect is for visual inspection, so maybe try

as.data.frame(as.matrix(matrix))

instead (btw matrix is a very unfortunate name for a variable, as that's a base function).

like image 105
eddi Avatar answered Apr 06 '23 01:04

eddi