Giter Site home page Giter Site logo

Comments (7)

rempsyc avatar rempsyc commented on June 16, 2024

Interesting, thanks, will investigate. However, I think this is an issue from the side of afex, as just loading a package should not silently change behaviour for other packages. Sometimes functions are overwritten but a warning is then printed.

from rempsyc.

rempsyc avatar rempsyc commented on June 16, 2024

Ok, I'm indeed able to replicate this with a simple flextable and then saving class as nice_table. The interesting bit is that this issue only arises during interactive use, because the reprex makes it look like it works:

data <- mtcars[1:3, ]
table <- flextable::flextable(data)
class(table) <- c("nice_table", class(table))
table

suppressPackageStartupMessages(library(afex))

table

Created on 2024-02-22 with reprex v2.0.2

While on the interactive console it is actually outputing this:

> table
[1] header     body       footer     col_keys   caption    blanks     properties
<0 rows> (or 0-length row.names)

from rempsyc.

rempsyc avatar rempsyc commented on June 16, 2024

In afex, nice_table is a printing method for that class:

## S3 method for class 'nice_table'
print(x, ...)

It is defined as:

print.nice_table <- function(x, ...) {
  if(!is.null(heading <- attr(x, "heading"))) {
    cat(heading, sep = "\n")
  }
  print.data.frame(x)
  if(!is.null(attr(x, "sig_symbols"))) print_legend(x)
  if(!is.null(correction_method <- attr(x, "correction")) && 
     correction_method != "none") {
    cat("\nSphericity correction method:", correction_method, "\n")
  }
  invisible(x)
}

This calls print.data.frame(x) on the flextable object, which gives the output that you are seeing:

data <- mtcars[1:3, ]
table <- flextable::flextable(data)
class(table) <- c("nice_table", class(table))
table

print.data.frame(table)
#> [1] header     body       footer     col_keys   caption    blanks     properties
#> <0 rows> (or 0-length row.names)

Created on 2024-02-22 with reprex v2.0.2

But then it also prevents printing the flextable, with returning it invisibly with invisible(x) at the very end.

from rempsyc.

rempsyc avatar rempsyc commented on June 16, 2024

I have opened an issue in the afex repository: singmann/afex#126

from rempsyc.

rempsyc avatar rempsyc commented on June 16, 2024

I have posted a workaround on your stackoverflow question.

Essentially, use pkgload::unload instead of detach, which you can use within your script for the time being.

data <- mtcars[1:3, ]
table <- flextable::flextable(data)
class(table) <- c("nice_table", class(table))

suppressPackageStartupMessages(library(afex))

print(table)
#> [1] header     body       footer     col_keys   caption    blanks     properties
#> <0 rows> (or 0-length row.names)

pkgload::unload("afex")

table

Created on 2024-02-22 with reprex v2.0.2


The workaround of pkgload::unload works because, unlike detach, it detaches the whole namespace. You can just load afex again later in the script if you need to and repeat these steps as needed.

from rempsyc.

seglima avatar seglima commented on June 16, 2024

Thank you for looking into this and for finding an easy workaround!

from rempsyc.

rempsyc avatar rempsyc commented on June 16, 2024

In the development version of rempsyc, I have now removed the nice_table class completely, so tomorrow you should be able to install the new version from the r-Universe without having to use pkgload::unload in your script. I'd have to submit the new version to CRAN, eventually...

install.packages("rempsyc", repos = c(
  rempsyc = "https://rempsyc.r-universe.dev",
  CRAN = "https://cloud.r-project.org"))

from rempsyc.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.