Skip to content

Commit

Permalink
Make everything work with 'learnrbook' as submitted to CRAN. Test the…
Browse files Browse the repository at this point in the history
… chapters can be built individually. Check clean up at end-of-chapters.
  • Loading branch information
aphalo committed May 12, 2017
1 parent ec25783 commit ed4825c
Show file tree
Hide file tree
Showing 20 changed files with 328,997 additions and 64,801 deletions.
32 changes: 28 additions & 4 deletions R.data.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ The aim of this chapter is to describe, and show how, some of the existing enhan

\section{Packages used in this chapter}

<<eval=FALSE>>=
install.packages(learnrbook::pkgs_ch_data)
@

For executing the examples listed in this chapter you need first to load the following packages from the library:

<<message=FALSE>>=
library(learnr)
library(learnrbook)
library(tibble)
library(magrittr)
library(stringr)
Expand All @@ -39,14 +43,14 @@ library(jsonlite)
@

\begin{infobox}
Some data sets used in this and other chapters are available in package \pkgname{learnr}. In addition to the
Some data sets used in this and other chapters are available in package \pkgname{learnrbook}. In addition to the
R data objects, we provide files saved in \emph{foreign} formats, which we used in examples on how to import data. The files can be either read from the R library, or from a copy in a local folder. In this chapter we
assume the user has copied the folder \code{"extdata"} from the package to his working folder.

Copy the files using:

<<load-pkg>>=
pkg.path <- system.file("extdata", package = "learnr")
<<copy-data-files>>=
pkg.path <- system.file("extdata", package = "learnrbook")
file.copy(pkg.path, ".", overwrite = TRUE, recursive = TRUE)
@

Expand Down Expand Up @@ -1566,3 +1570,23 @@ unlink("./extdata", recursive = TRUE)
@

\end{infobox}

<<eco=FALSE>>=
try(detach(package:jsonlite))
try(detach(package:lubridate))
try(detach(package:ncdf4))
try(detach(package:RNetCDF))
try(detach(package:xml2))
try(detach(package:haven))
try(detach(package:foreign))
try(detach(package:xlsx))
try(detach(package:readxl))
try(detach(package:readr))
try(detach(package:tidyr))
try(detach(package:dplyr))
try(detach(package:stringr))
try(detach(package:magrittr))
try(detach(package:tibble))
try(detach(package:learnrbook))
@

11 changes: 11 additions & 0 deletions R.friends.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ In this final chapter I highlight what in my opinion are limitations and advanta

\section{Packages used in this chapter}

<<eval=FALSE>>=
install.packages(learnrbook::pkgs_ch_performance)
@

For executing the examples listed in this chapter you need first to load the following packages from the library:

<<message=FALSE>>=
Expand Down Expand Up @@ -164,3 +168,10 @@ The\index{command shell}\index{sh}\index{bash} operating system shell can be acc
There is a lot to write on this aspect, and intense development efforts going on. One example is the \pkgname{Shiny} package and Shiny server \url{https://shiny.rstudio.com/}. This package allows the creation of interactive displays to be viewed through any web browser.

There are other packages for generating both static and interactive graphics in formats suitable for on-line display, as well as package \pkgname{knitr} used for writing this book \url{https://yihui.name/knitr/}, which when using R Markdown for markup (with package \pkgname{rmarkdown} \url{http://rmarkdown.rstudio.com} or \pkgname{Bookdown} \url{https://bookdown.org/} can output self-contained HTML files in addition to RTF and PDF formats.

<<>>=
try(detach(package:rJava))
# try(detach(package:rPython))
try(detach(package:inline))
try(detach(package:Rcpp))
@
6 changes: 4 additions & 2 deletions R.intro.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

\subsection{What is R?}

Most people think of R as a computer program. R is indeed a computer program---a piece of software---, but it is also a computer language, implemented in the R program. Does this make a difference? Yes, until recently we had only one mainstream implementation of R, the program R. In the last couple of years another implementation has started to gain popularity, Microsoft R. These are not the only two implementations, but others are not in widespread use. In other words, the R language can be used not only in the R program.
Most people think of R as a computer program. R is indeed a computer program---a piece of software---, but it is also a computer language, implemented in the R program. Does this make a difference? Yes, until recently we had only one mainstream implementation of R, the program R. In the last couple of years another implementation has started to gain popularity, Microsoft R. These are not the only two implementations, but others are not in widespread use. In other words, the R language can be used not only in the R program.

Being \Rpgrm a command line application in its simplest incarnation, it can be used on what nowadays are frugal computing resources, equivalent to a personal computer of a couple of decades ago. \Rpgrm can run even on the Raspberry Pi, a Linux micro-controller board with the processing power of a modest smartphone. At the other end of the spectrum on really powerful servers \pgrmname{R} can be used for the analysis of big data sets with millions of observations. How powerful a computer you will need will depend on the size of the data sets you want to analyze, and on how patient you are, together your ability to write `good' code.

Expand All @@ -20,7 +20,7 @@ However, the most import advantage of using R is that it makes it easy to do dat

The R program itself is open-source, the source code is available for anybody to inspect, modify and use. A small fraction of users will directly contribute improvements to the R program itself, but it is possible, and those contributions are important in making R reliable. The executable, the R program we actually use, can be built for different operating systems and computer hardware. The members of the R developing team make an important effort to keep the results obtained from calculations done on all the different builds and computer architectures as consistent as possible.

The R program does not have a graphical user interface (GUI), or menus from which to start different types of analyses. The user types the commands at the R console, or saves the commands into a text file, and uses the file as a `script' or list of commands to be run. When we work at the console typing in commands one by one, we say that we use R interactively. When we run script we would say that we run a ``batch job''. These are the two options that R by itself provides, however, it is common to use a front-end program ``in-between'' users and R itself. The simplest option is to use a text editor like Emacs to edit the scripts and then run the scripts in R from within the editor. With some editors like Emacs, rather good integration is possible. However, nowadays there are also Integrated Development Environments available for R, of which, RStudio is
The R program does not have a graphical user interface (GUI), or menus from which to start different types of analyses. The user types the commands at the R console, or saves the commands into a text file, and uses the file as a `script' or list of commands to be run. When we work at the console typing in commands one by one, we say that we use R interactively. When we run script we would say that we run a ``batch job''. These are the two options that R by itself provides, however, it is common to use a front-end program ``in-between'' users and R itself. The simplest option is to use a text editor like Emacs to edit the scripts and then run the scripts in R from within the editor. With some editors like Emacs, rather good integration is possible. However, nowadays there are also Integrated Development Environments available for R, of which, RStudio is
the most popular by a wide margin.

\subsubsection{Using R interactively}
Expand Down Expand Up @@ -185,4 +185,6 @@ Currently, for the development of packages, I use \pgrmname{RStudio} exclusively

When I started using R, nearly two decades ago, I was using other editors, using the operating system shell a lot more, and struggling with debugging as no IDE was available. The only reasonably good integration with an editor was for Emacs, which was widely available only under Unix-like systems. Given this past experience, I encourage you to use an IDE for R. \pgrmname{RStudio} is nowadays very popular, but if you do not like it, need a different set of features, such as integration with \pgrmname{ImageJ}, or are already familiar with the \pgrmname{Eclipse} IDE, you may like try the \pgrmname{Bio7} IDE, available from \url{http://bio7.org}.

All data sets and files needed to run the examples in the book can be obtained by installing different R packages. One of them \pkgname{learnrbook} available through CRAN, contains datasets and files not earlier available in R packages. The \pkgname{learnrbook} package also contains installation instructions and saved names of all other packages used in the book. Instructions on installing R, git, RStudio, and, also compilers and other tools in those cases they are needed, are available on-line. In many cases the IT staff at your employer or school will know how to still them, or they may be even included in the default setup. In addition we give step by step instructions in the Appendix (currently missing).


26 changes: 25 additions & 1 deletion R.maps.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,33 @@ In the second half of the chapter we describe how to load, write and manipulate

\section{Packages used in this chapter}

If the packages used in this chapter are not yet installed in your computer, you can install them with, as long as package \pkgname{learnrbook} is already installed.

<<eval=FALSE>>=
install.packages(learnrbook::pkgs_ch_maps_raster)
@

For executing the examples listed in this chapter you need first to load the following packages from the library:

<<>>=
library(learnrbook)
library(ggplot2)
library(ggmap)
library(rgdal)
library(scatterpie)
library(imager)
@

\begin{infobox}
As in Chapter \ref{chap:R:data}, we will use in the present chapter example files contained in package \pkgname{learnrbook}. Unless you have already copied these files, you need to copy them before running the code included in the section on package \pkgname{imager}.

<<copy-img-files>>=
pkg.path <- system.file("extdata", package = "learnrbook")
file.copy(pkg.path, ".", overwrite = TRUE, recursive = TRUE)
@

\end{infobox}

\section[ggmap]{\pkgname{ggmap}}

<<>>=
Expand Down Expand Up @@ -678,8 +695,15 @@ We can see above that function \Rfunction{nPix()} returns the number of pixels i
Package \pkgname{imager} is a package containing many different functions wrapping functions from library \pgrmname{CImg}, the examples given here are only an introduction to the most basic of its capabilities. This library is written in \langname{C++} using templates, and can be instantiated at compile time for different types of pixel data. Consequently one cannot expect that calling these functions from R can be as fast as a good \langname{C++} implementation of the same operations with the same library. On the other hand, for relatively small images or small numbers of images, calling the library from R allows the use of R for computations on pixel data, which opens the door to the quick development and testing of pixel related statistical algorithms.
\end{infobox}

<<clear-up-img-folders>>=
unlink("./extdata", recursive = TRUE)
@

<<>>=
try(detach(package:imager))
try(detach(package:ggmap))
try(detach(package:scatterpie))
try(detach(package:rgdal))
try(detach(package:ggmap))
try(detach(package:ggplot2))
try(detach(package:learnrbook))
@
78 changes: 61 additions & 17 deletions R.more.plotting.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ In contrast with previous chapters, I expect readers to first browse through the

\section{Packages used in this chapter}

If the packages used in this chapter are not yet installed in your computer, you can install them with, as long as package \pkgname{learnrbook} is already installed.

<<eval=FALSE>>=
packages_at_cran <- setdiff(learnrbook::pkgs_ch_ggplotext, learnrbook::pkgs_at_github)
install.packages(packages_at_cran)
for (p in learnrbook::pkgs_at_github) {
devtools::install_github(p)
}
@

For executing the examples listed in this chapter you need first to load the following packages from the library:

<<message=FALSE>>=
library(learnrbook)
library(tibble)
library(ggplot2)
library(showtext)
Expand All @@ -38,14 +49,14 @@ library(ggstance)
library(ggbiplot)
library(ggalt)
library(ggExtra)
# library(ggfortify) # loaded later
library(ggnetwork)
library(geomnet)
# library(ggradar)
library(ggsci)
library(ggthemes)
library(xts)
library(MASS)
# library(ggfortify) # loaded later
# library(ggtern) # loaded later
@

We set a font larger size than the default
Expand Down Expand Up @@ -759,15 +770,6 @@ ggplot(data = blood$edges, aes(from_id = from, to_id = to)) +
Change the graphic design of the plot in steps, by changing: 1) the shape of the nodes, 2) the color of the nodes, 3) the size of the nodes and the size of the text, 4) the type of arrows and their size, 5) the font used in nodes to italic.
\end{playground}

%\section[ggradar]{\pkgname{ggradar}}\label{sec:plot:ggradar}
%
%<<>>=
%citation(package = "ggradar")
%@
%
%Package \pkgname{ggradar} allows the creation of radar plots with \pkgname{ggplot2}. However, its user interface does not follow the grammar of graphics paradigm.


\section[`ggforce']{\pkgname{ggforce}}\label{sec:plot:ggforce}

<<>>=
Expand Down Expand Up @@ -1233,9 +1235,9 @@ ggplot(my.data, aes(x, y2, colour = group)) +

\subsubsection{Other types of models}

Another statistic, \Rfunction{stat\_fit\_glance()} allows lots of flexibility, but at the moment there is no equivalently flexible version of \Rfunction{stat\_smooth()}.
Another statistic, \Rfunction{stat\_fit\_glance()} allows lots of flexibility. The current version \pkgname{ggplot2} implements \Rfunction{stat\_smooth()} in a way that also allows ample flexibility.

We give an example with a linear model, showing a P-value (a frequent request for which I do not find much use).
We give a first example with a linear model, showing a P-value (a frequent request for which I do not find much use).

We use \Rfunction{geom\_debug()} to find out what values \Rfunction{stat\_glance()} returns for our linear model, and add labels with P-values for the fits.

Expand Down Expand Up @@ -1266,7 +1268,7 @@ ggplot(my.data, aes(x, y2, colour = group)) +
theme(legend.position = "top")
@

We use \Rfunction{geom\_debug()} to find out what values \Rfunction{stat\_glance()} returns for our resistant linear model fitted with \Rfunction{rlm()} from package \pkgname{MASS}.
We use \Rfunction{geom\_debug()} to find out what values \Rfunction{stat\_fit\_glance()} returns for our resistant linear model fitted with \Rfunction{rlm()} from package \pkgname{MASS}.

<<>>=
formula <- y ~ x + I(x^2) + I(x^3)
Expand Down Expand Up @@ -1298,7 +1300,49 @@ ggplot(my.data, aes(x, y2, colour = group)) +
theme(legend.position = "top")
@

In a similar way one can generate labels for any fit supported by package \pkgname{broom}.
In a similar way one can generate labels for any fit supported by package \pkgname{broom}, such as a Michaelis-Menten equation.

<<>>=
micmen.formula <- y ~ SSmicmen(x, Vm, K)
ggplot(Puromycin, aes(conc, rate, colour = state)) +
geom_point() +
geom_smooth(method = "nls",
formula = micmen.formula,
se = FALSE) +
stat_fit_glance(method = "nls",
method.args = list(formula = micmen.formula),
geom = "text",
aes(label = paste("AIC = ", signif(..AIC.., digits = 3),
", BIC = ", signif(..BIC.., digits = 3),
sep = ""))) +
theme_bw() +
theme(legend.position = "top")
@

To add an equation, or the values of the fitted parameters we use the statistic \Rfunction{stat\_fit\_tidy()}. We show an example of adding an equation using paste to build a character string that is later parsed into a
suitable expression. We skip here the use of \code{geom\_debug()} as the names of the parameters
can be found in the help page for \Rfunction{SSmicmen()}.

<<>>=
micmen.formula <- y ~ SSmicmen(x, Vm, K)
ggplot(Puromycin, aes(conc, rate, colour = state)) +
geom_point() +
geom_smooth(method = "nls",
formula = micmen.formula,
se = FALSE) +
stat_fit_tidy(method = "nls",
method.args = list(formula = micmen.formula),
geom = "text",
label.x.npc = 0.9,
label.y.npc = 0.3,
aes(label = paste("V~`=`~frac(", signif(..Vm.., digits = 2), "~C,",
signif(..K.., digits = 2), "+C)",
sep = "")),
parse = TRUE) +
labs(x = "C", y = "V") +
theme_bw() +
theme(legend.position = "top")
@

\subsection{Highlighting deviations from fitted line}
\index{plots!fitted curves!deviations}
Expand Down Expand Up @@ -1880,12 +1924,12 @@ ggplot(my.anscombe, aes(x = x, y = y)) +
\index{plots!advanced examples!quadrat plot}

<<>>=
try(detach(package:ggtern))
try(detach(package:ggfortify))
try(detach(package:MASS))
try(detach(package:xts))
try(detach(package:ggthemes))
try(detach(package:ggsci))
#try(detach(package:ggradar))
try(detach(package:geomnet))
try(detach(package:ggnetwork))
try(detach(package:ggExtra))
Expand All @@ -1902,5 +1946,5 @@ try(detach(package:viridis))
try(detach(package:showtext))
try(detach(package:ggplot2))
try(detach(package:tibble))
try(detach(package:learnrbook))
@

Loading

0 comments on commit ed4825c

Please sign in to comment.