knitr PDF 中出现奇怪的印刷品

Weird print showing up in knitr PDF

在 R Sweave 文件 (.Rnw) 中,当我使用 knitr 编译 PDF 时,有不需要的 things 打印。很难解释到底发生了什么。我认为这是使用 data.table 包和 dplyr 包的结果,但我找不到任何其他示例。但是,我什至不确定我可以搜索什么。

这是问题的屏幕截图:

老实说,除了我想弄清楚如何摆脱它之外,我真的不知道还能说什么。如果有人认为这是一个简单的问题,而不仅仅是否决票,你能否指出正确的方向,让我知道在哪里可以对此进行一些研究?

\documentclass[11pt]{article}
\usepackage[sc]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\setlength{\parindent}{0in}
\usepackage{url}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\hypersetup{
 pdfstartview={XYZ null null 1}}
\usepackage{breakurl}
\usepackage{longtable}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(stringr)
library(RODBC)
library(plyr)
library(reshape2)
library(ggplot2)
library(grid)
library(data.table)

rw1 <- c("File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3")
rw2 <- c("0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03")
rw3 <- c("Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final")
rw4 <- c(123, 456, 789, 312, 645, 978, 741, 852, 963, 369, 258, 147, 753, 498, 951, 753, 915, 438, 978, 741, 852, 963, 369, 258, 147, 753, 498)
rw5 <- c("01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12")
rw6 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3)
rw7 <- c("Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release")
rw8 <- c("None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "Cannot Connect to Database", "None", "None", "None", "None", "None", "None", "None", "None")


Testdf = data.frame(rw1, rw2, rw3, rw4, rw5, rw6, rw7, rw8)
colnames(Testdf) <- c("FileName", "Version", "Category", "Value", "Date", "Number", "Build", "Error") 

@

\title{Report}


\author{Current version}

\maketitle


\section{Report Summary}
This report documents the results 

<<Benchmarks,echo=FALSE>>=
library(ggplot2)
# library(data.table)
library(dplyr)

versions<-unique(Testdf[order(Testdf$Number), ][,2])

# Testdf %<>%
#     group_by(FileName) %>%
#     mutate(Benchmark = Value[which(Category == "Time" & Number == min(Number))]) %>%
#   ungroup()
setDT(Testdf)
Testdf[, Benchmark := Value[which.min(Number[Category == "Time"])], by = FileName]

Testdf$Version<-factor(Testdf$Version, levels = versions)
Testdf$Deviation<-Testdf$Value- Testdf$Benchmark
Testdf$DeviationP<-(Testdf$Value- Testdf$Benchmark)/Testdf$Benchmark



g<-ggplot(subset(Testdf, Category == 'Time') , aes(color = Value, x = Version, y = DeviationP, group = FileName)) + 
  geom_line(size=.25) + geom_point(aes(shape = Build), size = 1.5) +
  scale_shape_manual(values=c(1,15)) + stat_summary(fun.y=sum, geom="line") + 
  ylab("Run Time Deviation from Benchmark (min)") +  
  scale_colour_gradient(name = 'Run Time (min)',low = 'blue', high = 'red') + 
  coord_cartesian(ylim=c(-105,105)) +
  theme(axis.text.x = element_text(size = 10, angle = 90, vjust = .5)) + theme(axis.title.y = element_text(vjust = 1)) + 
  theme(axis.title.x = element_text(vjust = -0.1)) + theme(plot.margin=unit(c(0,0,0,0),"mm"))
g

@


\end{document}

这里有两件事。正在打印的 table 来自您的行:

Testdf[, Benchmark := Value[which.min(Number[Category == "Time"])], by = FileName]

该行都分配了 Benchmark returns 修改后的 table。您可以通过两种方式修复它:

  1. 将输出分配给同一个变量(或另一个,实际上并不重要):

    Testdf <- Testdf[, Benchmark := Value[which.min(Number[Category == "Time"])], by = FileName]
    
  2. 通过将 results = 'hide' 添加到该 knitr 块的 header 来避免打印任何结果:

    <<Benchmarks,echo=FALSE, results = 'hide'>>=
    

斜体的黑色消息是来自 library(dplyr) 的程序包启动消息。同样,两种修复方法的选择:

  1. 通过将 dplyr-loading 行更改为:

    来仅隐藏这些消息
    suppressPackageStartupMessages(library(dplyr))
    
  2. 通过将 message = FALSE 添加到该块的头部来隐藏该块中的所有消息:

    <<setup, include=FALSE, cache=FALSE, message=FALSE>>=
    

在这两种情况下,请注意,您可以通过在开头添加一个块来更改所有块中的默认值:

<<set_defaults, echo = FALSE>>=
knitr::opts_chunk$set(message = FALSE, results = 'hide')
@