[Rcpp-commits] r2751 - in pkg/Rcpp/inst/doc: . Rcpp-quickref

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 9 11:09:13 CET 2010


Author: romain
Date: 2010-12-09 11:09:13 +0100 (Thu, 09 Dec 2010)
New Revision: 2751

Modified:
   pkg/Rcpp/inst/doc/Makefile
   pkg/Rcpp/inst/doc/Rcpp-quickref/Rcpp-quickref.Rnw
Log:
xian patch + some tweak related to the new highlight

Modified: pkg/Rcpp/inst/doc/Makefile
===================================================================
--- pkg/Rcpp/inst/doc/Makefile	2010-12-09 09:55:08 UTC (rev 2750)
+++ pkg/Rcpp/inst/doc/Makefile	2010-12-09 10:09:13 UTC (rev 2751)
@@ -127,7 +127,6 @@
 Rcpp-quickref.pdf : Rcpp-quickref/Rcpp-quickref.Rnw
 	cp -f Rcpp-quickref/Rcpp-quickref.Rnw .
 	Rscript --vanilla -e "require(highlight); driver <- HighlightWeaveLatex(boxes = TRUE, bg = 'white' ); Sweave( 'Rcpp-quickref.Rnw', driver = driver ); "
-	Rscript --vanilla -e "tex <- readLines('Rcpp-quickref.tex'); tex <- gsub( 'begin{minipage}{0.9', 'begin{minipage}{0.42', tex, fixed = TRUE ) ; writeLines( tex, 'Rcpp-quickref.tex' )  "
 	Rscript --vanilla -e "tools::texi2dvi( 'Rcpp-quickref.tex', pdf = TRUE, clean = FALSE )"
 ifneq (,$(findstring edd,$(whoami)))
 	pdflatex Rcpp-quickref
@@ -135,6 +134,6 @@
 else
 	Rscript -e "tools::texi2dvi( 'Rcpp-quickref.tex', pdf = TRUE, clean = TRUE )"
 endif
-	rm -fr Rcpp-quickref.tex Rcpp-quickref.bbl Rcpp-quickref.blg Rcpp-quickref.aux Rcpp-quickref.out Rcpp-quickref.log Rcpp-quickref.Rnw
+	# rm -fr Rcpp-quickref.tex Rcpp-quickref.bbl Rcpp-quickref.blg Rcpp-quickref.aux Rcpp-quickref.out Rcpp-quickref.log Rcpp-quickref.Rnw
 	cp -f Rcpp-quickref/Rcpp-quickref-fake.Rnw Rcpp-quickref.Rnw
 

Modified: pkg/Rcpp/inst/doc/Rcpp-quickref/Rcpp-quickref.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-quickref/Rcpp-quickref.Rnw	2010-12-09 09:55:08 UTC (rev 2750)
+++ pkg/Rcpp/inst/doc/Rcpp-quickref/Rcpp-quickref.Rnw	2010-12-09 10:09:13 UTC (rev 2751)
@@ -29,6 +29,18 @@
 \title{\pkg{Rcpp} Quick Reference Guide}
 \date{\pkg{Rcpp} version \Sexpr{prettyVersion} as of \Sexpr{prettyDate}}
 
+
+\renewenvironment{Hchunk}%
+{%
+\vspace{0.5em}\noindent\begin{lrbox}{\highlightbox}%
+\begin{minipage}[b]{.42\textwidth}%
+}%
+{%
+\end{minipage}%
+\end{lrbox}%
+\fcolorbox{highlightBorder}{highlightBg}{\usebox{\highlightbox}}%
+\vspace{0.5em}}%
+
 \begin{document}
 \maketitle
 \thispagestyle{empty}
@@ -90,6 +102,23 @@
 int n = xx.size();
 @
 
+\paragraph{Inline}~
+  \newline
+  \newline
+<<lang=cpp>>=
+## Note - this is R code. inline allows rapid testing.
+require(inline)
+testfun = cxxfunction(
+            signature(x="numeric", i="integer"),
+            body = '
+                NumericVector xx(x);
+                int ii = as<int>(i);
+                xx = xx * ii;
+                return( xx );
+            ', plugin="Rcpp")
+testfun(1:5, 3)
+@
+
 \paragraph{Function}~
   \newline
   \newline
@@ -99,8 +128,7 @@
 @
 
 \paragraph{Environment}~
-  \newline
-  \newline
+    \newline
 <<lang=cpp>>=
 Environment stats("package:stats");
 Environment env( 2 ); // by position
@@ -173,7 +201,6 @@
 
 \paragraph{Using matrices}~
   \newline
-  \newline
 <<lang=cpp>>=
 // Initializing from SEXP, 
 // dimensions handled automatically
@@ -188,13 +215,60 @@
 for (int i = 0; i < xsize; i++) {
     xx[i] = 7;
 }
+// Same as above, using STL fill
+std::fill(xx.begin(), xx.end(), 8);
 
 // Assign this value to single element 
 // (1st row, 2nd col)
 xx(0,1) = 4;
 
-// Extract the second column
-NumericVector zz = xx( _, 1);
+// Reference the second column
+// Changes propagate to xx (same applies for Row)
+NumericMatrix::Column zzcol = xx( _, 1);
+zzcol = zzcol * 2;
+
+// Copy the second column into new object
+NumericVector zz1 = xx( _, 1);
+// Copy the submatrix (top left 3x3) into new object
+NumericMatrix zz2 = xx( Range(0,2), Range(0,2));
 @
 
+
+\paragraph{Random functions}~
+  \newline
+<<lang=cpp>>=
+// Set seed
+RNGScope scope;
+
+// For details see Section 6.7.1--Distribution functions of the `Writing R Extensions' manual. In some cases (e.g. rnorm), distribution-specific arguments can be omitted; when in doubt, specify all dist-specific arguments. The use of doubles rather than integers for dist-specific arguments is recommended. Unless explicitly specified, log=FALSE. 
+
+// Equivalent to R calls
+NumericVector xx = runif(20);
+NumericVector xx1 = rnorm(20);
+NumericVector xx1 = rnorm(20, 0);
+NumericVector xx1 = rnorm(20, 0, 1);
+
+// Example vector of quantiles
+NumericVector quants(5);
+for (int i = 0; i < 5; i++) {
+    quants[i] = (i-2);
+}
+
+// in R, dnorm(-2:2)
+NumericVector yy = dnorm(quants) ;
+NumericVector yy = dnorm(quants, 0.0, 1.0) ;
+
+// in R, dnorm(-2:2, mean=2, log=TRUE)
+NumericVector yy = dnorm(quants, 2.0, true) ;
+
+// Note - cannot specify sd without mean
+// in R, dnorm(-2:2, mean=0, sd=2, log=TRUE)
+NumericVector yy = dnorm(quants, 0.0, 2.0, true) ;
+
+// To get original R api, use Rf_*
+double zz = Rf_rnorm(0, 2);
+@
+
+
+
 \end{document}



More information about the Rcpp-commits mailing list