0

我正在处理一个我收到的降价文件,它希望使用我以前从未使用过的 knitr 包将一些脚本编写成代码块。

我遇到的问题是脚本在放置在块中时似乎没有工作,但是当我在该上下文之外运行它们时工作得很好。

例子:

sqlite <- dbDriver("SQLite")
con <- dbConnect(sqlite,"db/sqlite/table.db")

这样可行。

```{r db_connect}
sqlite <- dbDriver("SQLite")
con <- dbConnect(sqlite,"db/sqlite/table.db")
```

这不起作用。这是错误的回溯:

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")

有人对如何解决此问题有任何建议吗?

4

1 回答 1

0

第二次测试

% 
\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}
于 2017-12-15T20:57:52.377 回答