how to perform a sum of values for multiple rows with the same name in r and render a chart in plotly.i have tried a couple of methods like aggregate and tapply, none of them seems to be working for me, could anyone tell me where I am going wrong.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(shiny)
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
cdata1<-data.frame(stringsAsFactors=FALSE,
names = c("a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b",
"b", "c", "c", "c", "d", "d", "d"),
values = c(12, 32, 43, 45, 21, 21, 21, 32, 43, 54, 65, 76, 87, 80, 78,
68, 68, 67, 57)
)
ui<-fluidPage(fluidRow(plotlyOutput("id1")),
fluidRow(plotlyOutput("id2"))
)
server<-function(input,output){
output$id1<-renderPlotly({
# a<-aggregate(cdata1$X2014,by=list(cdata1$States.UTs),FUN=sum)
# plot_ly(cdata1,x=cdata1$States.UTs,y=cdata1$X2014)
cdata1 %>%
group_by(grp = rleid(cdata1$names)) %>%
summarise(names = first(cdata1$names),
values = sum(cdata1$values)) %>%
ungroup %>%
select(-grp)
plot_ly(cdata1,x=cdata1$names,y=cdata1$values)
})
}
shinyApp(ui,server)