I am looking for a way to structure my statistical analysis. I currently use Emacs/ESS and the analysis file turned out quite long. I have started to put parts of the code into sourceable functions in a separate file, but still...
我正在寻找一种方法来组织我的统计分析。我目前使用Emacs/ESS,分析文件显示很长。我已经开始将部分代码放在一个单独的文件中,但是……
I would like to introduce some kind of subheadings/ section titles in the file (Aggregation, Cluster-analysis, Simulations, ...) and write the code beneath it, so I could quickly jump to the sections that I want to work on.
我想在文件中引入一些副标题/节标题(聚合、集群分析、模拟……)并在其下面编写代码,这样我可以快速跳转到我想要处理的部分。
I guess I could just use comments and search for them, but I couldn't get an overview or an index that way. I also thought about using Org-Mode for the Headlines, but that wouldn't be very convenient for collaborators, who work with another Editor.
我想我可以只使用注释和搜索它们,但是我不能用这种方式获得概述或索引。我也考虑过在标题中使用组织模式,但这对与另一个编辑器一起工作的合作者来说不是很方便。
I know R-Studio implements this with sections, so there will be a solution for emacs, right?
我知道R-Studio是用分段实现的,所以会有一个emacs的解决方案,对吗?
Thank you very much!
非常感谢!
PS: something like imenu would work, but that's just for functions, not for sections
附注:像imenu这样的东西是可以用的,但那只是针对函数,而不是部分
11
Recent (Feb 2013) additions to Orgmode mean that you should now be able to embed org headings in your source code, and then navigate through them using orgstruct-mode. So, upgrade your org mode via git, and then try opening the following example R file. When you are on a comment line that has an org heading embedded, just hit TAB, or shift-TAB, and you should get org-mode headings.
最近(2013年2月)对Orgmode的添加意味着您现在应该能够在源代码中嵌入org标题,然后使用orgstruct模式导航它们。因此,通过git升级您的org模式,然后尝试打开以下示例R文件。当你在评论线上有一个组织标题嵌入,只要按TAB,或shift-TAB,你就会得到组织模式的标题。
### * Create data
data = list( s1=list(x=1:3, y=3:1),
s2=list(x=1:5, y=1:5), s3=list(x=1:4, y=rep(3,4)))
### * Base graphics version
par(mfrow=c(2,2))
lapply(data, plot)
### * Lattice version
nplots <- length(data)
pts.per.plot <- sapply(data, function(l) length(l$x))
df <- data.frame(which=rep(1:nplots, times=pts.per.plot),
x=unlist(sapply(data, function(l) l$x)),
y=unlist(sapply(data, function(l) l$y)))
xyplot(y~x|which, data=df, layout=c(2,2))
### ** Make the pdf
pdf(file='o.pdf')
xyplot(y~x|which, data=df, layout=c(2,1))
dev.off()
### * End of file
### Local Variables:
### eval: (orgstruct-mode 1)
### orgstruct-heading-prefix-regexp: "### "
### End:
15
In general I use org-mode and org-babel, but when I've got to share scripts with others, I've got the following in my .emacs
:
一般来说,我使用的是org-mode和org-babel,但当我必须与其他人共享脚本时,我的。emacs中有如下内容:
(defgroup ess-jb-faces nil
"Faces used by cutomized ess-mode"
:group 'faces)
(defface ess-jb-comment-face
'((t (:background "cornsilk"
:foreground "DimGrey"
:inherit font-lock-comment-face)))
"Face used to highlight comments."
:group 'ess-jb-faces)
(defface ess-jb-comment-bold-face
'((t (:weight bold
:inherit ess-jb-comment-face)))
"Face used to highlight bold in comments."
:group 'ess-jb-faces)
(defface ess-jb-h1-face
'((t (:height 1.6
:weight bold
:foreground "MediumBlue"
:inherit ess-jb-comment-face)))
"Face used to highlight h1 headers."
:group 'ess-jb-faces)
(defface ess-jb-h2-face
'((t (:height 1.2
:weight bold
:foreground "DarkViolet"
:inherit ess-jb-comment-face)))
"Face used to highlight h2 headers."
:group 'ess-jb-faces)
(defface ess-jb-h3-face
'((t (:height 1.0
:weight bold
:foreground "DarkViolet"
:inherit ess-jb-comment-face)))
"Face used to highlight h3 headers."
:group 'ess-jb-faces)
(defface ess-jb-hide-face
'((t (:foreground "white"
:background "white"
:inherit ess-jb-comment-face)))
"Face used to hide characters."
:group 'ess-jb-faces)
(font-lock-add-keywords 'ess-mode
'(("^###\\(#\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h1-face t))
("^###\\(##\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h2-face t))
("^###\\(###\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h3-face t))
("^###\\( .*\\|$\\)" 1 'ess-jb-comment-face t)
("^###" "\\*.*?\\*" nil nil (0 'ess-jb-comment-bold-face append))
))
With this, any comment with ####
at the beginning of the line is formatted as an "header 1". Any comment with #####
is formatted as an "header 2", etc. And lines which begin with ###
is seen as a comment with a special font-lock (used for long comments).
有了这个,任何在行的开头的####的注释都被格式化为“标题1”。任何带有##### #####的注释都被格式化为“头2”,以###开头的行被视为带有特殊类型锁的注释(用于长注释)。
This can give something like this :
它可以给出这样的东西:
This is quite hacky, but the advantage is that it only uses standard R comments and as such can be shared without problem with others. In general I use the following for "header 1" : others see it as below, while I enjoy my defined font-lock :
这是一种非常粗糙的做法,但是它的优点是它只使用标准的R注释,并且这样可以与其他用户共享。一般来说,我使用以下的标题1:其他人如下所示,而我喜欢我定义的字体锁定:
############################################
#### HEADER 1
############################################
With this syntax, you can also use the following to activate outline-minor-mode
on the sections defined previously and be able to fold/unfold them :
有了这种语法,您还可以使用以下方法来激活前面定义的区段上的out - line-minor模式,并能够折叠/展开它们:
(add-hook 'ess-mode-hook
'(lambda ()
(auto-complete-mode nil)
(outline-minor-mode 1)
(setq outline-regexp "\\(^#\\{4,6\\}\\)\\|\\(^[a-zA-Z0-9_\.]+ ?<- ?function(.*{\\)")
(setq outline-heading-alist
'(("####" . 1) ("#####" . 2) ("######" . 3)
("^[a-zA-Z0-9_\.]+ ?<- ?function(.*{" .4)))))
All this code has not been very well tested, and I'm far from an expert in emacs lisp, so there should be better ways to do it, and don't be surprised in case of bugs !
所有这些代码都没有经过很好的测试,而且我还不是emacs lisp的专家,所以应该有更好的方法来实现它,并且在出现bug时不要感到惊讶!
8
Sounds like you have one big analysis script. That's not very optimal...
听起来你有一个大的分析脚本。不是很理想…
Think about redoing it with RMarkdown or Sweave, and using knitr
to run it. Then you can use emacs' tools for navigating markdown or TeX and you can use the caching system in knitr
to save having to do analyses right from the start when you've changed something near the end.
考虑使用RMarkdown或Sweave重新执行它,并使用knitr运行它。然后,您可以使用emacs的工具来导航markdown或TeX,还可以使用knitr中的缓存系统,以避免在您在接近结束时更改某些内容时必须从头开始进行分析。
You also get a formatted report out of the analysis, if you want.
如果需要,还可以从分析中得到格式化的报告。
Also, breaking stuff out into separate files for sourcing is a bit sub-optimal - far far better to write functions and put them in a package - and to use the devtools
package to make working with it really simple. Just edit your .R files and load_all
updates it (none of the complex package build stuff of times past).
此外,将内容分解成单独的文件进行源代码处理也有点不太理想——编写函数并将其放入包中要好得多——并且使用devtools包使其工作起来非常简单。只需编辑. r文件和load_all更新它(没有任何复杂的过去构建包的内容)。
Big winnage.
大winnage。
3
polymode provides R+markdown, R+brew, R+cpp and other stuff. It is still in early stage of development and seems to work reliably only on very recent emacs (24.3).
polymode提供R+markdown、R+brew、R+cpp等功能。它仍处于开发的早期阶段,似乎只能在最近的emacs中可靠地工作(24.3)。
1
Similarly to the orgstruct-mode mentioned above you can also use the new outshine package which works together with outline-minor-mode to interpret comments with asterisks as org-mode-like headings. They can be folded/expanded/added/etc using simple shortcuts like TAB, etc.
与上面提到的orgstruct模式类似,您还可以使用新的outshine包,它与outline-minor-mode一起将带有星号的注释解释为类似于org-mode的标题。它们可以使用简单的快捷方式进行折叠/扩展/添加/等等。
0
Another option, which I've been happy with over the years is allout-mode. Using per-file local variables:
另一种选择是allout模式,多年来我一直很满意。使用上面的局部变量:
## Local variables:
## allout-layout: (-1 : 0)
## End: