小编Dav*_*ell的帖子

local()与R中的其他闭包方法有何不同?

昨天我从Bill Venables那里了解到local()如何帮助创建静态函数和变量,例如,

example <- local({
  hidden.x <- "You can't see me!"
  hidden.fn <- function(){
    cat("\"hidden.fn()\"")
  }
  function(){
    cat("You can see and call example()\n")
    cat("but you can't see hidden.x\n")
    cat("and you can't call ")
    hidden.fn()
    cat("\n")
  }
})
Run Code Online (Sandbox Code Playgroud)

从命令提示符的行为如下:

> ls()
[1] "example"
> example()
You can see and call example()
but you can't see hidden.x
and you can't call "hidden.fn()"
> hidden.x                 
Error: object 'hidden.x' not found
> hidden.fn()
Error: could not find function "hidden.fn"
Run Code Online (Sandbox Code Playgroud)

我已经在R中的静态变量中讨论过这种事情 …

closures r local static-variables

16
推荐指数
2
解决办法
486
查看次数

带日期的geom_vline()给出错误:提供给连续比例的离散值

收到Error: Discrete value supplied to continuous scale消息后ggplot,geom_vline()我做了一些实验,发现以下惊喜.

这是一个可重现的示例,从一些数据开始:

require(lubridate)
require(ggplot2)
df <- data.frame(
  date=dmy(c("2/6/2014", "3/6/2014", "4/6/2014", "5/6/2014")),
  value=1:4
)
Run Code Online (Sandbox Code Playgroud)

让我们用垂直线绘制"3/6/2014":

ggplot(data=df, aes(x=date, y=value)) + 
  geom_line() +
  geom_vline(xintercept = as.numeric(dmy("3/6/2014")), linetype=4)
Run Code Online (Sandbox Code Playgroud)

Ggplot,日期和vline问题的例证

但是,如果我们改变geoms的顺序:

ggplot(data=df, aes(x=date, y=value)) + 
  geom_vline(xintercept = as.numeric(dmy("3/6/2014")), linetype=4) +
  geom_line()
Run Code Online (Sandbox Code Playgroud)

生成以下错误消息:

Error: Discrete value supplied to continuous scale
Run Code Online (Sandbox Code Playgroud)

r ggplot2

13
推荐指数
1
解决办法
4839
查看次数

如何在Sweave文档中的表中包含超链接?

我有一个包含超链接的数据框,我希望将其作为可点击链接使用Sweave.我知道xtable,但不知道如何使用它来处理数据帧的内容作为LaTeX命令.

latex r sweave xtable knitr

6
推荐指数
1
解决办法
1097
查看次数

在knitr和Sweave中使用fig.cap的LaTeX错误

在knitr运行后,以下代码在LaTeX中导致"Runaway argument"错误:

\documentclass{article}

\begin{document}

<<fig.cap="This causes \\texttt{problems.}">>=
plot(1:10,10:1)
@

\end{document}
Run Code Online (Sandbox Code Playgroud)

这个原因并不明显,所以我希望这可以帮助那些可能遇到这个问题的人.

latex figure sweave knitr

3
推荐指数
1
解决办法
315
查看次数

标签 统计

r ×3

knitr ×2

latex ×2

sweave ×2

closures ×1

figure ×1

ggplot2 ×1

local ×1

static-variables ×1

xtable ×1