【问题标题】:Weird print showing up in knitr PDFknitr PDF 中出现奇怪的打印
【发布时间】:2015-07-10 18:43:59
【问题描述】:

在 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}

【问题讨论】:

  • 投反对票可能是因为您除了屏幕截图之外没有分享任何内容——没有代码。期望通常是 minimal reproducible 示例,您的示例两者都不是。你也没有展示任何你可能试图解决问题的东西。我从来没有真正使用过 Sweave,但是在 knitr 中有很多块选项可以很好地控制这类事情,我很确定 Sweave 在这方面是相似的。
  • @Gregor 原因是我很难创建一个可重现的示例。我正在进行编辑,但应该会解决这个问题。
  • 这看起来不像 Sweave,它看起来像 knitr。如果是knitr,我可以回答。
  • knitr 一直给我带来麻烦。如果您能提供帮助,我将不胜感激。
  • 请问您使用的是哪个版本的data.table

标签: r data.table knitr


【解决方案1】:

这里发生了两件事。打印出来的表格来自您的行:

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

该行同时分配Benchmark 返回修改后的表。您可以通过两种方式修复它:

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

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

    <<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')
@

【讨论】:

  • 非常感谢您的回答。我一直在搜索谷歌,但不确定如何搜索“不需要的 table print dplyr”。
  • @David 非常乐意提供帮助。正如其他人指出的那样,首先显示代码会很有帮助,而不仅仅是输出 - 仅从 PDF 输出中无法找到表格打印问题。
  • 我很感激。我最初没有包含代码,因为我无法确定哪些部分是负责任的,因此我所有可重现的示例都没有重现它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
相关资源
最近更新 更多