问题
I'm working on a markdown file I was sent that wants some of the script to be written in code chunks using the knitr package, which I've never used before.
The problem I'm running into is that the scripts don't appear to be working when placed in the chunks, but work just fine when I run them outside of that context.
Example:
sqlite <- dbDriver("SQLite")
con <- dbConnect(sqlite,"db/sqlite/table.db")
That works.
```{r db_connect}
sqlite <- dbDriver("SQLite")
con <- dbConnect(sqlite,"db/sqlite/table.db")
```
This does not work. Here is the traceback on the error:
Error in rsqlite_connect(dbname, loadable.extensions, flags, vfs) : Could
not connect to database: unable to open database file
8. stop(structure(list(message = "Could not connect to database:\nunable to open database file", call = rsqlite_connect(dbname, loadable.extensions, flags, vfs), cppstack = structure(list(file = "", line = -1L, stack = "C++ stack not available on this system"), .Names = c("file", ...
7. rsqlite_connect(dbname, loadable.extensions, flags, vfs)
6. initialize(value, ...)
5. initialize(value, ...)
4. new("SQLiteConnection", ptr = rsqlite_connect(dbname,loadable.extensions, flags, vfs), dbname = dbname, flags = flags, vfs = vfs, loadable.extensions = loadable.extensions, ref = new.env(parent = emptyenv()))
3. .local(drv, ...)
2. dbConnect(sqlite, "db/sqlite/table.db")
1. dbConnect(sqlite, "db/sqlite/table.db")
Does anyone have any recommendations on how to troubleshoot this?
回答1:
second test
%
\documentclass[a4paper]{article}
\title{test }
\begin{document}
<<db_create>>=
require("RSQLite")
sqlite <- dbDriver("SQLite")
db <- dbConnect(SQLite(), dbname="Test.sqlite")
dbSendQuery(conn = db,
"CREATE TABLE School
(SchID INTEGER,
Location TEXT,
Authority TEXT,
SchSize TEXT)")
dbListTables(con)
dbSendQuery(conn = db,
"INSERT INTO School
VALUES (1, 'urban', 'state', 'medium')")
dbSendQuery(conn = db,
"INSERT INTO School
VALUES (2, 'urban', 'independent', 'large')")
dbSendQuery(conn = db,
"INSERT INTO School
VALUES (3, 'rural', 'state', 'small')")
@
<<db_connect>>=
con <- dbConnect(RSQLite::SQLite(), "Test.sqlite")
dbListTables(con)
@
\end{document}
来源:https://stackoverflow.com/questions/47839559/running-code-chunks-in-r-knitr-package