Linux Format - UK (2020-03)

(Antfer) #1
92 LXF260 March 2020 http://www.linuxformat.com

CODING ACADEMY Dates & time


using by executing the R --version command, provided
that the directory of the R binary is in your PAT H
environment variable. The output will be similar to the
following below:
R version 3.6.1 (2019-07-05) -- “Action of the Toes”
Copyright (C) 2019 The R Foundation for Statistical
Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
If you have time and want to see some of the
capabilities of R, you can execute the demo() and help()
commands from the R shell. Lastly, the demo(graphics)
command will present a demo of the graphical
capabilities of R. Figure 1 shows a screen from the
output of demo(graphics).

Using a Docker image
Nowadays, almost all software can be run as a container
using a Docker image, and R is no exception. You can
get the official R Docker image by executing docker pull
r-base – find more information about that Docker image
by visiting https://hub.docker.com/_/r-base. However,
this tutorial will use the rocker/rstudio:latest Docker
image that contains R as well as RStudio. You can find
more information about it at https://hub.docker.
com/r/rocker/rstudio.
For reasons of simplicity, we are going to use a
docker-compose.yml file to execute the RStudio Docker
image. There will be a shared volume between the local

he subject of this tutorial is creating calendars
using R. The R language of statistical
computing is a free implementation of the S
language. This tutorial will show you how to use a
Docker image to execute RStudio and how to work with
dates and times in R, before explaining how to create
calendars – being able to work with dates and times,
and knowing how to extract the desired parts from a
date string or structure is really important for defining
the calendar you want.
We’ll then move on toexplaning how to use Python 3
to create a calendar heat map with random data.

Installing R
You can install R using your favourite package manager
or by following the instructions found in the R FAQ
(https://cran.r-project.org/doc/FAQ/R-FAQ.html). As
you will see, you can also run R and RStudio using a
Docker image.
You can enter the R shell by executing R on your
favourite terminal. You can find the version of R you are

Figure 2: How to work with times, dates and locales in R, how to convert
from epoch time and output of the help(DateTimeClasses) command.

Build calendars with


date and time types


Mihalis Tsoukalos explains how to work with dates and times in R,


along with a touch of Python to draw impressive calendars.


Mihalis
Tsoukalos
is a data engineer
and a technical
writer. He is also
the author of
Go Systems
Programming
and Mastering
Go, 2nd edition.
You can reach
him at http://www.
mtsoukalos.eu
and @mactsouk.

T


R CALENDARS


OUR
EXPERT

You can learn
more about
R at http://www.r-
project.org and
about RStudio
at https://
rstudio.com.
Additionally,
search for R
packages by
visiting CRAN
at http://cran.r-
project.org. Figure 1: Output of the demo(graphics) command as executed in the RStudio environment that was installed and executed as a Docker image.

92 LXF260 March 2020 http://www.linuxformat.com


CODING ACADEMY Dates & time


usingbyexecutingtheR--versioncommand,provided
thatthedirectoryoftheR binaryisinyourPAT H
environmentvariable.Theoutputwillbesimilarto the
followingbelow:
Rversion3.6.1(2019-07-05)--“ActionoftheToes”
Copyright(C) 2019 TheRFoundationforStatistical
Computing
Platform:x86_64-pc-linux-gnu(64-bit)
...
If youhavetimeandwanttoseesomeofthe
capabilities of R, you can execute the demo() and help()
commands from the R shell. Lastly, the demo(graphics)
command will present a demo of the graphical
capabilities of R. Figure 1 shows a screen from the
output of demo(graphics).

Using a Docker image
Nowadays, almost all software can be run as a container
using a Docker image, and R is no exception. You can
get the official R Docker image by executing docker pull
r-base – find more information about that Docker image
by visiting https://hub.docker.com/_/r-base. However,
this tutorial will use the rocker/rstudio:latest Docker
image that contains R as well as RStudio. You can find
more information about it at https://hub.docker.
com/r/rocker/rstudio.
For reasons of simplicity, we are going to use a
docker-compose.yml file to execute the RStudio Docker
image. There will be a shared volume between the local

hesubjectofthistutorialiscreatingcalendars
usingR.TheR languageofstatistical
computingisa freeimplementationoftheS
language.Thistutorialwillshowyouhowtousea
DockerimagetoexecuteRStudioandhowtoworkwith
datesandtimesinR,beforeexplaininghowtocreate
calendars– beingabletoworkwithdatesandtimes,
andknowinghowtoextractthedesiredpartsfroma
datestringorstructureisreallyimportantfordefining
thecalendaryouwant.
We’llthenmoveontoexplaninghowtousePython 3
tocreatea calendarheatmapwithrandomdata.

InstallingR
YoucaninstallR usingyourfavouritepackagemanager
orbyfollowingtheinstructionsfoundintheR FAQ
(https://cran.r-project.org/doc/FAQ/R-FAQ.html). As
youwillsee,youcanalsorunR andRStudiousinga
Dockerimage.
YoucanentertheR shellbyexecutingR onyour
favouriteterminal.YoucanfindtheversionofR youare

Figure2:Howtoworkwithtimes,datesandlocalesinR,howtoconvert
fromepochtimeandoutputofthehelp(DateTimeClasses)command.

Build calendars with


date and time types


Mihalis Tsoukalos explains how to work with dates and times in R,


alongwitha touchofPython to draw impressive calendars.


Mihalis
Tsoukalos
is a data engineer
and a technical
writer. He is also
the author of
Go Systems
Programming
and Mastering
Go, 2nd edition.
You can reach
him at http://www.
mtsoukalos.eu
and @mactsouk.


T


R CALENDARS


OUR
EXPERT

Youcanlearn
moreabout
R atwww.r-
project.organd
aboutRStudio
athttps://
rstudio.com.
Additionally,
searchforR
packagesby
visitingCRAN
athttp://cran.r-
project.org. Figure 1: Output of the demo(graphics) command as executed in the RStudio environment that was installed and executed as a Docker image.
Free download pdf