我想使用R markdown添加为每个页面创建一个页脚(包括第一个!)的问题.编织到pdf时,那里的代码(也在下面)对我来说非常好.但我不会得到html或docx输出的页眉/页脚.
在R Markdown中,无论输出文档的类型如何,我还能为输出文档的每个页面生成页眉/页脚?
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
\lipsum[1-30]
Run Code Online (Sandbox Code Playgroud) 我想制作一个简单的R图,y轴标签位于y轴刻度标签的中心.我用下面的代码创建了我喜欢的东西.但它需要对at图形参数进行一些摸索.
问:这样做的方法是否不那么简单?有没有办法查询y轴刻度标签的宽度,以便我可以使用该信息将y轴标签置于其上方?
set.seed(1)
n.obs <- 390
vol.min <- .20/sqrt(252 * 390)
eps <- rnorm(n = n.obs, sd = vol.min)
windows(width = 5.05, height = 3.8)
plot(eps, main = "Hello World!", las=1, ylab="")
mtext(text="eps", side=3, at=-60)
Run Code Online (Sandbox Code Playgroud)

我想添加问题使用R markdown为每个页面创建页脚:除了以下所有页面之外,我如何为文档的第一页执行此操作?
在RStudio中,这段代码:
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
\lipsum[1-30]
Run Code Online (Sandbox Code Playgroud)
在pg1的底部生成这个:

这个(奇怪的是,这不应该在左边因为\fancyfoot[LE,RO]{\thepage}?)在pg2的底部:

这在pg3的底部:

如何在数据框中获得密集的多列列?例如,
# I have:
df <- data.frame(x = c(1,1,1,1,2,2,2,3,3,3),
y = c(1,2,3,4,2,2,2,1,2,3))
# I want:
res <- data.frame(x = c(1,1,1,1,2,2,2,3,3,3),
y = c(1,2,3,4,2,2,2,1,2,3),
r = c(1,2,3,4,5,5,5,6,7,8))
res
x y z
1 1 1 1
2 1 2 2
3 1 3 3
4 1 4 4
5 2 2 5
6 2 2 5
7 2 2 5
8 3 1 6
9 3 2 7
10 3 3 8
Run Code Online (Sandbox Code Playgroud)
我的hack方法适用于这个特定的数据集:
df %>%
arrange(x,y) %>%
mutate(r = if_else(y - lag(y,default=0) …Run Code Online (Sandbox Code Playgroud) 我开始玩R中的lubridate包.我注意到now(tzone="EST")计算如下:
[1] "2015-08-25 13:01:08 EST"
Run Code Online (Sandbox Code Playgroud)
而now(tzone="PST")导致的警告:
[1] "2015-08-25 18:02:16 GMT"
Warning message:
In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'PST'
Run Code Online (Sandbox Code Playgroud)
那么众所周知的时区是什么? Lubridate的有效时区有一个答案.但我想知道如何为自己解答这个问题(即通过挖掘包本身).我看看这个now()功能:
> now
function (tzone = "")
with_tz(Sys.time(), tzone)
<environment: namespace:lubridate>
Run Code Online (Sandbox Code Playgroud)
那么我看一下这个with_tz功能:
> with_tz
function (time, tzone = "")
{
check_tz(tzone)
if (is.POSIXlt(time))
new <- as.POSIXct(time)
else new <- time
attr(new, "tzone") <- tzone
reclass_date(new, time)
}
<environment: namespace:lubridate>
>
Run Code Online (Sandbox Code Playgroud)
那么我检查一下这个check_tz功能:
> check_tz
Error: object …Run Code Online (Sandbox Code Playgroud) 我的项目目录结构的一部分如下所示:
\projects\project\main.R
\projects\project\src
Run Code Online (Sandbox Code Playgroud)
其中\src包含一堆1-function-per-file,项目特定的函数.
问:将这些功能添加到工作目录的最佳实践方法是projects\project什么?
我看到了一些解决方案:
attach("./src").我试图避免这种情况,因为(1)Google Styleguide建议避免使用attach()和(2)我收到了
Warning messages:
Run Code Online (Sandbox Code Playgroud)
1: Reading Unix style database directory (./tmp) from Splus on Windows: may
have problems finding some datasets, especially those whose names
differ only by case (file tmp-script1.ssc should not have been made by
Splus on Windows) in: exists(name, where = db)
这样做的时候.
lapply(paste("./src/",list.files("./src/"),sep=""),source).这很好用,看起来很笨重.必须有更好的方法,对吗?
以他们的全名参考我的职能./src/myfunc.这会很快变得难看.我相信有更好的方法.
摆脱./src我的目录的一部分,只是把所有的功能扔在主工作目录中.这个问题是我宁愿保持一个接近John Myles White的目录结构ProjectTemplate
将所有函数放在一个文件中,./src/func.R并将其作为源代码.我想这种方法避免了"2"的丑陋.上面,但我真的希望每个文件都有一个功能.只是看起来更清洁.
目前我做以下事情:
x <- cbind(c(1, 2, 3), c(4, 5, 6))
x.merged <- matrix(t(x), ncol=1)
Run Code Online (Sandbox Code Playgroud)
使用1, 4, 2, 5, 3, 6,矩阵中的值创建一个列x.但依靠t(x)似乎有点笨重.有一个更好的方法吗?我想避免使用for循环,或者apply是否有一个更简单的内置函数来处理这类事情.
编辑:更清楚,x只是给了我.上面的第一行代码仅用于说明所涉及的值.我可能写得更好:
> x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Run Code Online (Sandbox Code Playgroud) 我想编写一个处理多种数据类型的函数.下面是一个有效的例子,但看起来很笨重.这样做有标准(或更好)的方法吗?
(这是像这样的时候我想念Matlab,其中一切都是一种类型:>)
myfunc = function(x) {
# does some stuff to x and returns a value
# at some point the function will need to find out the number of elements
# at some point the function will need to access an element of x.
#
# args:
# x: a column of data taking on many possible types
# e.g., vector, matrix, data.frame, timeSeries, list
x.vec <- as.vector(as.matrix(as.data.frame(x)))
n <- length(x.vec)
ret <- x.vec[n/3] # this line only for …Run Code Online (Sandbox Code Playgroud) 我有一data.frame组和日期.如何填写每组最小 - 最大日期范围内的所有缺失日期?
理想情况下我会这样做dplyr.但最终,我只想用尽可能少的(可读)代码行来有效地完成这项工作.以下是一个最小的例子.我实际上有很多日期和小组.我的两种方法看起来都很难看.必须有更好的方法,对吗?
#### setup ####
library(sqldf)
library(dplyr)
df <- data.frame(the_group = rep(LETTERS[1:2], each=3), date = Sys.Date() + c(0:2, 1:3), stringsAsFactors = F) %>%
tbl_df() %>%
slice(-2) # represents that I may be missing data in a range!
#### dplyr approach with cross join dummy ####
full_seq <- data.frame(cross_join_dummy = 1, date = seq.Date(from=min(df$date), to=max(df$date), by = "day"))
range_by_group <- df %>%
group_by(the_group) %>%
summarise(min_date = min(date), max_date = max(date)) %>%
ungroup() %>%
mutate(cross_join_dummy = …Run Code Online (Sandbox Code Playgroud) 在bookdown中,是否有一个LaTeX数学环境可以对每个方程进行编号,无论输出是否为.pdf、.docx、.html?添加这个乳胶:
\begin{align}
X &= Y \\
Z &= W
\end{align}
Run Code Online (Sandbox Code Playgroud)
bookdown -demo输出以下内容:
PDF:按预期工作。
DOCX:缺少方程编号。
HTML:缺少方程式编号。
笔记:
pdf_book, word_document2,生成的gitbook。 更新:将下面拉尔夫的答案与我在 bookdown 中的其他学习相结合,以下所有内容在 .pdf、.docx、.html 输出中一致且按预期工作。
Add a single un-numbered equation:
\begin{equation*}
X = Y
\end{equation*}
Add a single numbered equation:
\begin{equation}
X = Y
(\#eq:eq02)
\end{equation}
I refer to previous, equation \@ref(eq:eq02).
Add multiple un-numbered equations:
\begin{align*}
X …Run Code Online (Sandbox Code Playgroud) 我想在R中的向量上执行指数加权移动平均值(此处定义参数化).是否有比下面第一次尝试更好的实现?
我的第一次尝试是:
ewma <- function(x, a) {
n <- length(x)
s <- rep(NA,n)
s[1] <- x[1]
if (n > 1) {
for (i in 2:n) {
s[i] <- a * x[i] + (1 - a) * s[i-1]
}
}
return(s)
}
y <- 1:1e7
system.time(s <- ewma(y,0.5))
#user system elapsed
# 2.48 0.00 2.50
Run Code Online (Sandbox Code Playgroud)
在我的第二次尝试中,我认为我可以通过矢量化做得更好:
ewma_vectorized <- function(x,a) {
a <- 0.1
n <- length(x)
w <- cumprod(c(1, rep(1-a, n-1)))
x1_contribution <- w * x[1]
w <- a …Run Code Online (Sandbox Code Playgroud) 我想在我的 .Rmd bookdown 文件中包含 LaTeX 环境(例如, algorithmicfrom algorithmicx、minifrom optidef、dcasesfrom等)。mathtools这对于 pdf 输出来说没有问题。但这些不会呈现 html 或 docx 输出。
我当前的黑客解决方案:
缺点:
必须有更好的方法,对吗?我在想有一种方法可以告诉 rmarkdown/LaTeX,当渲染为 pdf 时,某些代码块应该以某种图像格式保存。这样,它们就可以作为图像添加回文档中,条件是输出文档是 docx 或 html。这可能吗?
更新:对 TikZ 独立图的回答提出了一种涉及 LaTeXstandalone包的方法。但不幸的是,人们发现它不能与独立的算法一起工作,这不适用于该algorithm环境。有任何想法吗?
---
title: "Bookdown"
header-includes:
- \usepackage{float}
- \floatplacement{figure}{!htb}
- \usepackage{algorithm}
- \usepackage{algpseudocode}
output:
bookdown::gitbook:
split_by: none
bookdown::pdf_book:
fig_caption: yes
keep_tex: yes …Run Code Online (Sandbox Code Playgroud) 问:在我下面的二叉树实现中,为什么编译器会窒息
if (data.compareTo(this.data) <= 0),
生产
Error: incompatible types: java.lang.Comparable<T> cannot be converted to T?
这两个data和this.data的类型Comparable<T>,应该能够使用或者是一个参数传递给的compareTo()方法,对吧?好吧,显然不是.但我真的不明白为什么.仿制药仍然令我感到困惑.
public class MyBinaryTreeNodeG<T>{
Comparable<T> data;
MyBinaryTreeNodeG<T> parent;
MyBinaryTreeNodeG<T> left;
MyBinaryTreeNodeG<T> right;
public MyBinaryTreeNodeG(Comparable<T> data){
this.data = data;
}
public MyBinaryTreeNodeG<T> addChild(Comparable<T> data){
if (data.compareTo(this.data) <= 0) { //this is the line on which the compiler chockes
//check if left tree node is null. If so, add. Otherwise, recurse.
} else {
//same for the right …Run Code Online (Sandbox Code Playgroud)