Publish Reproducible Examples from R to Datanovia Website

Render R script using built-in data


Publish Reproducible Examples from R to Datanovia Website

This article describes how to publish a reproducible example from R to the datanovia website using the pubr package.

The goal of pubr R package is to convert reproducible R scripts and Rmd contents into a publishable HTML block. It makes it easy to share reproducible R code in (wordpress) website comments and blog posts.

You will learn many examples for publishing reproducible R scripts.



Contents:

Prerequisites

Install the pubr package:

if(require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/pubr")

Load the package:

library("pubr")

Note that, if you are using Rstudio on linux, make sure you have installed the following system dependencies that make it easy to interact with clipboard: xclip or xsel. This can be installed using for example apt-get install xclip in bash terminal.

Main requirements

  1. Include a demo data, which can be built-in data or a sample of your own data. Examples of built-in R datasets are: ToothGrowth, PlantGrowth, mtcars and iris
  2. Include commands on a strict “need to run” basis
  3. Include the so-called “session info”: pubr::render_r(session_info = TRUE)
  4. Use good R coding style

Yes, creating a great reproducible example (reprex) requires work. You are asking other people to do work too. It’s a partnership.

80% of the time you will solve your own problem in the course of writing an excellent reprex.

The remaining 20% of the time, you will create a reprex that is more likely to elicit the desired behavior in others.

Example 1: Reproducible R script using R built-in data

  1. Write a pure R script in Rstudio
  2. Select and copy the script
  3. Run pubr::render_r(). The output of the rendered R script is a HTML block, which is automatically copied into the clipboard.
  4. Paste into a website comment areas or into a blog post
# Load required package
suppressPackageStartupMessages(library(ggpubr))

# Data preparation
data("ToothGrowth")
df <- ToothGrowth
df$dose <-as.factor(df)

# Create a boxplot
ggboxplot(df, x = "dose", y = "len")

Render R script using built-in data

Example 2: Reproducible R script using data from clipboard

  1. Write a pure R script in Rstudio
  2. Copy the data from an Excel spreadsheet and paste it into R using the function pubr::paste_data()
  3. Select and copy the script
  4. Run pubr::render_r(). The output of the rendered R script is a HTML block, which is automatically copied into the clipboard.
  5. Paste into a website comment area or into a blog post
# Data preparation
df <- pubr::paste_data()

# Summary statistics
summary(df)

Render R script using data from clipboard

Example 3: Reproducible R script using external data file

  1. Write a pure R script in Rstudio
  2. Paste your data into R using the function pubr::paste_data(data_file)
  3. Select and copy the script
  4. Run pubr::render_r(). The output of the rendered R script is a HTML block, which is automatically copied into the clipboard.
  5. Paste into a website comment area or into a blog post
# Data preparation
data_file <-system.file("demo_data", "toothgrowth.txt", package = "pubr")
df <- pubr::paste_data(data_file)

# Create a boxplot
summary(df)

Example 4: Render a reproducible Rmd

  1. Write an Rmd (without yaml header) from Rstudio
  2. Select and copy the Rmd content
  3. Run pubr::render_rmd(). The output of the rendered Rmd content is a HTML block, which is automatically copied into the clipboard.
  4. Paste into a website comment area or into a blog post

Render Rmd contents



Version: Français





Comments ( 4 )

  • Darryn Knobel

    Hi. I am trying this and I get the following:
    > install.packages(“pubr”)
    Warning in install.packages :
    package ‘pubr’ is not available (for R version 3.6.3)

    • Kassambara

      As indicated in the prerequisites, you can install the pubr package as follow:

      if(require(devtools)) install.packages("devtools")
      devtools::install_github("kassambara/pubr")
  • Pumbaa

    i got the following error trying pubr::render_r()

    R-3.6.1-intel> pubr::render_r()
    Error in reprex::reprex(..., style = TRUE, advertise = FALSE, venue = "html",  : 
      unused argument (session_info = session_info)

  • Fernanda Anselmo-Moreira

    I can’t install the packages requested packages in “Prerequisites”.
    I tried this and I got the following:

    install.packages(“devtools”)
    Installing package into ‘C:/Users/Fernanda Anselmo/Documents/R/win-library/4.0’
    (as ‘lib’ is unspecified)
    — Please select a CRAN mirror for use in this session —
    tentando a URL ‘https://cran-r.c3sl.ufpr.br/bin/windows/contrib/4.0/devtools_2.3.2.zip’
    Content type ‘application/zip’ length 448412 bytes (437 KB)
    downloaded 437 KB

    package ‘devtools’ successfully unpacked and MD5 sums checked

    The downloaded binary packages are in
    C:\Users\Fernanda Anselmo\AppData\Local\Temp\RtmpS4OEUK\downloaded_packages
    > devtools::install_github(“kassambara/pubr”)
    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
    there is no package called ‘glue’

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments