当重新使用包含绘图的块时,绘图没有被正确引用,即它们对于两个块都是相同的,尽管它们应该是不同的.我能做些什么呢?
## Test
```{r}
col <- "black"
```
```{r chunk1}
plot(0, col=col)
```
```{r}
col <- "red"
```
```{r chunk1}
```
Run Code Online (Sandbox Code Playgroud) 我有两个类(a和b),我想+为它们定义方法.我需要两种类的四种可能组合的不同方法,即:
a + a method 1
a + b method 2
b + a method 3
b + b method 4
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用S4进行多次调度,但我想知道是否有办法使用S3模拟这种行为.我的方法如下:
a <- "b"
class(a) <- "a"
b <- "e"
class(b) <- "b"
Ops.a <- function(e1, e2){
if (class(e1) == "a" &
class(e2) == "a")
print("a & a")
if (class(e1) == "a" &
class(e2) == "b")
print("a & b")
if (class(e1) == "b" &
class(e2) == "a")
print("b & a")
NULL
}
a …Run Code Online (Sandbox Code Playgroud) 我想svg在.Rmd文档中包含图像.svg是使用RSVGTipsDevice包创建的,因为我希望图像有一些工具提示.问题是,在将图像包含到生成的HTML文档中之后,工具提示不再起作用.虽然我在浏览器中打开svg时它们正常工作(链接到图像,将鼠标悬停在红色矩形上以查看工具提示).
http://www.gridhub.uni-bremen.de/public/pics/plot.svg
这是完整的.Rmd代码:
```{r}
library(RSVGTipsDevice)
devSVGTips("plot.svg", toolTipMode=1)
plot(0, type="n")
setSVGShapeToolTip("A rectangle","it is red")
rect(.1,.1,.4,.6, col='red')
dev.off()
```

Run Code Online (Sandbox Code Playgroud)
问题是:如何使工具提示工作?
该RDCOMclient程序包可用于连接Windows上的COM对象。在MSDN参考中,存在许多对应于值的枚举(例如,wdColorBlue对于蓝色,请参见此处)。这些可以直接在VBA代码中使用。
如何使用RDCOMClient处理枚举对象?是否有例如从R内检索枚举的对应值的方法?有一个功能,EnumValue但我看不到如何用于此目的。
一个小例子
下面的代码创建一个新的Word文档并在其中写入一些文本。段落更改为右对齐。在此2使用与枚举对应的整数wdAlignParagraphRight。我希望能够使用例如字符串"wdAlignParagraphRight"代替value 2。有没有办法做到这一点RDCOMclient?
x <- COMCreate("Word.Application") # create application
x[["visible"]] <- TRUE
x[["Documents"]]$Add()
x[["Selection"]]$TypeText("hello")
p <- x[["ActiveDocument"]][["Paragraphs"]]$Item(1)
p[["Alignment"]] <- 2
Run Code Online (Sandbox Code Playgroud) 我需要在彼此之下放置几个图,我希望边距和图形区域的宽度相同,因此它们整齐排列.请注意,我需要两个单独的图,而不是一个联合图.我想将每个保存在一个单独的PNG文件中.我只是希望它们的结构(边距,图形区域大小)相同.
library(ggplot2)
d <- data.frame(label=c("some very longe label with lots of text",
"another long label with lots of text",
"short",
"also short",
" short",
" also short"),
x = 1:6)
ggplot(d[1:2, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
ggplot(d[3:4, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
Run Code Online (Sandbox Code Playgroud)
我想要的是绘图2具有与绘图1中相同的左边距宽度,或多或少如下,当然不添加额外的空白;)
在基本图形中,我只是相应地设置par("mar").
我怎样才能在ggplot中实现这一目标?
我想以编程方式在我的.Rmd降价文档中包含大量图像.就像是
```{r echo=FALSE}
cat("")
```
Run Code Online (Sandbox Code Playgroud)
不会起作用,因为产生的.md结果是
```
## 
```
Run Code Online (Sandbox Code Playgroud)
我需要摆脱代码标签```和领先##.是否可以直接从R块中注入markdown代码?
BTY:同样的问题也适用于HTML.在这里,从R块中注入HTML代码也会非常有用.
我想在模型中使用nlme::lme(底部的数据)指定不同的随机效果.随机效应是:1)intercept并且position变化subject; 2) intercept变化comparison.这很简单lme4::lmer:
lmer(rating ~ 1 + position +
(1 + position | subject) +
(1 | comparison), data=d)
> ...
Random effects:
Groups Name Std.Dev. Corr
comparison (Intercept) 0.31877
subject (Intercept) 0.63289
position 0.06254 -1.00
Residual 0.91458
...
Run Code Online (Sandbox Code Playgroud)
但是,我想坚持,lme因为我也想模拟自相关结构(position是一个时间变量).我怎么能像上面那样使用lme?我在下面的尝试会影响效果,这不是我想要的.
lme(rating ~ 1 + position,
random = list( ~ 1 + position | subject,
~ 1 | comparison), data=d) …Run Code Online (Sandbox Code Playgroud) 我想离开加入两个数据帧,其中NA两侧的连接列中可能有s(即两code列)
a <- data.frame(code=c(1,2,NA))
b <- data.frame(code=c(1,2,NA, NA), name=LETTERS[1:4])
Run Code Online (Sandbox Code Playgroud)
使用dplyr,我们得到:
left_join(a, b, by="code")
code name
1 1 A
2 2 B
3 NA C
4 NA D
Run Code Online (Sandbox Code Playgroud)
使用SQL,我们得到:
CREATE TABLE a (code INT);
INSERT INTO a VALUES (1),(2),(NULL);
CREATE TABLE b (code INT, name VARCHAR);
INSERT INTO b VALUES (1, 'A'),(2, 'B'),(NULL, 'C'), (NULL, 'D');
SELECT * FROM a LEFT JOIN b USING (code);
Run Code Online (Sandbox Code Playgroud)
似乎dplyr连接NA不像SQL NULL值那样对待.
dplyr与SQL相同的方式行事? …我想编写一个装饰器函数,为函数添加一个计数器,计算它被调用的次数。例如
foo <- function(x) {x}
foo <- counter_decorator(foo)
foo(1)
foo(1)
# => the counter gets incremented with each call and has the value 2 now
Run Code Online (Sandbox Code Playgroud)
下面的方法基本上有效,但是:
...)。我不知道如何做到这一点。有任何想法吗?这是我到目前为止所做的:
# Init or reset counter
counter_init <- function() {
.counters <<- list()
}
# Decorate a function with a counter
#
# Each time the function is called the counter is incremented
#
# fun: function to be decorated
# fun_name: name in .counters list to store number …Run Code Online (Sandbox Code Playgroud)