Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to escape characters in SQL code in an R Markdown chunk?

$
0
0
```
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(odbc)
library(DBI)
library(dbplyr)
```

```{r SQL, connection=con, output.var="df"}
SELECT DB_Fruit.Pear, Store.Name, Cal.Year, Sales.Qty FROM DB_Fruit
```
#> Error: unexpected symbol in "SELECT DB_Fruit.Pear"

I'm attempting to run SQL code in an R Markdown chunk as shown above. I'm getting the "unexpected symbol" error shown above. My best guess is that I need to escape the underscore with something such as \_ or \\_ but neither of those makes my error go away.

If I instead query using DBI (shown below) I do not get any errors:

df <- dbGetQuery(con,'
  SELECT DB_Fruit.Pear, Store.Name, Cal.Year, Sales.Qty 
  FROM DB_Fruit
')

Maybe the dbGetQuery function is able to interpret things such as underscores _ correctly whereas the regular R Markdown parser can't? Or maybe there's blank spaces that have been copy/pasted as some weird unicode characters that again dbGetQuery function is able to interpret whereas the regular R Markdown parser can't?

What's the likely culprit and what do I do about it?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>