Use Shiny with plotly

About plotly

Plot­ly makes it pos­si­ble to make inter­ac­tive charts with R and a num­ber of oth­er pro­gram­ming lan­guages.

The code for a plot­ly plot i struc­tured in three pieces:

  1. Code for the plot (the plot_ly() func­tion)
  2. Code for lines (the add_trace () func­tion)
  3. Code for the for­mat­ting (the lay­out() func­tion)

You can gen­er­ate a sim­ple plot by just using the plot_ly func­tion. The result will how­ev­er just be sim­ple. Below I will show you how you can gen­er­ate a plot­ly plot based on the built in Plant­Growth dataset. For the exam­ple I will not use the add_trace func­tion. It is pos­si­ble to pub­lish the graph on a web­site. First you need to sign up for an account at plotly’s web­site.

Plotly example


library(plotly)

d<-tapply(PlantGrowth$weight,PlantGrowth$group,mean)
d<-data.frame(d) plot_ly(x = rownames(d), y = d$d, type = "bar" )%>%

layout(title = "Mean plantgrowth", 
         xaxis = list(           
           title = "GROUP",
           titlefont = list(size=16)
           ),
         yaxis = list(           
           title = "MEAN GROWTH",
           titlefont = list(size=16),
           ticks="outside",
           ticklen=7,
           zeroline=TRUE,
           showline=TRUE
           )
  )

Using plotly in a shiny application

There are a few things you need to think about when mak­ing a shiny app with plot­ly.

  1. In the server.r script you have to replace ren­der­Plot with ren­der­Plot­ly
  2. In the ui.r script you have to replace plotOut­put with plot­ly­Out­put

The PlantGrowth example

ui.r

library(shiny)
library(plotly)

shinyUI(fluidPage(

             column(12,
             plotlyOutput("plot",width="50%")

             ),

        fluidRow(
            column(4,
                   h3("Help"),
                   br(),
                   helpText("Change the means to render a new plot.")

            ),

            column(4,                 
                      sliderInput("ctrl", 
                                  "Mean of ctrl:", 
                                  min = 0,
                                  max = 20, 
                                  step=0.1,
                                  value = 5.04),
                      sliderInput("trt1", 
                                  "Mean of trt1:", 
                                  min = 0,
                                  max = 20,
                                  step=0.1,
                                  value = 4.7),
                      sliderInput("trt2", 
                                  "Mean of trt2:", 
                                  min = 0,
                                  max = 20,
                                  step=0.1,
                                  value = 5.5)
              )
            )

        )

)

server.r

library(shiny)
library(plotly)

shinyServer(function(input, output) {

  output$plot <- renderPlotly({

    d<-tapply(PlantGrowth$weight,PlantGrowth$group,mean)
    d<-data.frame(d)

    p<-plot_ly(x = rownames(d), y = c(input$ctrl,input$trt1,input$trt2),
               type ="bar" 
    )

    p<-layout(p,title = "Mean plantgrowth", 
              xaxis = list(           
                title = "GROUP",
                titlefont = list(size=16)
              ),
              yaxis = list(           
                title = "MEAN GROWTH",
                titlefont = list(size=16),
                ticks="outside",
                ticklen=7,
                zeroline=TRUE,
                showline=TRUE
              )
    )
    p

  })

})

Jes­per Mar­tins­son is a phd in marine ecol­o­gy with many years of expe­ri­ence with­in sta­tis­ti­cal analy­ses and mod­el­ing using R.