Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created October 27, 2015 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalapic/9a071fc5f99b7c1e838a to your computer and use it in GitHub Desktop.
Save jalapic/9a071fc5f99b7c1e838a to your computer and use it in GitHub Desktop.
dimple & shiny
library(shiny)
library(dplyr)
library(rcdimple)
library(htmltools)
## INTERACTIVE APP
ex_data <- read.delim(
"http://pmsi-alignalytics.github.io/dimple/data/example_data.tsv"
)
#eliminate . to avoid confusion in javascript
colnames(ex_data) <- gsub("[.]","", colnames(ex_data))
server <- shinyServer(function(input, output) {
output$distPlot <- renderDimple({
dimple(
# input$ChoiceA~UnitSales, #cannot get input working
OperatingProfit~UnitSales,
groups = c("SKU","Channel"),
data = ex_data,
type = "bubble"
) %>%
xAxis( type = "addMeasureAxis" ) %>%
yAxis( type = "addMeasureAxis" ) %>%
add_legend()
})
}
)
ui <- shinyUI(fixedPage(
titlePanel("title-title"),
fixedRow(
column(width = 2,
selectInput("ChoiceA", "ChoiceA:", choices = c("OP"="OperatingProfit",
"GP"="GrossProfit",
"P"="Price"), selected="OperatingProfit")
),
column(width = 10,
mainPanel(
dimpleOutput("distPlot", height = 700, width = 700)
)
)
)
)
)
shinyApp(ui = ui, server = server)
@jalapic
Copy link
Author

jalapic commented Oct 27, 2015

Is there a way to directly put the input (input$ChoiceA) into the dimple function within the renderDimple function. I'm trying to dynamically change the y-axis using one input.

@timelyportfolio
Copy link

really need to improve this with some lazy eval and dynamic formula handling, but here is how to make it work.

library(shiny)
library(dplyr)
library(rcdimple)
library(htmltools)

## INTERACTIVE APP 

ex_data <- read.delim(
  "http://pmsi-alignalytics.github.io/dimple/data/example_data.tsv"
)
#eliminate . to avoid confusion in javascript
colnames(ex_data) <- gsub("[.]","", colnames(ex_data))


server <- shinyServer(function(input, output) {

  output$distPlot <- renderDimple({


    dimple(
      y = input$ChoiceA,
      x = "UnitSales",  #cannot get input working
      #OperatingProfit~UnitSales,
      groups = c("SKU","Channel"),
      data = ex_data,
      type = "bubble"
    ) %>%
      xAxis( type = "addMeasureAxis" ) %>%
      yAxis( type = "addMeasureAxis" ) %>%
      add_legend()


  })


}
)


ui <- shinyUI(fixedPage(

  titlePanel("title-title"),

  fixedRow(
    column(width = 2,  

           selectInput("ChoiceA", "ChoiceA:", choices = c("OP"="OperatingProfit",  
                                                          "GP"="GrossProfit", 
                                                          "P"="Price"), selected="OperatingProfit")
    ),        

    column(width = 10,      

           mainPanel(
             dimpleOutput("distPlot", height = 700, width = 700)
           )
    )
  )



)
)


shinyApp(ui = ui, server = server)

@jalapic
Copy link
Author

jalapic commented Oct 27, 2015

Thanks - the above code is great. Another question - Is it possible to adjust font size of the titles of axes ?

Here's some updated code:

library(shiny)
library(dplyr)
library(rcdimple)
library(htmltools)

## INTERACTIVE APP 

ex_data <- read.delim(
  "http://pmsi-alignalytics.github.io/dimple/data/example_data.tsv"
)

colnames(ex_data) <- gsub("[.]","", colnames(ex_data))#eliminate . to avoid confusion in javascript



server <- shinyServer(function(input, output) {

  output$distPlot <- renderDimple({

    #groupvars <- input$checkGroup

    dimple(
      y = input$ChoiceA,
      x = "UnitSales",
      groups = input$checkGroup,
      data = ex_data,
      type = "bubble"
    ) %>%
      xAxis( type = "addMeasureAxis"
             , fontSize = "120%"
             , fontFamily = "monospace") %>%
      yAxis( type = "addMeasureAxis" 
             , fontSize = "120%"
             , fontFamily = "monospace") %>%
      add_legend()


  })


}
)


ui <- shinyUI(fixedPage(

  titlePanel("title-title"),

  fixedRow(
    column(width = 2,  

           selectInput("ChoiceA", "ChoiceA:", choices = c("OP"="OperatingProfit",  
                                                          "GP"="GrossProfit", 
                                                          "P"="Price"), selected="OperatingProfit")

           ,

           checkboxGroupInput("checkGroup", label = h3("Include Group"), 
                              choices = list("1. SKU" = "SKU", "2. Channel" = "Channel", "3. Brand" = "Brand"),
                              selected = c("SKU", "Channel"))



    ),        

    column(width = 10,      

           mainPanel(
             dimpleOutput("distPlot", height = 700, width = 900)
           )
    )
  )



)
)


shinyApp(ui = ui, server = server)

I've tried variations of adding html in to the xAxis parameter:

e.g. html ="<b style = 'font-size:130%;'></b>" but I'm obviously off-target.

@jalapic
Copy link
Author

jalapic commented Oct 28, 2015

This was my other attempt:

ui <- shinyUI(fixedPage(


  tags$head(
    tags$style(HTML("
                    axis-label {
                    font-size: 20px;
                    }
                    "))),
...

I also tried other variations on axis-label in case I wasn't referring accurately, but not getting it

@timelyportfolio
Copy link

Looking into this further, this is very strange, since the font-size is being applied to the title, but has a different effect. I'm guessing my ignorance of the finer points of CSS is to blame. For now, to fix, you can do something like this.

library(shiny)
library(dplyr)
library(rcdimple)
library(htmltools)

## INTERACTIVE APP 

ex_data <- read.delim(
  "http://pmsi-alignalytics.github.io/dimple/data/example_data.tsv"
)

colnames(ex_data) <- gsub("[.]","", colnames(ex_data))#eliminate . to avoid confusion in javascript



server <- shinyServer(function(input, output) {

  output$distPlot <- renderDimple({

    #groupvars <- input$checkGroup

    d1 <- dimple(
      y = input$ChoiceA,
      x = "UnitSales",
      groups = input$checkGroup,
      data = ex_data,
      type = "bubble"
    ) %>%
      xAxis( type = "addMeasureAxis"
             , fontSize = "120%"
             , fontFamily = "monospace") %>%
      yAxis( type = "addMeasureAxis" 
             , fontSize = "120%"
             , fontFamily = "monospace") %>%
      add_legend()

    d1$x$options$tasks <- list(htmlwidgets::JS(
'
function(){
  d3.select(this).selectAll(".dimple-axis.dimple-title")
    .style("font-size","200%")
}
'
    ))

    d1
  })


}
)


ui <- shinyUI(fixedPage(

  titlePanel("title-title"),

  fixedRow(
    column(width = 2,  

           selectInput("ChoiceA", "ChoiceA:", choices = c("OP"="OperatingProfit",  
                                                          "GP"="GrossProfit", 
                                                          "P"="Price"), selected="OperatingProfit")

           ,

           checkboxGroupInput("checkGroup", label = h3("Include Group"), 
                              choices = list("1. SKU" = "SKU", "2. Channel" = "Channel", "3. Brand" = "Brand"),
                              selected = c("SKU", "Channel"))



    ),        

    column(width = 10,      

           mainPanel(
             dimpleOutput("distPlot", height = 700, width = 900)
           )
    )
  )



)
)


shinyApp(ui = ui, server = server)

Alternately, if you want to use tags$head, you can style with the CSS selector .dimple-axis.dimple-title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment