在R中运行代码块(knitr包)

时间:2017-12-15 20:37:49

标签: r knitr knitrbootstrap knitr-spin

我正在处理我发送的markdown文件,该文件希望使用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")

有没有人就如何解决此问题提出任何建议?

1 个答案:

答案 0 :(得分: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}