2 min read

Week51 - functionplotR

Week51 - functionplotR

This Week’s Widget - functionplotR

Mauricio Poppe has built a really amazing 2d d3-js-based function plotter function-plot. I think it has all sorts of helpful uses in R, so functionplotR will be this week’s htmlwidget. Note, I released a couple days early. I hope nobody minds.

Installation

This is not on CRAN, so to install we will need some help from devtools::install_github.

devtools::install_github("timelyportfolio/functionplotR")

Examples

I was able to replicate nearly all of the fantastic examples from the function-plot JavaScript library. For the sake of brevity, I’ll only include a couple here. Go here or ?functionplot to see all of them.

One Simple Equation

# devtools::install_github("timelyportfolio/functionplotR")

library(functionplotR)

functionplot("x + 2")

Multiple Simple Functions

There a couple ways to supply functions to functionplot. Below is the easiest but least flexible.

# devtools::install_github("timelyportfolio/functionplotR")

library(functionplotR)

functionplot(c("x + 2","x"))

With Some Options

function-plot gives us lots of options. Let’s use some of them to configure our plot from above.

# devtools::install_github("timelyportfolio/functionplotR")

library(functionplotR)

functionplot(
  # use the more flexible but a little more confusing
  #  way to provide functions
  fn = list(
    list( fn = "x + 2", color = "black" ),
    list( fn = "x", color = "gray" )
  ),
  yDomain = c(-4, 4),
  xDomain = c(-4, 4),
  width = 420, 
  height = 400
)

Function with Derivative

One of my favorite features of function-plot is the ability to plot interactive derivatives. It does require you to know the derivative though. With a quick Google, I think I now remember the derivative of cos(x). It is fun to put in something other than the derivative. Try x as an example.

# devtools::install_github("timelyportfolio/functionplotR")

library(functionplotR)

functionplot(
  fn = list(
    list(
      fn = "cos(x)",
      derivative = list(
        fn = "-sin(x)",
        updateOnMouseMove = TRUE
      )
    )
  )
)

Thanks

Thanks Mauricio Poppe for the really special function-plot plotting library.

As always, thanks to

  • Ramnath Vaidyanathan and RStudio for htmlwidgets
  • all the contributors to R and JavaScript