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
sql chunk handles statements well with "Knit" but poorly with "Run" #1637
Comments
|
Thanks, confirmed.
Unfortunately, it seems not. I guess it's just the warnings are not captured on knitting because it doesn't come from the chunks, but from knitr itself. The problem is here: Lines 550 to 558 in dc5ead7
This probably should be something like below. if (is_sql_update_query(query)) {
data = DBI::dbExecute(conn, query)
} else if (is.null(varname) && max.print > 0) {
res = DBI::dbSendQuery(conn, query)
data = DBI::dbFetch(res, n = max.print)
DBI::dbClearResult(res)
} else {
data = DBI::dbGetQuery(conn, query)
} |
|
@yutannihilation I'm not an expert here, so please go ahead and push the fix to the master branch. No need to create a PR. Thanks! |
|
Sure. |
|
@yihui I've modified the code and installed locally, but it seems RStudio still uses the unmodified version. Do you know when and where RStudio captures knitr's codes? This is what I see with |
|
Interesting. Did you restart R? |
|
Yes, I even restarted RStudio, but no luck... |
|
That's weird. I cannot reproduce your issue. While applying your patch, I found I needed to patch the next diff --git a/R/engine.R b/R/engine.R
index a6d1bd25..b57b760e 100644
--- a/R/engine.R
+++ b/R/engine.R
@@ -549,7 +549,9 @@ eng_sql = function(options) {
# execute query -- when we are printing with an enforced max.print we
# use dbFetch so as to only pull down the required number of records
- if (is.null(varname) && max.print > 0 && !is_sql_update_query(query)) {
+ if (is_sql_update_query(query)) {
+ data = DBI::dbExecute(conn, query)
+ } else if (is.null(varname) && max.print > 0) {
res = DBI::dbSendQuery(conn, query)
data = DBI::dbFetch(res, n = max.print)
DBI::dbClearResult(res)
@@ -558,7 +560,7 @@ eng_sql = function(options) {
}
# create output if needed (we have data and we aren't assigning it to a variable)
- output = if (!is.null(data) && ncol(data) > 0 && is.null(varname)) capture.output({
+ output = if (length(dim(data)) == 2 && is.null(varname)) capture.output({
# apply max.print to data
display_data = if (max.print == -1) data else head(data, n = max.print)This is because |
Really?? I cannot avoid reproducing this issue.... Here's what I did:
devtools::install_github("yutannihilation/knitr@test-notebook-sql-chunk")
And I still see the warning...:
Thanks for pointing out! I got the error when I pushed knit button. |
|
TIL: https://community.rstudio.com/t/where-is-knitr-used-by-rstudio/19244/4 |
|
Hmm. I didn't know that. So we'll have to fix both knitr and RStudio? If the patch above looks good to you, please feel free to push it to the master branch (I can do it, too). Thanks! |
|
Sure, it looks good to me. Will do. |
|
Done! I'll send a PR to RStudio as well. May I keep this open until it's merged? |
|
rstudio/rstudio#3991 is merged. So, this should be fixed in the next release of RStudio. |
|
Great. Thanks again! |
…sql (yihui#1642) Continue the fix for yihui#1637
|
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary. |


Knitr appropriately handles chunks using the
sqlengine for statements (i.e. INSERT, UPDATE, CREATE, etc.) differently from how it handles chunks making queries (i.e. SELECT): it does not calldbFetch. In RStudio however, when you click "Run" (or the play button on a specific sql chunk), you get ugly results and a warning:The following Rmd is a reproduces the warning, if you run all the chunks rather than knit.
My system information:
The text was updated successfully, but these errors were encountered: