-
-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default print method for SQL-chunks #307
Comments
The problem partially lies in
(I unescaped some HTML characters to make it easier to read.) The markdown parser used by remarkjs ignores markdown that appears inside There's also no (easy?) way around this; the wrapping # wrap html output in a div so special styling can be applied
if (is_html_output()) cat('<div class="knitsql-table">\n') @cderv could the sql engine be updated to turn off this feature? Or to possibly use the |
Oh yes we certainly need to do a special handling for using this engine in a xaringan format. By Currently the workaround is to define a custom method that will print correctly for xaringan format. ---
output:
xaringan::moon_reader: default
---
```{r include=FALSE}
library(DBI)
con <- dbConnect(RSQLite::SQLite(), dbname = ":memory:")
foo <- dbExecute(con, "DROP TABLE IF EXISTS packages")
foo <- dbExecute(con, "CREATE TABLE packages (id INTEGER, name TEXT)")
foo <- dbExecute(con, "INSERT INTO packages VALUES (1, 'readr'), (2, 'readxl'), (3, 'haven')")
# custom print method
custom_print <- function(x) {
paste(knitr::kable(x, format = "markdown"), collapse = "\n")
}
knitr::opts_knit$set(sql.print = custom_print)
```
```{sql connection=con}
SELECT * FROM packages
``` So maybe this should be fixed in xaringan directly by defining a setting |
Ah a custom |
Is there a way around that ? Not sure.
If we have a way to detect this is a xaringan format from within knitr we could not add this for this format also. That could be another solution : teach knitr about xaringan
maybe that is the cleanest way: modify knitr default behavior from within xaringan |
Thanks - you guys are awesome! The workaround provided by @cderv works perfectly. Is it possible to extend on this to get datatable ( |
Yes, almost certainly! Try removing the |
Not sure it will work as is @gadenbuie .
So Best way to output in any table format for now is to save into a variable and print in another chunk. ---
output:
xaringan::moon_reader: default
---
```{r include=FALSE}
library(DBI)
con <- dbConnect(RSQLite::SQLite(), dbname = ":memory:")
foo <- dbExecute(con, "DROP TABLE IF EXISTS packages")
foo <- dbExecute(con, "CREATE TABLE packages (id INTEGER, name TEXT)")
foo <- dbExecute(con, "INSERT INTO packages VALUES (1, 'readr'), (2, 'readxl'), (3, 'haven')")
# custom print method
custom_print <- function(x) {
paste(knitr::kable(x, format = "markdown"), collapse = "\n")
}
```
```{sql connection=con, output.var = "tab"}
SELECT * FROM packages
```
```{r, echo = FALSE}
DT::datatable(tab)
``` |
I instinctively reached for that as my first attempt as well, but it throws an error as pointed out by @cderv.
That's the solution I was using before creating the issue, but it felt kind of clunky. Datatable was more of a "nice to have", so I'm more than happy with the custom print-function you provided for the time being. Thank you both again. Your quick response solved what I had been spending a hours searching and debugging. I was about |
Should be fixed with the development versions of xaringan and knitr now: remotes::install_github(c('yihui/knitr', 'yihui/xaringan')) The |
Christof Schötz (2): bug fix in group_indices() (#2097) fix bug in Sweave2knitr when removing unnecessary chunk ends (#2106) Christophe Dervieux (2): Add a newline on empty string in `comment_out(newline = TRUE)` (#2096) Update GHA workflow (#2110) Kenneth C. Arnold (1): Give a warning if include_graphics() is given a pathname starting a tilde (#2063) Konrad Siek (1): Engine for ditaa text diagrams (#2092) Lionel Henry (1): Pass user-supplied calling handlers to `evaluate()` (#2079) Xianying Tan (1): add rnw2pdf(), an opinionated wrapper to convert Rnw to PDF (#2115) Yihui Xie (27): use xfun::read_all() to read more than one file for the chunk option `file` just use format() directly and let S3 dispatch it fix #2086: apply partition_chunk() after the code has been processed to remove the possible indentation and > Add a generic engine for executing a command (#2073) simplify the code that determines the plot file extension from the dev and fig.ext for the `exec` engine, allow the full path of the command to be specified in the chunk option engine.path, just like other command-based engines make the chunk option `lang` available to all engines language -> lang no longer modify the `engine` option for syntax highlighting, but set the `lang` option instead lowercase Rscript add a engine eng_plot() as a helper function to generate a plot from a command, and apply this function to the dot/asy engines update language name inside eng2lang() instead close #2100: bump the minimal required version of R from 3.2.3 (2015-12-10) to 3.3.0 (2016-05-03) lowercase engine name set the language early in eng_dot() for the asy engine; otherwise eng_exec() will set the language to `asy` later set the correct language name in eng_exec(), e.g., when the command is `Rscript`, `lang` should be `r` merge eng_dot() into eng_plot() close #2105: clarify the description of the knitr package written in 2011, without assuming that users know Sweave forgot to add () in tests in #2099 factor out create_fence() so that it can be reused for both ``` and ::: blocks (#2099) fix yihui/xaringan#307: don't add a div around the sql table output use a more general option knitr.sql.html_div instead of detecting a specific xaringan output format, so that users can set this option to FALSE to disable the div let the $set() method return old values instead of NULL factor out a function in_input_dir() fix #2081: use the input directory (or root.dir, if specified) as the working directory to evaluate chunk options close #2061: add fig.scap to opts_knit$get('eval.after') CRAN release v1.38 Zhian N. Kamvar (1): document and export partition_chunk() (#2083) atusy (1): Implement class.chunk and attr.chunk option to wrap entire chunk in Pandoc's fenced div (closes #2000) (#2099)
Thanks you so much for this awesome package. It's a fantastic format for presentations. I am currently making some training material for SQL and discovered that the standard formatting of SQL-results appear to be using
kable
without line breaks for formatting results.Given the following
Rmd
-file:The result looks like this:
I have spent some time investigating how this could be overridden, but have come up short. I suspect that there may be some chunk options I have missed.
Session info
The text was updated successfully, but these errors were encountered: