0

I am using the following dataset: https://docs.google.com/spreadsheets/d/1C_P5xxzYr7HOkaZFfFiDhanqDSuSIrd2UkiC-6_G2q0/edit#gid=0

I am using ShinyDashboard and I have a selectInput that allows me to choose a specific type of Candy bar (in the Candy column in my data set).

How do I take that Candy selection, and then make a graph that contains the frequency for that selected candy bar for each purchase month? In my server.R, I am not sure what to have in that CandyCount reactive element.

My code is as follows:

## ui.R ##
library(shinydashboard)
library(rCharts)


dashboardPage(
  dashboardHeader(title = "Dashboard"),

  dashboardSidebar(
    width = 150,
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("bar-chart"))
    )
    ),

  dashboardBody(
    sidebarPanel(
      htmlOutput("candy")
    ),
    mainPanel(
      showOutput("plot2", "polycharts")
    )))

##server.R##
server <- function(input, output, session) { 


  output$candy<- renderUI({
    selectInput(
      inputId = "candy",
      label = "Candy: ",
      choices = as.character(unique(dataset$Candy)),
      selected = "Twix"
    )
  })


    output$plot2 <- renderChart2({
    candySelect<- input$candy
    df <- dataset[dataset$candy == candySelect,]
    p2 <- rPlot(freq~purchase_month, data = df, type = 'line')
    p2$guides(y = list(min = 0, title = ""))
    p2$guides(y = list(title = ""))
    p2$addParams(height = 300, dom = 'chart2')
    return(p2)
  })


  }
Gary
  • 2,137
  • 3
  • 23
  • 41
  • use a variable to store the chosen candy, `candyChosen <- input$candy`, then filter your `dataset` by that chosen candy. – tospig Oct 16 '15 at 00:45
  • Should all of this happen inside of the `reactive` element? – Gary Oct 16 '15 at 00:46
  • not necessarily. you can do it inside a `renderPlot` function, something like: `output$candyPlot <- renderPlot({ candyChosen <- input$candy; })` – tospig Oct 16 '15 at 00:48
  • then everytime `intput$candy` changes, the graph will update – tospig Oct 16 '15 at 00:50
  • Okay, cool. And how do you suggest that I filter the data? Can I still use `rPlot`? – Gary Oct 16 '15 at 00:52
  • You can use whatever method you like. If you have one that works in a 'normal' `R` space using `rPlot` you can use that same code in the shiny app. – tospig Oct 16 '15 at 00:54
  • Alright, so I modified my original code. Can you explain why my solution isn't working? The chart isn't showing up. – Gary Oct 16 '15 at 00:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92443/discussion-between-tospig-and-nick). – tospig Oct 16 '15 at 00:58

1 Answers1

1

If your okay with using ggplot you could do something like this:

Edited to have dynamic tooltip

## ui.R ##
library(shinydashboard)
library(shinyBS)
require(ggplot2)

dataset <- read.csv("Sample Dataset - Sheet1.csv")

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard"),

  dashboardSidebar(
    width = 150,
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("bar-chart"))
    )
  ),

  dashboardBody(
    sidebarPanel(
      htmlOutput("candy")
    ),
    mainPanel(
      uiOutput("plotUI")
    )
  ))

##server.R##
server <- function(input, output, session) { 

  output$candy<- renderUI({
    selectInput(
      inputId = "candy",
      label = "Candy: ",
      choices = as.character(unique(dataset$Candy)),
      selected = "Twix"
    )
  })

  output$plotUI <- renderUI({
    if(is.null(input$candy)) return(NULL)
    local({
      candySelect <- input$candy
      str1 <- sprintf("The candybar you selected is: %s",candySelect)
      str2 <- sprintf("More about %s <a>here</a>",candySelect)
      print (str1)
      popify(plotOutput('plot'),str1,str2)
    })

  }) 

  observeEvent(input$candy,{
    if(is.null(input$candy)) return(NULL)
    candySelect<- input$candy
    print ('plot')
    # Assuming only one entry for each mont per candybar
    d <- dataset[dataset$Candy==candySelect,]
    output$plot <- renderPlot({
      ggplot(data=d, aes(x=purchase_month,y=freq,group=Candy)) + 
      geom_line() + 
      ggtitle(candySelect)
    })
  })

}

shinyApp(ui = ui, server = server)

I guess this should work otherwise you can bind tooltips using jQuery.

RmIu
  • 4,357
  • 1
  • 21
  • 24
  • Worked perfectly. Thanks Oskar! How would I add some interactive tooltips to this ggplot? – Gary Oct 16 '15 at 12:41
  • Great! I've added a dynamically generated tool-tip that should hopefully work. – RmIu Oct 16 '15 at 14:27
  • @OskarForsmo Is it really work correctly? I just try it on my RStudio and what I see is the situation when `Popover` text shows up every second i.e when I change Twix-> KitKat a `Popover` shows up, then when I change KitKat -> Herskley a `Popover` doest not show up etc. – Nicolabo Jan 23 '16 at 11:23
  • I've edited my answer to use `renderUI` and the `popify` function, this seems to work better. – RmIu Jan 25 '16 at 08:35