R函数开发中针对knitr / data.table组合的奇怪范围问题

时间:2014-02-26 11:15:46

标签: r data.table knitr

我编写了一个名为optiRum的包generatePDF的函数,该函数包含knitr:::knit2pdf以允许添加日期组件和其他一些碎片。

通常这种方法很好,但我发现了data.table功能错误的奇怪情况。

示例文件:

\documentclass{article}

\begin{document}
\tableofcontents
<<>>=
library("data.table")
df<-data.table(expand.grid(seq(10000,200000,1000),seq(10000,200000,2000),seq(100000,400000,5000)))
setnames(df,c("Var1","Var2","Var3"),c("ad","fm","value"))
df<-df[ad+fm<value*.9&ad+fm>=value*.6]
df<-df[,`P Value`:=paste0(round(value/1000,2),"k")]
df<-df[,`ad Amount`:=paste0(round(ad/1000,2),"k")]
df<-df[,ratio:=(ad+fm)/value]
@

<<>>=
ggplot(df[value %in% seq(100000,400000,50000)&fm==50000],aes(x=ratio,y=ad,group=value,colour=`P Value`))+
  geom_line()
@
\end{document}

当我使用optiRum:::generatePDF时,例如

install.packages("optiRum")
library("optiRum")
generatePDF( srcname="sample" ,destname="sample")

我收到以下类型的错误:

# Error: object 'ad' not found

knit2pdf对它自己的工作正常。如果我使用generatePDF代码并使其成为全局函数并重新执行,那么它也可以正常工作。据我所知,就范围而言,我并没有做任何奇怪的事情。

有没有人知道如何解决?


generatePDF <- function (srcpath = getwd(), srcname, destpath = getwd(), destname, 
    DATED = FALSE, CLEANUP = TRUE, ...) 
{
    require("knitr")
    stopifnot(is.character(srcpath), is.character(srcname), is.character(destpath), 
        is.character(destname), is.logical(DATED), is.logical(CLEANUP), 
        file.exists(file.path(srcpath, paste0(srcname, ".Rnw"))))
    knit2pdf(input = file.path(srcpath, paste0(srcname, ".Rnw")), 
        output = file.path(destpath, paste0(destname, ifelse(DATED, 
            format(Sys.Date(), "%Y%m%d"), ""), ".tex")), compiler = "pdflatex")
    if (CLEANUP) 
        file.remove(dir(path = destpath, pattern = "*aux|*out|*toc", 
            full.names = TRUE))
}

更新

更改上述示例代码中的分配方法会产生不同的错误:

## Error: := and `:=`(...) are defined for use in j, once only and
in particular ways. See help(":="). Check is.data.table(DT) is TRUE.

如果它是相关的 - 在linux和windows上测试

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

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C            LC_COLLATE=C         LC_MONETARY=C       
 [6] LC_MESSAGES=C        LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C         LC_TELEPHONE=C      
[11] LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

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

other attached packages:
[1] optiRum_0.30      scales_0.2.3      ggthemes_1.6.0    AUC_0.3.0         ggplot2_0.9.3.1   data.table_1.8.10
[7] knitr_1.5         testthat_0.8.1    devtools_1.4.1   

loaded via a namespace (and not attached):
 [1] MASS_7.3-29        RColorBrewer_1.0-5 RCurl_1.95-4.1     colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4      
 [7] evaluate_0.5.1     formatR_0.10       grid_3.0.2         gtable_0.1.2       highr_0.3          httr_0.2          
[13] labeling_0.2       memoise_0.1        munsell_0.4.2      parallel_3.0.2     plyr_1.8           proto_0.3-10      
[19] reshape2_1.2.2     stringr_0.6.2      tools_3.0.2        whisker_0.3-2   

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

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

other attached packages:
[1] data.table_1.8.10 knitr_1.5         optiRum_0.21     

loaded via a namespace (and not attached):
[1] evaluate_0.5.1 formatR_0.10   highr_0.3      stringr_0.6.2  tools_3.0.2 

1 个答案:

答案 0 :(得分:1)

envir中设置knit2pdf似乎有效。我下载了package:optiRum的来源,并更改了generatePDF以将...传递给knit2pdf。文档说他们应该去哪里,但原来的功能实际上并没有与...做任何事情。所以generatePDF变为:

generatePDF<-function(srcpath = getwd() , srcname ,
                      destpath = getwd() , destname ,
                      DATED = FALSE , CLEANUP = TRUE ,
                      ...){
  require("knitr")
  stopifnot(
    is.character(srcpath),
    is.character(srcname),
    is.character(destpath),
    is.character(destname),
    is.logical(DATED),
    is.logical(CLEANUP),
    file.exists(file.path(srcpath , paste0(srcname ,
                                           ".Rnw")))
    )


  knit2pdf( input  = file.path(srcpath , paste0(srcname ,
                                             ".Rnw")),
            output = file.path(destpath , paste0(destname ,
                                              ifelse(DATED, format(Sys.Date(),"%Y%m%d"),"") ,
                                              ".tex") ) ,
            compiler='pdflatex',
            ...
  )
  if (CLEANUP) file.remove(dir( path=destpath,pattern="*aux|*out|*toc",full.names=TRUE))


}

然后,从源代码安装并使用OP中编辑的.Rnw后,运行:

library(optiRum)
optiRum::generatePDF(srcname = "sample", destname = "sample", envir=new.env(parent = .GlobalEnv))

对我来说很好。现在也在Mac上..但我认为它只是环境。

## R version 3.0.2 (2013-09-25)                                                                                                                                                                                                               
## Platform: x86_64-apple-darwin10.8.0 (64-bit)                                                                                                                                                                                               

## locale:                                                                                                                                                                                                                                    
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8                                                                                                                                                                          

## attached base packages:                                                                                                                                                                                                                    
## [1] stats     graphics  grDevices utils     datasets  methods   base                                                                                                                                                                       

## other attached packages:                                                                                                                                                                                                                   
## [1] data.table_1.8.11 ggplot2_0.9.3.1   knitr_1.4.1       optiRum_0.21                                                                                                                                                                     

## loaded via a namespace (and not attached):                                                                                                                                                                                                 
##  [1] colorspace_1.2-2   compiler_3.0.2     dichromat_2.0-0    digest_0.6.3                                                                                                                                                                 
##  [5] evaluate_0.4.7     formatR_0.9        grid_3.0.2         gtable_0.1.2                                                                                                                                                                 
##  [9] highr_0.2.1        labeling_0.2       MASS_7.3-29        munsell_0.4.2                                                                                                                                                                
## [13] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2                                                                                                                                                               
## [17] scales_0.2.3       stringr_0.6.2      tools_3.0.2