Git Product home page Git Product logo

Comments (30)

cschwem2er avatar cschwem2er commented on July 29, 2024 1

okay, will do :)

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

So what would this method do that is easier than the current approach of printing tables in knitr documents?

from sjplot.

artemklevtsov avatar artemklevtsov commented on July 29, 2024

Now it's look like this:

```{r}
cat(sjt.frq(..., no.output = TRUE)$knitr)
```

vs knit_print method:

```{r}
sjt.frq(...)
```

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

I think I don't know how generic methods can be implemented. I don't get how your example should or could work...

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

I'll keep this issue opened, when I find some time I may try to figure out how to implement a generic print function.

from sjplot.

jemus42 avatar jemus42 commented on July 29, 2024

Any news on this? I'll be teaching first year students how to report with R/RMarkdown, and I'm not looking forward to explaining them why we need to manually extract the knitr-output from the function :/

Also, I remember reading an example of a function being called inside an RMarkdown document being able to tell what the target output is and print appropriately, but I can't find that example anymore.

In principle the function would need to be able to tell it's inside a knitr-context, suppress the viewer output and print the appropriate output, but I also don't know how to achieve this, since I never even built any kind of method :/

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Put simple: the sjt-functions produce HTML output, which cannot be easily integrated into markdown.

I'm not using knitr on a regular basis, thus I'm not sure how knit_print works, or how to integrate my functions in a more convenient way into knitr. Any hints appreciated :-)

from sjplot.

jemus42 avatar jemus42 commented on July 29, 2024

I'm confused, as far as I know markdown handles plain HTML exactly as that, and just incorporates it. Thereby, at least for html_output in RMarkdown, this should not be an issue, or am I confusing something?

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Yes, it was a bit confusing. I have two use two different HTML outputs: One for the viewer / browser, and another formatted table for knitr-integration. That's why no.output = TRUE)$knitr) is needed to distinguish between knitr-documents and browser view.

from sjplot.

jemus42 avatar jemus42 commented on July 29, 2024

Is it possible to detect wether the function is being called from within knitr, so that no.output=F and use.viewer=F can be set automatically, and the return value set to the $knitr bit?
I can't think of any other way right now, so I really hope someone shows up who knows how knitr works, because right now the lacking knitr integration is my biggest problem with sjPlot :/

from sjplot.

artemklevtsov avatar artemklevtsov commented on July 29, 2024

As variant you could attach class attribute to the sjPlot output and use something like this:

knit_print.sjTable <- function(x, ...) {
    asis_output(x$knitr)
}

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

I have added a sjTable class attribute to some of the table functions (like sjt.frq), so I'd be happy if someone would create a pull request to get on with this issue.

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Since this issue is "only" for convenience and I'm not sure how to implement it as native knitr method, I'll close this issue. If someone can help, please create a pull request.

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

I contacted the Cologne R User group where one of the members is author of the googleVis package. Maybe they have an idea how to add native print methods here, or @artemklevtsov has a solution. I'm in the same situation as @jemus42 and would really like to avoid explaining students the manual approach.

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

Here is a related stackoverflow question:
http://stackoverflow.com/questions/41172411/add-native-knitr-print-methods
One of the comments is by the creator of knitr, so maybe this is useful

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

All sjt-function are of class sjTable and return a $knitr-object with inline-CSS. Maybe you could file a pull request that addresses this issue?

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

It will take a while for me to understand the code, but I will see what I can do :)

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Actually, you don't need to understand much of the sjPlot-package, it's more about knowing knitr and S3-methods. And, everyone is invited to submit a pull-request, it's not about shoving the work just to you ;-)

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

After a lot of unsuccesful attempts I think I finally found a solution. Basically two things are necessary:

  • a custom print function for knitr, e.g.
knit_print.sjTable <-  function(input, ...){
  knitr::asis_output(input$knitr)
}
  • the class of corresponding object has to be visible, e.g. for sjt.frq() the last lines have to be adjusted to
  structure(class = c("sjTable", "sjtfrq"), # I removed invisible() here
                      list(page.style = page.style,
                           page.content.list = page.content.list,
                           output.complete = toWrite,
                           knitr = knitr))

If you find the time please have a look the attached markdown file.
knitr_sjplot.zip

It looks like a very easy solution to me. I guess you'd only have to include the print function somewhere in sjPlot and make classes visible right? What do you think?

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

I guess this would mean that in regular R use, the complete HTML and CSS data would be printed to console, unless I also add an S3-print-method. What would this print-method do? Show the table in the browser or ViewerPane of the IDE? And what, if a sjt-function is used inside knitr? Would this print the table to knitr and show the table inside the ViewerPane?

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

What would this print-method do? Show the table in the browser or ViewerPane of the IDE?

I would say so. Its the same as calling shiny from the console, which, like sjt.frq() is not made for returning console output and thus opens its html stuff in the browser / viewer.

And what, if a sjt-function is used inside knitr? Would this print the table to knitr and show the table inside the ViewerPane?

I'm not 100% sure whether I understand you correctly, but If you try to render the file in the example above with knitr, it should do both, e.g. insert the html table in the document and also show it inside the viewerpane.

Btw: I used the knitr print vignette for the code which also contains two notes for package authors:

  • knit_print() is an S3 generic function in knitr, so in theory you need to import it in your namespace via importFrom(knitr, knit_print), but due to the “lack of rigor” of the S3 system, you can choose not to import knitr, and just do export(knit_print.foo) in the namespace, then R will find the S3 “method” after your package is attached via library(), because it is essentially a normal R function;
  • asis_output() is simply a function that marks an object with the class knit_asis, and you do not have to import this function to your package, either – just let your print method return structure(x, class = 'knit_asis'), and if there are additional metadata, just put it in the knit_meta attribute; here is the source code of this function:
knitr::asis_output
## function (x, meta = NULL, cacheable = NA) 
## {
##     structure(x, class = "knit_asis", knit_meta = meta, knit_cacheable = cacheable)
## }
## <environment: namespace:knitr>

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Ok, could you check, if sjt.frq() works so far inside knitr now?

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

If it works, all sjt-functions need to be modified, returning this now:

  structure(
    class = c("sjTable", "sjtfrq"),
    list(
      page.style = page.style,
      page.content.list = page.content.list,
      output.complete = toWrite,
      knitr = knitr,
      file = file,
      show = !no.output,
      use.viewer = use.viewer
    )
  )

and some of the vignettes need to be changed... I would be happy for any pull-requests here. :-)

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

cool! it works :)
not for rnotebook previews yet, but I'm pretty sure that is a problem on their end, for which I already opened an issue.

So what you would need is just an adjustment of the structure() call for every sjt function? And about the vignettes: Is it sufficient to change the .Rmd files, like sjtbasic.Rmd?

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Yes the Return value (insible(structure(...))) should be replaced by the above Code snippet, and only the .rmd Files need to be changed.

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

I'm currently trying do adjust these functions / vignettes and while everything works for correlation tables I experience several issues. Some functions do not work with the parameters from sjt.frq(). For example sjTabItemAnalysis / sjTabMannWhitney don't work with page.content.list. Unfortunately I'm not very familiar with all the html/css related functions in sjPlot and all attempts to adjust the functions failed so far. I'm afraid you'd still have to check this :/

I have everything in the branch https://github.com/methodds/devel/tree/knitr-changes, but don't think I should file a PR to the main branch, as some functions are broken. If you want you could open another branch and I PR it to that one?

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

Sorry, page.content.list is for sjt.frq() only (because if you use a df as argument, a table for each variable is returned), all other methods return just page.content. You should revise the S3-print-method accordingly, and use

  structure(
    class = c("sjTable", "sjtfrq"),
    list(
      page.style = page.style,
      page.content = page.content,
      # for sjt.frq() probably this one:
      # page.content = page.content.list[[1]],
      output.complete = toWrite,
      knitr = knitr,
      file = file,
      show = !no.output,
      use.viewer = use.viewer
    )
  )

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

And you can commit in master, PR can be reviewed and edited.

from sjplot.

cschwem2er avatar cschwem2er commented on July 29, 2024

Thanks, this was helpful. What about irregularities in other parameters? TabGrpmean and TabLinReg do not seem to have code for page.style. TabLinReg accepts

page.style = get_table_css_styles(cell.spacing, 
cell.gpr.indent,
 p.numeric, 
show.header, CSS)

there, but TabGrpmean does not:

Error in sprintf("border-bottom: 1px solid; padding:%.1fcm;", cell.spacing) : 
  object 'cell.spacing' not found 

Edit: sjTabItemAnalysis also throws an error for page.style

from sjplot.

sjPlot avatar sjPlot commented on July 29, 2024

TabGrpMean has page.style, see here: https://github.com/sjPlot/devel/blob/master/R/sjTabGrpmean.R#L204
Same for TabItemanalysis

For sjt.lm(), sjt.glm(), sjt.lmer() and sjt.glmer() is used get_table_css_styles() to re-use code - else, I would have copied this code 4 times, because the CSS is identical for all these HTML-tables.

from sjplot.

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.