knitr错误:数据库连接已关闭

时间:2014-02-26 16:56:17

标签: r knitr r-markdown

我正在使用knitr在R Studio中生成markdown。我正在使用巴西PNAD,我的数据库是SQLite,大小是967MB。报告的一部分是提出问题,它有一条消息:## Error: Database connection is closed.在RStudio中我没有脚本问题,问题只是报告。

示例:

library(knitr)
opts_chunk$set(cache=TRUE)


```{r echo=FALSE, results='hide', message=FALSE}

rm(list=ls(all=TRUE))

if(Sys.info()[1]=="Linux") {setwd("~/Dropbox/Rotinas monografias/2014_Thaina")} else
                      {setwd( "/Users/sfleite/Dropbox/Rotinas monografias/2014_Thaina")}

pnad2012.dbname <- "~/Dropbox/Microdados/2012pnad.db"

sqlite    <- dbDriver("SQLite") 
pnad2012 <- dbConnect(sqlite,"~/Dropbox/Microdados/2012pnad.db")  
dbListTables(pnad2012) 
```
```{r}

##############################################
# survey design for a database-backed object #
##############################################

# create survey design object with PNAD design information
# using existing table of PNAD data
sample.pnad <-
svydesign(
      id = ~v4618 ,
      strata = ~v4617 ,
      data = paste0( "monografiathaina" ) ,
      weights = ~pre_wgt ,
      nest = TRUE ,
      dbtype = "SQLite" ,
      dbname = "~/Dropbox/Microdados/2012pnad.db"
      )
y <-
pnad.postStratify(
          design = sample.pnad ,
          strata.col = 'v4609' ,
          oldwgt = 'pre_wgt'
          )
```

```{r}
####################################
# female analysis of Brazil #
####################################

# restrict the y object to females only

y.female <- subset (y, v0302==4)
```
```{r}
y.empresaria <- subset( y.female , empresaria == 1 )
```
```{r}
y.femalemono <- subset( y , famonofe == 1)
```
```{r}
y.empresariamono <- subset( y.femalemono,  empresaria == 1 )
``` 

问题:

y.female <- subset(y, v0302 == 4)
## Error: Database connection is closed
y.empresaria <- subset(y.female, empresaria == 1)
## Error: objeto 'y.female' não encontrado
y.femalemono <- subset(y, famonofe == 1)
## Error: Database connection is closed

但是,最后还是可以的:

y.empresariamono <- subset(y.femalemono, empresaria == 1)
## Warning: 269 strata have only one PSU in this subset.

有没有办法解决此问题并获得包含所有结果的报告?我找了解这个问题,但我没有找到类似的问题。

> sessionInfo()

$R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C              
[3] LC_TIME=pt_BR.UTF-8        LC_COLLATE=pt_BR.UTF-8    
[5] LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=pt_BR.UTF-8   
[7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                 
[9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] splines   grid      stats     graphics  grDevices utils    
[7] datasets  methods   base     

other attached packages:
[1] gmodels_2.15.4.1      plotrix_3.5-3        
[3] ggplot2_0.9.3.1       doBy_4.5-10          
[5] MASS_7.3-29           plyr_1.8             
[7] sqldf_0.4-6.4         RSQLite.extfuns_0.0.1
[9] chron_2.3-45          gsubfn_0.6-5         
[11] proto_0.3-10          pastecs_1.3-15       
[13] boot_1.3-9            psych_1.4.2.3        
[15] weights_0.75          gdata_2.13.2         
[17] anesrake_0.70         Hmisc_3.14-0         
[19] Formula_1.1-1         survival_2.37-7      
[21] lattice_0.20-24       RSQLite_0.11.4       
[23] DBI_0.2-7             survey_3.29-5        
[25] downloader_0.3        knitr_1.5            
[27] vimcom.plus_0.9-93    setwidth_1.0-3       
[29] colorout_1.0-2       

loaded via a namespace (and not attached):
[1] cluster_1.14.4      colorspace_1.2-4    dichromat_2.0-0    
[4] digest_0.6.4        evaluate_0.5.1      formatR_0.10       
[7] gtable_0.1.2        gtools_3.3.0        labeling_0.2       
[10] latticeExtra_0.6-26 lme4_1.0-6          Matrix_1.1-2       
[13] minqa_1.2.3         munsell_0.4.2       nlme_3.1-113       
[16] RColorBrewer_1.0-5  Rcpp_0.11.0         reshape2_1.2.2     
[19] scales_0.2.3        stringr_0.6.2       tcltk_3.0.2        
[22] tools_3.0.2 

0 个答案:

没有答案