To run Rmd chunks, RStudio seems to use knitr's code internally in tools:rstudio
environment. Curiously, it won't be updated even when I install the development version of knitr... Anyone knows when and where RStudio captures the knitr's code?
Some contexts: I tried to fix this issue, but the warning won't go away even when I install the patched version of knitr.
opened 11:50PM - 15 Nov 18 UTC
closed 10:37PM - 06 Dec 18 UTC
Knitr appropriately handles chunks using the `sql` engine for statements (i.e. I… NSERT, UPDATE, CREATE, etc.) differently from how it handles chunks making queries (i.e. SELECT): it does not call `dbFetch`. In RStudio however, when you click "Run" (or the play button on a specific sql chunk), you get ugly results and a warning:
```
In result_fetch(res@ptr, n = n) :
Don't need to call dbFetch() for statements, only for queries
```
The following Rmd is a reproduces the warning, if you run all the chunks rather than knit.
<pre><code>
---
editor_options:
chunk_output_type: inline
---
```{r}
library(RSQLite)
conn <- dbConnect(SQLite(), ":memory:")
```
```{sql, connection = conn}
CREATE TABLE test (
id integer
)
```
```{sql, connection = conn}
SELECT * FROM test
```
```{r}
dbDisconnect(conn)
```
</code></pre>
My system information:
```
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RSQLite_2.1.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 digest_0.6.18 rprojroot_1.3-2 DBI_1.0.0 backports_1.1.2
[6] evaluate_0.12 blob_1.1.1 rmarkdown_1.10 tools_3.5.0 bit64_0.9-7
[11] bit_1.1-14 yaml_2.2.0 compiler_3.5.0 pkgconfig_2.0.2 memoise_1.1.0
[16] htmltools_0.3.6 knitr_1.20
```
Here's steps to reproduce:
Install the patched branch.
devtools::install_github("yutannihilation/knitr@test-notebook-sql-chunk")
Close and relaunch RStudio to make sure to have a fresh R session.
Open a new R Markdown file, and copy and paste this Rmd codes .
Run chunks by ▷ ("Run Current Chunk") buttons on the top right of the chunks.
Then, I think you'll see a warning which is supposed to be fixed at my branch...
@yutannihilation
this is what you need
```{sql , connection = conn, output.var= result}
select * from test
```
```{r}
head(result)
```
Another Tips is that the short-cut can be configured in RStudio, like ctrl
+ alt
+ S
, means insert a SQL chunk.
Thanks, but I think it's not what I need; I want to fix RStudio, not to fix the Rmd codes, which is supposed to be executed without warnings...
RStudio uses its own internal routine when running SQL chunks interactively, as defined here:
pages = options[["pages.print"]]
)
list(
columns = columns,
data = if (length(data) == 0) list() else data,
options = pagedTableOptions
)
})
.rs.addFunction("runSqlForDataCapture", function(sql, outputFile, options)
{
is_sql_update_query <- function(query) {
# remove line comments
query <- gsub("^\\s*--.*\n", "", query)
# remove multi-line comments
if (grepl("^\\s*\\/\\*.*", query)) {
query <- gsub(".*\\*\\/", "", query)
}
so unfortunately, updating knitr
alone won't fix the issue as seen within RStudio. We'd need to patch our own routine as well.
2 Likes
Thanks, that's what I wanted to know! I didn't expect the knitr's source code is copied into the RStudio's source code...
(Disclaimer: I'm not an expert of OSS licenses.)
I'm happy to send a PR to fix this, but I feel a bit uneasy that there seems no credit to the original author of knitr in RStudio's source code. Are there any agreement between Yihui and RStudio to grant these lines of codes? Or, am I missing something?
Fortunately, knitr's license is GPL (GPLv2/GPLv3) and RStudio's license is AGPLv3, and GPLv3 and AGPLv3 can be considered compatible, so I believe there would be no actual problem. To be clear, I don't have a strong opinion on this and just want to avoid confusion.
In this case, members of RStudio were authors of (at least, the initial) implementation of the SQL engine:
yihui:master
← javierluraschi:feature/sql_eng
opened 09:17PM - 12 Jul 16 UTC
This PR adds a SQL engine for knitr to allow chunks based on DBI that look like:…
```
``{r}
library(DBI)
conn <- dbConnect(RSQLite::SQLite(), dbname = "sql.sqlite")
number <- 42
``
``{sql output, conn = conn}
SELECT ?number
``
``{r}
output
``
## 42
## 1 42
```
This change comes from integrating https://github.com/jcheng5/knitsql adding a sample `.Rmd` file and removing the `yaml` configuration feature.
- @jjallaire @jcheng5
I imagine we inlined this code into RStudio to help prevent any possible issues that could occur if knitr
were to update and break some of the assumptions made in our internal notebook implementation.
Ah, thanks for the context. It makes sense.
to help prevent any possible issues that could occur if knitr
were to update and break some of the assumptions
I understand the motivation and feel it's valid. Yet, I hope someday we'll find some nice way to keep the assumptions without inlining
1 Like
system
Closed
December 11, 2018, 11:26pm
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.