Learn R: As a Language

Image of the book cover

ISBN 9780367182533

My book on R was published on 28 July 2020. The R package ‘learnrbook’ available through CRAN contains data and the R code scripts and chunks from the book. The book has now its own website.

In the book’s website you will find free supplementary chapters as downloadable PDF files and other material complementing the book.

[2022-06-24] Last week I started updating the book manuscript for a 2nd printing/edition. For the time being I have been adding several diagrams and flow charts, expanding the text to improve clarity and fixing all typos in text and code that I am aware of. I am also addressing the new features in R 4.2.0 and changes to the packages used in the examples. Please, do share with me anything you would like changed from the current edition and report any mistakes you may have found. All useful contributions will be acknowledged. Thanks in advance!

 

photobiology 0.10.12

The package documentation web site at https://docs.r4photobiology.info/photobiology/ includes a changelog with information for each release since version 0.1.0.


BUG FIX: Conversions between Date and POSIXct objects are tricky because objects of the former class do not store information on the time zone. A change in ‘lubridate’ 1.8.0 made a previously working approach to these conversions silently fail to apply the shift to the hours.

If you use any of day_night(), sunrise_time(), noon_time() and sunset_time() either use ‘lubridate’ (< 1.8.0) or update a.s.a.p. to ‘photobiology’ (>= 0.10.12) from GitHub.


WARNING: In the current version of ‘photobiology’, if no time zone argument is passed concurrently with a date, the date is assumed to be in UTC. If this time zone does not match the location given by the geocode, the date used for the calculations can be wrong by one day.

Package ‘photobiology’ continues using UTC (universal time coordinates) as its default time zone. This differs from some functions in recent versions of package ‘lubridate’ that use the time zone reported by the OS, indicated by the character string "" as their default. Using "UTC" as default ensures that the code output using defaults will not depend on the OS’ time zone settings, making it reproducible across geographical locations.

This release corrects problems triggered by recent updates to packages ‘lubridate’ and possibly ‘tibble’ (reported by putmanlab in issue #7 ).  It also adds some enhancements for class solute_spct, still under development.

Changes from version 0.10.11 the most recent CRAN release, are:

  • Bug fix: With ‘lubridate’ (1.8.0) but not with previous versions, functions day_night(), sunrise_time(), noon_time() and sunset_time() would return wrong time values when non-default arguments to parameter tz were passed together with objects of class Date passed as arguments to date.
  • Add methods as.filter_spct() and as.solute_spct specialised for two-way conversion between objects of classes solute_spct and filter_spct.
  • Revise the class solute.properties adding fields solvent.name and solvent.ID.
  • Revise documentation checking that units expected for arguments and of returned values are clearly indicated and correctly formatted. Update outdated text and correct mistakes and revise unclear explanations.

NOTE: Version 0.10.12 will be submitted to CRAN as soon as it reopens for submission. Meanwhile, it can be installed from GitHub.

Please raise issues concerning bugs or enhancements to this package through GitHub at https://github.com/aphalo/photobiology/issues

photobiology 0.10.11

The package documentation web site is at https://docs.r4photobiology.info/photobiology/ includes a changelog with information for each release since version 0.1.0.

This update adds flexibility to the user interface used to indicate the target when searching for wavelengths matching specific spectral data values. It also contains a preliminary implementation of classes solute_spct and collections solute_mspct for storing spectral molar or mass coefficients of attenuation.

Changes from version 0.10.10 the most recent CRAN release, are:

  • Add new classes of objects solute_spct and solute_mspct to be used to store molar (default) and mass based coefficients of attenuation describing overall attenuation, or attenuation by absorption or by scattering. Implement the corresponding methods. (Unstable: interface may change).
  • Add example data for two substances: water.spct and phenylalanine.spct.
  • Rewrite join_mspct() to use interpolation when wavelengths differ among member spectra. This should not break old code but output can slightly differ.
  • Expand syntax accepted for character arguments passed to parameter target in all wls_at_target() methods and function find_wls().
  • Fix failure to correctly handle spectra with zero rows, a bug affecting several methods, operators and functions including rbindspct() and find_wls().
  • Fix bug in rowwise_filter() affecting parallel summaries of absorptance.
  • Fix bugs in extraction and replacement functions for collections of spectra, possibly triggered by changes in R >= 4.0.0.
  • Add method s_mean_se_band_band().

NOTE: Version 0.10.11 has been submitted to CRAN.

Please raise issues concerning bugs or enhancements to this package through GitHub at https://github.com/aphalo/photobiology/issues

ggpmisc 0.4.7

Version 0.4.7 brings a fix for a bug that could prevent the use of weights passed through aesthetic weight in some of the model-fitting statistics. Several enhancements to the model fitting statistics make it easier to fit different models to different groups or panels, and make it possible/easier to select among methods supported by a model fit function.

Here is a simple example of fitting different models to different panels. Instead of using lm() as method, we define a wrapper function that tests for the significance of the slope in linear regression, and if not significant, fits the mean instead.

poly_or_mean <- function(formula, data, ...) {
   mf <- lm(formula = formula, data = data, ...)
   if (anova(mf)[["Pr(>F)"]][1] > 0.1) {
      lm(formula = y ~ 1, data = data, ...)
   } else {
      mf
   }
}

We pass the name of this function as argument to method overriding the default argument "lm".

ggplot(mpg, aes(displ, hwy)) +
   geom_point() +
   stat_poly_line(method = "poly_or_mean") +
   stat_poly_eq(method = poly_or_mean,
   aes(label = after_stat(eq.label)),
   label.x = "right") +
   theme(legend.position = "bottom") +
   facet_wrap(~class, ncol = 2)

Which produces the figure below, showing the mean in panels 2seater and minivan, and a linear regression in others.

Aditional examples are available in the free supplementary chapters of my book Learn R: As a Language.

Changes compared to version 0.4.6, the previous version in CRAN are:

  • Fix bug in the handling of the weight aesthetic in some of the model fitting statistics.
  • The model formula is in calls to stat_poly_eq() and stat_quant_eq() now retrieved from the returned fitted model object. This makes it possible model selection within the function passed as argument to method. (Inspired by an answer read in Stackoverflow.)
  • Statistics now search for a matching function when an arbitrary name is supplied as a character string argument to parameter method.
  • The character string passed as argument to parameter method is now parsed so that it can contain both the name of a model fit function and the argument to be passed to this function’s own method parameter. (Backward compatibility is maintained.)
  • The stats that create equation labels now include a variable method in the returned data containing a character string with the method used in the model fit.

Documentation web site at http://docs.r4photobiology.info/ggpmisc/ includes all help pages, with output from all examples, vignettes as well as a changelog in HTML format.

NOTE: Version 0.4.7 is on its way to CRAN.

Please raise issues concerning bugs or enhancements to this package through GitHub https://github.com/aphalo/ggpmisc/issues

photobiologyInOut 0.4.24

This non-code-breaking update brings a few new features and tracks deprecations in package ‘tidyr’.

The main changes from version 0.4.23 the previous CRAN release, are:

  • Replace use of deprecated tidyr::gather_().
  • Add function read_cid_spectravue_csv() supporting data import from measurements CSV files output by CID Bio-Science’s SpectraVue CI-710s leaf spectrometer.
  • Add function read_foreign2mspct() to apply an import function to a list of files.
  • Add color-related functions spct_CRI(), spct_CCT() and expect_SSI() implemented as wrappers to functions in package ‘colorSpec’. These functions accept source_spct objects as arguments. CRI, CCT and SSI are quantities used to describe light used for illumination, photography, cinematography and video.
  • Documentation web site at http://docs.r4photobiology.info/photobiologyInOut/.

NOTE: This version of the package is on its way to CRAN.

Please raise issues concerning bugs or enhancements to this package through Bitbucket at https://github.com/aphalo/photobiologyinout/issues