<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-luxembourguish-residents-spend-their-time-a-small-flexdashboard-demo-using-the-time-use-survey-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-09-15-time_use/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://brodriguesco.shinyapps.io/time_use_luxembourg/" target="_blank"><br>
<img src="https://i2.wp.com/www.brodrigues.co/blog/2018-09-15-time_use/img/time_use_dashboard.png?w=456" data-recalc-dims="1"></a></p>
</div>
<p>In a <a rel="nofollow" href="http://www.brodrigues.co/blog/2018-09-11-human_to_machine/" target="_blank">previous blog post</a> I have showed<br>
how you could use the <code>{tidyxl}</code> package to go from a human readable Excel Workbook to a tidy<br>
data set (or flat file, as they are also called). Some people then contributed their solutions,<br>
which is always something I really enjoy when it happens. This way, I also get to learn things!</p>
<p><span>@expersso</span> proposed a solution without <code>{tidyxl}</code>:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">
Interesting data wrangling exercise in <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a>. <br>My solution (without using {tidyxl}): <a rel="nofollow" href="https://t.co/VjuOoM82yX" target="_blank">https://t.co/VjuOoM82yX</a> <a rel="nofollow" href="https://t.co/VsXFyowigu" target="_blank">https://t.co/VsXFyowigu</a>
</p>
<p>— Eric (<span>@expersso</span>) <a rel="nofollow" href="https://twitter.com/expersso/status/1039894727808757761?ref_src=twsrc%5Etfw" target="_blank">September 12, 2018</a>
</p></blockquote>
<p></p>
<p>Ben Stenhaug also proposed a solution on his <a rel="nofollow" href="https://github.com/stenhaug/shared/blob/master/tidyxl_bruno_blog.md" target="_blank">github</a><br>
which is simpler than my code in a lot of ways!</p>
<p>Now, it would be too bad not to further analyze this data. I’ve been wanting to play around with<br>
the <code>{flexdashboard}</code> package for some time now, but never really got the opportunity to do so.<br>
The opportunity has now arrived. Using the cleaned data from the last post, I will further tweak<br>
it a little bit, and then produce a very simple dashboard using <code>{flexdashboard}</code>.</p>
<p>If you want to skip the rest of the blog post and go directly to the dashboard, just click <a rel="nofollow" href="https://brodriguesco.shinyapps.io/time_use_luxembourg/" target="_blank">here</a>.</p>
<p>To make the data useful, I need to convert the strings that represent the amount of time spent<br>
doing a task (for example “1:23”) to minutes. For this I use the <code>{chron}</code> package:</p>
<pre><code>clean_data &lt;- clean_data %>%
    mutate(time_in_minutes = paste0(time, &quot;:00&quot;)) %>% # I need to add &quot;:00&quot; for the seconds else it won't work
    mutate(time_in_minutes = 
               chron::hours(chron::times(time_in_minutes)) * 60 + 
               chron::minutes(chron::times(time_in_minutes)))

rio::export(clean_data, &quot;clean_data.csv&quot;)</code></pre>
<p>Now we’re ready to go! Below is the code to build the dashboard; if you want to try, you should<br>
copy and paste the code inside a Rmd document:</p>
<pre><code>---
title: &quot;Time Use Survey of Luxembourguish residents&quot;
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

`` `{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(plotly)
library(ggthemes)

main_categories &lt;- c(&quot;Personal care&quot;,
                     &quot;Employment&quot;,
                     &quot;Study&quot;,
                     &quot;Household and family care&quot;,
                     &quot;Voluntary work and meetings&quot;,
                     &quot;Social life and entertainment&quot;,
                     &quot;Sports and outdoor activities&quot;,
                     &quot;Hobbies and games&quot;,
                     &quot;Media&quot;,
                     &quot;Travel&quot;)

df &lt;- read.csv(&quot;clean_data.csv&quot;) %>%
    rename(Population = population) %>%
    rename(Activities = activities)
`` `

Inputs {.sidebar}
-----------------------------------------------------------------------

`` `{r}

selectInput(inputId = &quot;activitiesName&quot;, 
            label = &quot;Choose an activity&quot;, 
            choices = unique(df$Activities))

selectInput(inputId = &quot;dayName&quot;, 
            label = &quot;Choose a day&quot;, 
            choices = unique(df$day), 
            selected = &quot;Year 2014_Monday til Friday&quot;)

selectInput(inputId = &quot;populationName&quot;, 
            label = &quot;Choose a population&quot;, 
            choices = unique(df$Population), 
            multiple = TRUE, selected = c(&quot;Male&quot;, &quot;Female&quot;))

`` `

The Time Use Survey (TUS) aims to measure accurately how people allocate their time across different day-to-day activities. To this end, people are asked to keep records of all their activities in a time diary. For each activity, additional information is collected about whether or not the person was alone doing it or together with other persons, where did the activity take place, etc. The main studies on time use have been conducted to calculate indicators making possible comparative analysis of quality of life within the same population or between countries. International studies care more about specific activities such as work (unpaid or not), free time, leisure, personal care (including sleep), etc.
Source: http://statistiques.public.lu/en/surveys/espace-households/time-use/index.html

Layout based on https://jjallaire.shinyapps.io/shiny-biclust/

Row
-----------------------------------------------------------------------

### Minutes spent per day on certain activities
    
`` `{r}
dfInput &lt;- reactive({
        df %>% filter(Activities == input$activitiesName,
                      Population %in% input$populationName,
                      day %in% input$dayName)
    })

    dfInput2 &lt;- reactive({
        df %>% filter(Activities %in% main_categories,
                      Population %in% input$populationName,
                      day %in% input$dayName)
    })
    
  renderPlotly({

        df1 &lt;- dfInput()

        p1 &lt;- ggplot(df1, 
                     aes(x = Activities, y = time_in_minutes, fill = Population)) +
            geom_col(position = &quot;dodge&quot;) + 
            theme_minimal() + 
            xlab(&quot;Activities&quot;) + 
            ylab(&quot;Time in minutes&quot;) +
            scale_fill_gdocs()

        ggplotly(p1)})
`` `

Row 
-----------------------------------------------------------------------

### Proportion of the day spent on main activities
    
`` `{r}
renderPlotly({
    
       df2 &lt;- dfInput2()
       
       p2 &lt;- ggplot(df2, 
                   aes(x = Population, y = time_in_minutes, fill = Activities)) +
           geom_bar(stat=&quot;identity&quot;, position=&quot;fill&quot;) + 
            xlab(&quot;Proportion&quot;) + 
            ylab(&quot;Proportion&quot;) +
           theme_minimal() +
           scale_fill_gdocs()
       
       ggplotly(p2)
   })
`` `</code></pre>
<p>You will see that I have defined the following atomic vector:</p>
<pre><code>main_categories &lt;- c(&quot;Personal care&quot;,
                     &quot;Employment&quot;,
                     &quot;Study&quot;,
                     &quot;Household and family care&quot;,
                     &quot;Voluntary work and meetings&quot;,
                     &quot;Social life and entertainment&quot;,
                     &quot;Sports and outdoor activities&quot;,
                     &quot;Hobbies and games&quot;,
                     &quot;Media&quot;,
                     &quot;Travel&quot;)</code></pre>
<p>If you go back to the raw Excel file, you will see that these main categories are then split into<br>
secondary activities. The first bar plot of the dashboard does not distinguish between the main and<br>
secondary activities, whereas the second barplot only considers the main activities. I could<br>
have added another column to the data that helped distinguish whether an activity was a main or secondary one,<br>
but I was lazy. The source code of the dashboard is very simple as it uses R Markdown. To have<br>
interactivity, I’ve used Shiny to dynamically filter the data, and built the plots with <code>{ggplot2}</code>.<br>
Finally, I’ve passed the plots to the <code>ggplotly()</code> function from the <code>{plotly}</code> package for some<br>
quick and easy javascript goodness!</p>
<p>If you found this blog post useful, you might want to follow me on <a rel="nofollow" href="https://www.twitter.com/brodriguesco" target="_blank">twitter</a><br>
for blog post updates.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.brodrigues.co/blog/2018-09-15-time_use/"> Econometrics and Free Software</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/_7gNHjXVodM" height="1"></a></div>