4

I am trying to create a dashboard in shiny with 2 visualizations (datatable and bargraph) that will allow the user to apply the same filters to both visualizations simultaneously. I tried using the solution shown here but I guess I am missing something. "Data5" is the name of the data frame I am using to populate. Any help would be appreciated.

server.R

library(shiny)
library(ggplot2)
library(dplyr)

dt <- data5

shinyServer(function(input, output) {


data <- reactive({data5
    if (input$year != "All"){
      data <- data[data5$year == input$year,]
    }
   if (input$month != "All"){
      data <- data[data5$month == input$month,]
    }
   if (input$partner_name != "All"){
      data <- data[data5$partner_name == input$partner_name,]
    }
    if (input$cube_title != "All"){
      data <- data[data5$cube_title == input$cube_title,]
    }

  })

  #plots
  output$table1 <- renderDataTable({
    data
  })

  output$plot1 <- renderPlot({
    ggplot(data=subset(data5,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads")
  })
  output$plot2 <- renderPlot({
    ggplot(data=subset(data5,cube_title==c("CommunityProfileViews","HomeProfileViews")), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Profile Views") + ggtitle("Profile Views")

  })

})

ui.R

dashboardPage(
  skin = "blue",
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
     ( 
          selectInput("year", 
                      "Year:", 
                      c("All", 
                        unique(as.character(data5$year))))
      ),
      ( 
          selectInput("month", 
                      "Month:", 
                      c("All", 
                        unique(as.character(data5$month))))
      ),
      (
          selectInput("partner_name", 
                      "Partner:", 
                      c("All", 
                        unique(as.character(data5$partner_name))))
                        ),
      (
          selectInput("cube_title", 
                      "Metric:", 
                      c("All", 
                        unique(as.character(data5$cube_title))))
      )        
    ),

  dashboardBody(


     tabsetPanel(id = "tabSelected",
                tabPanel("Charts", plotOutput("plot1"), 
                box(plotOutput("plot2"))),
                tabPanel("DataTable", dataTableOutput("table1"))
    )

               )
            )
Community
  • 1
  • 1
ericbrownaustin
  • 1,230
  • 4
  • 18
  • 36

1 Answers1

4

Issue 1: reactive returns a function, not an object. So we need to call data(), not data, in your renderDataTable and renderPlot functions

Issue 2: you need to put data() in your render functions. Currently, it is calling data5, which is not reactive:

output$plot1 <- renderPlot({
    Temp <- data()
    ggplot(data=subset(Temp,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads")
}) 

Issue 3: Your reactive function returns an assignment call. I would do this more like:

data <- reactive({ #The data5 that was here is not necessary
  temp <- data5 
  if (input$year != "All"){
    temp <- temp[temp$year == input$year,]
  }
  if (input$month != "All"){
    temp <- temp[temp$month == input$month,]
  }
  if (input$partner_name != "All"){
    temp <- temp[temp$partner_name == input$partner_name,]
  }
  if (input$cube_title != "All"){
    temp <- temp[temp$cube_title == input$cube_title,]
  }
  return(temp)

})

so that now when we call data(), we return our filtered data.frame

Issue 4: (albeit less of an issue) I would avoid using data as an object name. It means something else and can often get confusing when reading code. Use the safer DT or something else. You can always check if a name is in use by typing exists("NameToCheck") in the console.

Suggested Reading

Chris
  • 6,302
  • 1
  • 27
  • 54
  • Still doesn't seem to work when calling a function instead of an object. When I tried, I got an error of object "cube_title" not found. I am not sure if I added your suggestions correctly. **server.ui** `datainput <- reactive({ if (input$year != "All") {data5[data5$year == input$year,]...
    output$plot1 <- renderPlot({` ggplot(data=subset(datainput(),cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads") })`
    – ericbrownaustin Dec 17 '15 at 22:33
  • @ericb no way to know without knowing what your data is like. call `print(datainput())` within renderPlot and see what displays. If there is no column called `cube_input`, you will get the error you saw – Chris Dec 17 '15 at 23:21
  • Did that. Got a blank box in the dashboard and "NULL" in the console. – ericbrownaustin Dec 17 '15 at 23:30
  • @ericb are you no longer getting the "cube_title" error? It is very difficult for me to reproduce + troubleshoot your error without your `data5`. See: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Chris Dec 17 '15 at 23:33
  • Sorry about that. Here is a dummy example: `year <- c('2014','2015','2015','2013','2014')` `month <- c('Jan','Feb','Feb','Dec','Oct')` `partner_name <- c('1','2','3','1','3')` `cube_title <- c('leads','calls','check','store','go')` `data7 <- cbind(year,month,partner_name,cube_title)` – ericbrownaustin Dec 17 '15 at 23:46
  • @ericb maybe try `data7` in your reactive call instead of `data5` – Chris Dec 18 '15 at 14:27
  • I'm sorry, I created the dummy data with the incorrect dateframe name. I'm still using data5. – ericbrownaustin Dec 18 '15 at 16:34
  • @ericb might be an issue with ggplot trying to interpret a function. Try my edit above – Chris Dec 18 '15 at 17:22
  • I see your change, but it didn't make a difference. I am not sure why the datatable also wouldn't work if the plot was incorrectly formatted. Also, when I switch the data.frame of data5 back into the plot instead of the function of data(), it works. I don't know what I'm doing wrong. – ericbrownaustin Dec 18 '15 at 18:23
  • @ericb one more change, this time to the reactive function – Chris Dec 18 '15 at 19:42
  • Tried that. Everything now renders a plot or table, but the filters no longer work. **yikes!** – ericbrownaustin Dec 18 '15 at 20:33