使用 Sweave 给 R 写文档的模板

以前一直傻傻的 用 \begin{verbatim} 或者 listing 的方式在文章中加 R 代码
然后再手工 include 结果的方式,现在知道了 Sweave,终于可以结束这段傻
不楞登的过程直接自动化了。

\documentclass[a4paper, 12pt]{article}
\begin{document}
\title{emacs \& Sweave}
\author{Wang Jun}
\maketitle

\section{Now begins R Sweave test}

First Save this file as example.Rnw

Then run Sweave("example") in R

Compile the generated tex file example.tex with pdflatex or xelatex (I prefer XeLeTeX)

Here you got the Sweaved R script

%%%%%%%%%%%%% Here begins the Main Content

This is just a simple example

<<>>=
library(maps)
library(mapdata)
###map("china")
x=rnorm(100)
y=rnorm(100)
lm(y~x)
@

Now that you have tried Sweave, how does it perform?

\end{document}

PS: 当然,现在流行的 R 文档写作方式应该是 knitr + LyX的方式,将另文描述了。

一个Fortran 读文件并带参数的模板

Fortran 作为科学计算常用语言,自然免不了各种文件读写的操作,以下是一个简单的模板

参见 gist bdb32fffcd08b9ab21ea:

或者,直接参见下面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
program gaugeselect
implicit none
character(len=80):: filename
character(len=80):: arg1 ,arg2
character(len=80):: buff
logical:: flag=.false.
!================================================================!
call getarg(1,arg1)
call getarg(2,arg2)
if (len_trim(arg1)==0 )then
print *,"please specify the argument of running file!!"
print *,"usage: ./gaugeselect filename"
stop
else
filename=trim(arg1)
inquire(file=trim(filename),exist=flag)
if(flag)then
open(5,file=trim(filename))
do while(.true.)
read(5,*,end=100)buff
enddo
100 close(5)
print *,'file:',trim(filename), "processed"
else
print *,"oops........"
print *,'can NOT open file:',trim(filename)
print *," "
endif
endif
end program gaugeselect