在R中运行代码块(编织程序包) [英] Running Code Chunks in R (knitr package)

查看:137
本文介绍了在R中运行代码块(编织程序包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个Markdown文件,该文件希望使用knitr包将某些脚本写入代码块中,这是我以前从未使用过的.

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.

示例:

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

那行得通.

```{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?

推荐答案

第二次测试

% 
\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}

这篇关于在R中运行代码块(编织程序包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆