在R Sweave文件(.Rnw)中,当我使用knitr编译PDF时,会有不需要的事物打印。很难解释究竟发生了什么。我认为这是使用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}
答案 0 :(得分:4)
这里有两件事。打印出来的表格来自您的行:
Testdf[, Benchmark := Value[which.min(Number[Category == "Time"])], by = FileName]
该行都指定Benchmark
列和返回已修改的表。您可以通过两种方式解决它:
将输出分配给同一个变量(或其他变量,实际上并不重要):
Testdf <- Testdf[, Benchmark := Value[which.min(Number[Category == "Time"])], by = FileName]
通过将results = 'hide'
添加到该knitr块的标题中,避免任何打印结果:
<<Benchmarks,echo=FALSE, results = 'hide'>>=
斜体的黑色消息是来自library(dplyr)
的包启动消息。同样,有两种选择如何解决它:
通过将dplyr加载行更改为:
来隐藏这些消息suppressPackageStartupMessages(library(dplyr))
通过将message = FALSE
添加到该块的头部来隐藏块中的所有消息:
<<setup, include=FALSE, cache=FALSE, message=FALSE>>=
在这两种情况下,请注意您可以通过在开头添加块来更改所有块中的默认值:
<<set_defaults, echo = FALSE>>=
knitr::opts_chunk$set(message = FALSE, results = 'hide')
@