07/06, 2022

Introduction

Where everything is

What is reproducible research?

  • Interlaced code, data (Raw) and text
  • Repos on github
  • in R: Rmarkdown (Rmd)

Reproducibility en R

  1. A folder
    • Raw data (csv, xls, html, json)
    • Code and text (Rmd, shiny)
    • Results (Manuscript, Web Page, App)

Some motivation

External Resources

Objectives

Objectives

  • Part 1: Set up the project and rmarkdown basics
  • Part 2: getting the most of your rmd by using interactive HTML
  • Part 3: Reusing code for presentations and PDfs

Part 1 Set up the project and rmarkdown basics

In this part

  • Start a new project
  • Generate your github repo
  • Generate your readme file
  • Simple html report

Start a new project and generate your github repo

usethis::use_git()
# set your credentials if you must
gitcreds::gitcreds_set()
# if you dont have a pat you can use
usethis::create_github_token()
# if you need add your PAT
usethis::edit_r_environ()
# generate your readme
usethis::use_readme_rmd()
# Edit and upload to git
usethis::use_github()

lets get the dataset

Variables:

  • lon: Longitude in decimal degrees
  • Lat: Latitude in decimal degrees
  • bio_1 to bio_19 bioclimatic variables as defined in worldclim
  • abund_sp1 to abund_sp4: abundance of species 1, 2 and 4

Basic rmarkdown

Text

link

# Title

## subtitle

*cursive*

**bold**

[link](https://stackoverflow.com/users/3808018/derek-corcoran)

Chunks

  • echo = T o F show code
  • message = T o F shows message of packages
  • warning = T o F shows or does not show warning
  • eval = T o F runs or does not run the code
  • cache = T o F saves the result or not
  • For more options check this link

Inline code

Inline

  • Code interlinked with text
  • To update means, maximum, minimum
  • p-values, statistical differences
  • They can be vectors, and not tables.

State the objective of the project in your readme

  • include a chunk
  • include an inline code
  • include a graph
  • try to include a table
05:00

First look at the dataset

First look at the dataset (cont.)

First html report

More advanced rmarkdown

Second activity

  • Document in your readme the creation of this document and it’s objective
  • Generate graphs and tables for your project and make crossreferences
  • Change the style of your bibliography using csl
  • Until 10 am

Part 2: getting the most of your rmd by using interactive HTML

In this part

  • Generate interactive tables with DT
  • Generate interactive plots with plotly
  • Generate interactive maps with leaflet
  • Let them comunicate through crosstalk

The basics of DT

At its most basic you just need to do:

DT::datatable(Species)

Add some extra

Lets check the code

# Extensions to reorder columns and to add buttons
Test <- DT::datatable(Species, extensions = c('Buttons', 'ColReorder'),
          caption = 'Species abundances and explaining variables.',
          ## You can filter the data on top
            filter = "top",
          ## https://datatables.net/reference/option/dom
            options = list(dom = 'Blfrtip',
          ## you can reorder columns
                            colReorder = TRUE,
          ## You can scroll de table
                          scrollX='400px',
                           scrollY='200px',
          ## you can download or take away variables
                           buttons = c('copy', 'csv', 'excel', I('colvis')),
          ### how long you can make tables
                           lengthMenu = list(c(10,25,50,-1),
                                             c(10,25,50,"All"))))

Continued

# round the digits of these columns
Test %>%
    formatRound(columns = c("lon", "lat", "bio_1",
        "bio_2", "bio_3", "bio_4", "bio_5", "bio_6",
        "bio_7", "bio_8", "bio_9", "bio_10", "bio_11",
        "bio_12", "bio_13", "bio_14", "bio_15", "bio_16",
        "bio_17", "bio_18", "bio_19", "abund_sp1",
        "abund_sp2", "abund_sp4"), digits = 3)

The basics of plotly

  • if you know how to use ggplot, you can use plotly
Species2 <- Species %>%
    pivot_longer(cols = abund_sp1:abund_sp4, names_to = "Species",
        values_to = "Abundance") %>%
    mutate(Species = str_remove_all(Species, "abund_"),
        Species = str_replace_all(Species, "sp", "Spp "))

ggplotly

G <- ggplot(Species2, aes(x = bio_12, y = Abundance)) +
    geom_point(aes(color = Species)) + geom_smooth(aes(color = Species)) +
    theme_bw()

ggplotly(G)

The graph

More control with plotly

Even animations and control

library(gapminder)
df <- gapminder
fig <- df %>%
    plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop,
        color = ~continent, frame = ~year, text = ~country,
        hoverinfo = "text", type = "scatter", mode = "markers")

fig <- fig %>%
    layout(xaxis = list(type = "log"))

fig

The animation

Basics of leaflet

leaflet(data = Species) %>%
    addTiles() %>%
    addCircles(lng = ~lon, lat = ~lat)

Basics of crosswalk

  • you need to add a dataframe or data frame like object into a shared data:
sd <- SharedData$new(Species2)

Generate filters

crosstalk::filter_checkbox("Species", "Species", sd,
    ~Species)
crosstalk::filter_slider("Abundance", "Abundance",
    sd, ~Abundance, step = 10, round = T, min = min(Species2$Abundance),
    max = max(Species2$Abundance))

Why do you want to use crosstalk (Example)

Lets see in this link

Activity 3

Check the template in this link and:

  • Add to your report with interactive widgets and if you feel that it helps with your crosswalk.
  • Try with the prior exploration, and variables you feel are important, to allow the user (advisor, colleague, co-author, etc.), To help you explore the relationships.
  • When you are ready lets publish to rpubs
  • Dont forget to push to github

Part 3 Reusing code for presentations and PDFs

How to reuse code

  • To reuse code we need a script with the code commented as follows
## ---- read-dataset --------
Species <- read_csv("https://raw.githubusercontent.com/derek-corcoran-barrios/OikosRepoducibleResearch/master/Species.csv")

## ---- species2 --------

Species2 <- Species %>%
    pivot_longer(cols = abund_sp1:abund_sp4, names_to = "Species",
        values_to = "Abundance") %>%
    mutate(Species = str_remove_all(Species, "abund_"),
        Species = str_replace_all(Species, "sp", "Spp "))

Lets use our templates

  • You can find all needed templates here
  • The file ReusableChunks.R has the chunks needed to run the code
  • Every rmd needs to have the following code
knitr::read_chunk("ReusableChunks.R")
  • Modify the document ReuseDocument.Rmd and ReusableChunks.R, to include models, and predictions
  • Fix the issue in ReuseDocument.Rmd to get the crossreference of the figure to work
  • Try to generate a coherent presentation
  • Lets work until we are finished

Thank you