我正在尝试建立一个工作流程来回答SO问题,knitr并使用和
render_markdown(strict = T )格式化我的答案和/问题.
最近我试图对一些代码进行分析,profr并且由于实现knitr,分析会收集所有调用evaluate等等的代码.knitr
例如
如果我在平原上跑 R
library(profr)
quantile_ex <- profr({Sys.sleep(1); example(quantile, setRNG = TRUE)}, 0.01)
quantile_ex
## f level time start end leaf source
## 9 Sys.sleep 1 0.64 0.01 0.65 TRUE base
## 10 example 1 0.05 0.65 0.70 FALSE utils
## 11 index.search 2 0.01 0.65 0.66 FALSE <NA>
## 12 <Anonymous> 2 0.02 0.66 0.68 FALSE <NA>
## 13 source 2 0.01 0.68 …Run Code Online (Sandbox Code Playgroud) 我是R的新手,所以请原谅我的无知.我制作了一个伪堆叠的条形图,其中我使用geom_bar在彼此的顶部绘制了4组条形图.三种橡树(QUAG,QUKE,QUCH)有4种健康状况类别(活着,死亡,感染和死亡).
我的代码如下:
x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109), infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))
x.plot = ggplot(x, aes(variable, alive)) + geom_bar(fill="gray85") +
geom_bar(aes(variable,dead), fill="gray65") +
geom_bar(aes(variable, infected), fill="gray38") +
geom_bar(aes(variable, sod.dead), fill="black")+
opts(panel.background = theme_rect(fill='gray100'))
x.plot
Run Code Online (Sandbox Code Playgroud)
现在我想制作一个传说,显示哪个灰色阴影与树状态有关,即"灰色65"是"死树"等等.我一直在尝试过去一小时而无法让它工作.
如果我这样做,我会得到正确的结果:
a <- c("10","28","3")
which(as.numeric(a) == min(as.numeric(a)))
[1] 3
Run Code Online (Sandbox Code Playgroud)
但是如果向量中有NA,则存在问题
a <- c("10","28","3","NA")
which(as.numeric(a) == min(as.numeric(a)))
integer(0)
Warning messages:
1: In which(as.numeric(a) == min(as.numeric(a))) :
NAs introduced by coercion
2: In which(as.numeric(a) == min(as.numeric(a))) :
NAs introduced by coercion
Run Code Online (Sandbox Code Playgroud) 我试图找到ggplot2R包中的美学和几何列表以及help(qplot不会产生任何结果的问题.我找不到一种方法来调用只有美学或几何的帮助.
在R中调用美学帮助的正确方法是什么?
我知道有一种简单的方法可以做到这一点......但是,我无法弄明白.
我的R脚本中有一个数据帧,如下所示:
A B C
1.2 4 8
2.3 4 9
2.3 6 0
1.2 3 3
3.4 2 1
1.2 5 1
Run Code Online (Sandbox Code Playgroud)
请注意,A,B和C是列名.而我正试图得到这样的变量:
sum1 <- [the sum of all B values such that A is 1.2]
num1 <- [the number of times A is 1.2]
Run Code Online (Sandbox Code Playgroud)
有什么简单的方法吗?我基本上想要得到一个如下所示的数据框:
A num totalB
1.2 3 12
etc etc etc
Run Code Online (Sandbox Code Playgroud)
其中"num"是特定A值出现的次数,"totalB"是给定A值的B值之和.
我担心会有这样的反应:"Markdown本来就很简单而且不会这样做",但它(几乎)从来都不会伤害.
在编写R Markdown文档时,我可以在浏览器中查看HTML文件,它看起来很棒.当我尝试以纸张或PDF格式打印时,会打印图中的颜色,但不会显示语法高亮显示.有没有办法在打印时保持语法高亮?
例:
Minimal Example
=====
This text looks great in the file and the plot prints in color, but see commented code below.
```{r}
# this commented line will be green in the HTML file, but will be black when I print it
z <- cor(mtcars)
require(lattice) # 'require' will be blue in the HTML file, but will be black when I print it
levelplot(z)
```
Run Code Online (Sandbox Code Playgroud)
我按下RStudio中的"Knit HTML"按钮并在Chrome或Safari中打开HTML,没有任何问题.如果我从浏览器中打印HTML,则所有语法突出显示都将丢失.
我试图通过使用ggplot()来显示框图中的中值(即水平条).R一直要求指定y轴.我有点卡住了.
p <-structure(list(TYPE = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L,
3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
2L, 3L, 3L, 3L, 3L, …Run Code Online (Sandbox Code Playgroud) 我经常编写R代码,例如,我测试向量的长度,数据帧中的行数或矩阵的维数if (length(myVector) == 1).在一些基本的R代码中,我注意到在这种比较中,值被明确地表示为整数,例如通常使用'L'后缀if (nrow(data.frame) == 5L).显式整数有时也用于函数参数,例如cor函数中的这些语句:x <- matrix(x, ncol = 1L)和apply(u, 2L, rank, na.last = "keep").什么时候应该在R中明确指定整数?不指定整数会产生任何潜在的负面后果吗?
我经常在GNU R/ggplot中绘制图形,用于与字节相关的一些测量.内置轴标签是普通数字或科学记数法,即1兆字节= 1e6.我想要SI前缀(Kilo = 1e3,Mega = 1e6,Giga = 1e9等),即轴应标记为1.5K,5K,1M,150M,4G等.
我目前使用以下代码:
si_num <- function (x) {
if (!is.na(x)) {
if (x > 1e6) {
chrs <- strsplit(format(x, scientific=12), split="")[[1]];
rem <- chrs[seq(1,length(chrs)-6)];
rem <- append(rem, "M");
}
else if (x > 1e3) {
chrs <- strsplit(format(x, scientific=12), split="")[[1]];
rem <- chrs[seq(1,length(chrs)-3)];
rem <- append(rem, "K");
}
else {
return(x);
}
return(paste(rem, sep="", collapse=""));
}
else return(NA);
}
si_vec <- function(x) {
sapply(x, FUN=si_num);
}
library("ggplot2");
bytes=2^seq(0,20) + rnorm(21, 4, 2);
time=bytes/(1e4 …Run Code Online (Sandbox Code Playgroud)