Skip to content

summary.corpus implementation not suppressing output when assigned to a variable #926

Closed

Description

@myusrn

Elsewhere in R when i assign summary(some object) to a variable as shown below with mtcars data object it suppresses any output the summary() call would have normally produced. I'm finding that summary(quanteda::corpus object) isn't doing that and not sure if its by design or not.

Where it was an issue for me is in R markdown, *.Rmd, work where i wanted to assign summary(quanteda:corpus object) to a variable so i could then output just pieces of summary of interest vs the more verbose output. Tried using invisible() wrapper with no change in results.

Here is a simple repro of what i'm seeing . . .

> data(mtcars); class(mtcars)
[1] "data.frame"
> mtcarsSummary <- summary(mtcars)
> corpusObject <- quanteda::corpus("some corpus text of no consequence"); class(corpusObject)
[1] "corpus" "list"  
> invisible(corpusSummary <- summary(corpusObject))
Corpus consisting of 1 document.

  Text Types Tokens Sentences
 text1     6      6         1

Source:  D:/src/repos/dsJhuC10/* on x86-64 by ob1
Created: Sat Aug 12 11:43:57 2017
Notes:   

Warning message:
In nsentence.character(object, ...) :
<snip />

Activity

  1. lgreski commented on Aug 12, 2017

    @lgreski

    Hello Ken. Adding to myusrn's point, I wrote a test R Markdown report to see whether I could completely suppress quanteda messages in library() and other knitr code chunks. The results are posted on rpubs at Suppressing quanteda Output. summary() suppresses the output if verbose=FALSE is directly listed as an argument to summary(), but not with the quanteda_options() function.

    regards,

    Len Greski

  2. kbenoit commented on Aug 13, 2017

    @kbenoit
    Collaborator

    That's actually intentional at the moment, since we wanted the default for summary.corpus() to produce printed output, not to return a data.frame and invoke the data.frame print method (if not captured). For, most of the functions, users will want the

    quanteda_options("verbose")
    # [1] FALSE

    except for summary.corpus().

    However this is slightly inconsistent with the method for summary.data.frame(), which returns a table, whereas summary.corpus() returns a data.frame.

    So, the immediate solution to not printing the output here is simply to specify

    corpusSummary <- summary(corpusObject, verbose = FALSE)

    Advice on how you'd prefer to see this function work is welcome!

  3. myusrn commented on Aug 13, 2017

    @myusrn
    Author

    Can summary.corpus() implementation easily determine when its being called in the context of a variable assignment and when it is not?

    If so then given what i see with other summary implementations, summary.numeric/character/data.table(), is that they output data when no variable assignment is happening and suppress it when a variable assignment is happening.

    That would be my preferred default user experience with summary.corpus(), optionally overrided with verbose = TRUE param, if its doable without having to introduce significant complexity.

  4. lgreski commented on Aug 13, 2017

    @lgreski

    Thanks for the follow up, Ken. Generally speaking, I like the verbose output. However, if I am including quanteda functions in an R Markdown report, I'd like to:

    1. Suppress all warnings / messages with a global quanteda parameter (i.e. have quanteda_options() work consistently across all quanteda functions), and
    2. Use library() options to prevent quanteda startup messages from being written to the output.

    regards,

    Len

  5. myusrn commented on Aug 13, 2017

    @myusrn
    Author

    @len, don't the knitr::opts_chunk$set(..., message = FALSE, warning = FALSE) settings address your concerns over messages and warnings in r markdown output with quanteda library load and function calls like it does for other libraries?

    {r knitr-global-options, include=FALSE}
    knitr::opts_chunk$set(cache.comments = FALSE, comment = FALSE, message = FALSE, warning = FALSE)
    
  6. lgreski commented on Aug 15, 2017

    @lgreski

    @myusrn Apparently quanteda does not obey all of these settings, as I demonstrated in my Rpubs test. The code block where I load quanteda includes:

    knitr::opts_chunk$set(echo = FALSE,warning = FALSE,messsage=FALSE,comment=NA)
    library(readr,quietly=TRUE)
    library(dtplyr,quietly=TRUE)
    library(stringr,quietly=TRUE)
    library(stringi,quietly=TRUE)
    library(quanteda,quietly=TRUE,verbose=FALSE,warn.conflicts=FALSE)
    

    The library() function arguments should suppress messages from quanteda, but it still writes its load message (including X of Y cores for parallel processing) to the output file.

    image

    regards,

    Len

  7. kbenoit commented on Aug 15, 2017

    @kbenoit
    Collaborator

    Thanks @lgreski and @myusrn these are good points and should be straightforward to fix. I'll get to them in a few days.

    On summary.corpus() I think the simplest solution is to remove the verbose argument altogether, and make it return an additionally classed data.frame that has a slightly modified print method.

  8. kbenoit commented on Sep 7, 2017

    @kbenoit
    Collaborator

    I’ve experimented now and done some research, and it turns out that the quanteda startup message behaviour is entirely consistent with other packages, e.g.:

    library("data.table", quietly = TRUE, warn.conflicts = FALSE, verbose = FALSE)
    ## data.table 1.10.4
    ##   The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
    ##   Documentation: ?data.table, example(data.table) and browseVignettes("data.table")
    ##   Release notes, videos and slides: http://r-datatable.com

    The library() arguments are very strangely designed, but hey that’s not our doing.

    If you attach the package normally, you will see the message, plus the namespace conflict warning about View, and the startup messages:

    library(“quanteda")
    ## quanteda version 0.99
    ## Using 4 of 8 threads for parallel computing
    ##
    ## Attaching package: ‘quanteda’
    ##
    ## The following object is masked from ‘package:utils’:
    ##
    ##     View

    If you do not want the namespace conflict warnings, use this:

    detach("package:quanteda")
    library(“quanteda", warn.conflicts = FALSE)
    ## quanteda version 0.99
    ## Using 4 of 8 threads for parallel computing

    If you do not want any output at all, use this (in your RMarkdown file for instance:

    detach("package:quanteda")
    suppressPackageStartupMessages(library("quanteda”))
  9. lgreski commented on Sep 8, 2017

    @lgreski

    Thanks for the explanation, Ken.

    Len

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions