2014-02-26 18 views
3

我寫了一個包optiRum的函數generatePDF,它包裝了knitr:::knit2pdf以允許添加日期分量和其他一些零碎。R針對函數開發中的knitr/data.table組合的怪異範圍問題

通常這工作正常,但我發現了一個奇怪的情況,其中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 
+0

我正在安裝latex以嘗試您的代碼...在此期間您可能需要查看您的data.table用法。這個'df $'P值'<-df [,paste0(round(value/1000,2),「k」)]'不會從包的主要特性中受益,通過引用進行更改而不會複製整個表。 – Michele

+0

我有一個乳膠到PDF轉換的問題(看起來fancyvrd.sty丟失了或者其他東西),但是我確實在'.tex'中有了上面的腳本R腳本的執行很好 – Michele

+0

爲快速響應而歡呼。用法的多樣性是查看它是否是一種或所有類型的訪問方法,而當在「結果=」中使用時,「as = is」分塊傾向於將輸出內容輸出到.tex(然後pdf)文件。在.tex文件中,你有這個字符串是否存在'\ color {errorcolor}'? –

回答

1

設置envirknit2pdf似乎工作。我下載了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 
+0

並確認,它是從包中爲你工作,而不是創建一個全局函數並利用它? –

+0

@StephLocke你說得對。它不能與包中的功能一起使用...我會在看到這個後刪除 –

+0

對不起,麻煩! –