0

Sorry if this question sounds dumb. I have been trying to modify this shiny app hosted at: https://joejansen.shinyapps.io/citi-bike-data-with-rCharts/

Github link: https://github.com/joejansen/citi-bike-data-with-rCharts

I want to add grouped variable so that I can have correlation table with grouping. For example, if the user selects "month" then it should list correlation for each month vs "Miles traveled".

So far, I have this

Server.r

#Server.r
library(dplyr)

corTable <- reactive({
  yChoice = input$variable
  yCor = citibike_all[yChoice]

#Here we input our group
  # yGroup = input$group
  # yGroup_input = citibike_all[yGroup]
#For simplicity I selected "month" as a grouping variable but it gives me same values. 

  grouped_table <- citibike_all %>%
    group_by(month) %>%
    dplyr::summarize(., cor(citibike_all$miles, yCor , use = "complete.obs"))

  grouped_table
})

#Output
 output$corTable<-renderTable({
           corTable()
            })

In index.html

 <div id="corTable" class="shiny-html-output shiny-bound-output">

I have tried this answer on stackoverflow: Passing user specifications as arguments to dplyr within Shiny but it does not work for me. I keep getting same correlation value for all months:enter image description here

Thanks for reading this question.

Community
  • 1
  • 1
deepseas
  • 133
  • 1
  • 6
  • 15
  • I suspect the problem is you're passing in `citibike_all$miles` to `summarise`. This will be the full vector, and so not affected by your `group_by` (as opposed to passing in the locally scoped variable). Try `cor(miles, yCor...` instead. – Akhil Nair May 14 '17 at 01:36
  • @AkhilNair Thanks for the response. After trying: `cor(miles, yCor...` I get this error: "incompatible dimensions" on the web page. – deepseas May 14 '17 at 01:44
  • 2
    That's because `miles` and `yCor` are vectors of different lengths, so the correlation is undefined. I'm not totally sure what you're trying to do, but I assume it's something along the lines of `citibike_all %>% group_by(month) %>% dplyr::summarize(., cor(miles, eval(as.symbol(yChoice)) , use = "complete.obs"))` – Akhil Nair May 14 '17 at 01:55
  • This works! I have been working on this for so long. Missed this one. – deepseas May 14 '17 at 02:02

0 Answers0