Git Product home page Git Product logo

Comments (9)

markfairbanks avatar markfairbanks commented on August 20, 2024 2

I hadn't seen that actually. I gave it a try and it didn't work unfortunately.

It seems to mimic this process of creating a setup chunk and setting global chunk options:

```{r setup}
knitr::opts_chunk$set(paged.print=TRUE)

from tidytable.

TysonStanley avatar TysonStanley commented on August 20, 2024 1

That's interesting. I thought for sure knitr would check for that and print them with the same formatting. I'll try to read up on it over the next few days and see what I can find.

from tidytable.

TysonStanley avatar TysonStanley commented on August 20, 2024

Interesting. What does the error look like?

from tidytable.

markfairbanks avatar markfairbanks commented on August 20, 2024

No error, but it prints differently. If you open an R Notebook and paste in this code you'll see that the data.table returns a "paged print" that allows you to scroll through the data, and the tidytable does a normal console print.

library(tidytable)
library(data.table)

data.table(x = 1:50, y = 1:50) # Returns a paged.print

tidytable(x = 1:50, y = 1:50) # Returns a console print

In Rmarkdown paged.print = TRUE is the default. When set to FALSE it returns a console print. So the tidytable should (hypothetically) return a paged.print in this situation.

I think I have to somehow register an S3 method for tidytable with the knitr or rmarkdown print, but I can't seem to figure out how.

Here are a couple pages I've found that seem to touch on the topic:
https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html#for-package-authors
https://community.rstudio.com/t/customizing-general-objects-formatting-output/11262

from tidytable.

TysonStanley avatar TysonStanley commented on August 20, 2024

Oh, yes. I've seen this with other output. Does the tidytable class have a second class (like data.table or data.frame)?

from tidytable.

markfairbanks avatar markfairbanks commented on August 20, 2024

Yep - there's 3 classes: c("tidytable", "data.table", "data.frame")

from tidytable.

markfairbanks avatar markfairbanks commented on August 20, 2024

So I don't know if this is the "correct" way, but it seems like inside print.tidytable there should be a way to do something like this:

if ("knitr" %in% installed.packages()) {
  if (knitr::opts_chunk$get()$paged.print) {
    rmarkdown::paged_table(x)
  }
}

But I can't seem to get it to work. Not sure if you have any ideas on that one.

This issue shows that explicitly calling print() overrides paged.print = TRUE. So the "if" statement shouldn't be necessary, but I haven't been able to figure out another way

from tidytable.

TysonStanley avatar TysonStanley commented on August 20, 2024

You probably saw this but wanted to check. Have you put those arguments in the Rmarkdown yet and is it still printing wrong?

from tidytable.

markfairbanks avatar markfairbanks commented on August 20, 2024

Found a workaround! Basically - if knitr is installed and paged.print = TRUE, make the class of the object c("data.table", "tidytable", "data.frame"). If not make the class c("tidytable", "data.table", "data.frame").

That makes it page-print like a data.table, but console print like a tidytable.

There's probably a better way, but this will work for now until we can figure out a better solution.

@TysonStanley Thanks for taking a look!

as_tidytable <- function(x) {
  UseMethod("as_tidytable")
}

#' @export
as_tidytable.tidytable <- function(x) {
    x
}

#' @export
as_tidytable.default <- function(x) {
  if (is.data.table(x)) x <- add_class(x)
  else x <- add_class(as.data.table(x))
  x
}

add_class <- function(.data) {
  if (knitr_installed) {
    if (!knitr::opts_chunk$get()$paged.print %||% TRUE) {
      class(.data) <- c("tidytable", "data.table", "data.frame")
    } else {
      class(.data) <- c("data.table", "tidytable", "data.frame")
    }
  }
  .data
}

from tidytable.

Related Issues (20)

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.