1

Please find the code below for a App using , I do not know how to sort them inside the server.R code.

WIth the below code I am able to display the bar chart and change it but ordering the data seems to be an issue.

ui.R

library(shiny)
library(ggplot2)


shinyUI(fluidPage(

    column(12,offset=5,
    titlePanel("Template Type by Hours")),
    br(),
    h6(textOutput("text1")),

    fluidRow(

        column(4,offset=0,
               wellPanel(
                   selectInput("var","Hours",
                               choices = colnames(sum1[2:8]),selected ="hrs_0to1")))),

        column(12,offset=0,
                plotOutput("stack", height=550,width=1300)
            ),

        column(12,
               dataTableOutput("table1")
               )

    ))

server.R

library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
a<-theme(panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank(),
         panel.background=element_rect(fill="white",colour=NA),
         axis.ticks=element_blank(),axis.text.x = element_text(angle = 80, vjust = 1, hjust=1),
         legend.position="none")

#cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#sum1<-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")
#sum1<-sum1[,-9]

shinyServer(function(input,output){

        output$text1<- renderText({
        paste("Template 1 is:",input$var)})

        output$stack<-renderPlot({

        p<-ggplot(data = sum1,aes_string("TicketType",input$var,fill="TicketType"))+
        geom_bar(stat="identity") +
        scale_colour_continuous(low="#56B4E9",high ="#009E73") + ggtitle("Distribution for Templates")+
        geom_text(aes_string(label=input$var),size=3.5,vjust=1,colour="black")

        print(p+a)})

    output$table1<-renderDataTable({sum1})

    })

Data Structure

> str(sum1)
'data.frame':   37 obs. of  8 variables:
 $ TicketType : Factor w/ 37 levels "Address change",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ hrs_0to1   : num  4.04 21.39 0.14 24.95 0 ...
 $ hrs_1to6   : num  3.08 32.03 0.18 9.3 0 ...
 $ hrs_6to12  : num  3.06 23.68 0.28 19.5 0.09 ...
 $ hrs_12to24 : num  2.5 18.07 0.09 6.02 0.05 ...
 $ hrs_24to48 : num  1.32 7.41 0.11 1.43 0.08 0.64 0.08 1.99 0 4.74 ...
 $ hrs_48to96 : num  0.76 3.31 0.24 0.27 0 2.25 0.27 1.46 0.09 5.38 ...
 $ hrs_above96: num  0 1.97 0 0 0 3.95 0.66 3.29 0 3.29 ...

enter image description here

Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
  • As far as I know you cannot sort in ggplot2 directly, you have to reorder the factor. Doesn't that work in your case? – oliver13 Aug 12 '14 at 07:48
  • You can try sorting the variable hrs_0to1 first before plotting – Keniajin Aug 12 '14 at 07:51
  • It doesn't,because in the shiny code my variable is `input$var` which must be used to order the data and I do not know how to use `input$var` and order data. – Shoaibkhanz Aug 12 '14 at 07:54
  • http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph – oliver13 Aug 12 '14 at 08:02
  • `sum2$TicketType<- reorder(sum2$TicketType, -sum2$hrs_0to1) p<-ggplot(data = sum2,aes_string("TicketType","hrs_0to1",fill="TicketType"))+ geom_bar(stat="identity") print(p)` With this code it works, but the problem is I need to make hrs_0to1 variable which I do not know. – Shoaibkhanz Aug 12 '14 at 08:27
  • The only thing you need to change is the -sum2$hrs_0to1 part, as this is variable. You can just use -sum[,input$var] to make it dynamic – oliver13 Aug 12 '14 at 11:37
  • @Shoaibkhanz: I am facing exactly the same problem, did you find a solution? Thanks – ChriiSchee Sep 26 '16 at 20:03

1 Answers1

2

You just have to use the sorting as in:

sum2$TicketType<- reorder(sum2$TicketType, -sum2[,input$var])

This works just fine for me.

# server.R

library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
a<-theme(panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank(),
     panel.background=element_rect(fill="white",colour=NA),
     axis.ticks=element_blank(),axis.text.x = element_text(angle = 80, vjust = 1, hjust=1),
     legend.position="none")

#cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#sum1<-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")
#sum1<-sum1[,-9]

sum1 <- data.frame(TicketType=c("Test1", "Test2", "Test3", "Test4"),hrs_0to1=c(4.04,21.39,0.14,24.95), hrs_1to6=c(3.08, 32.03, 0.18, 9.3))

shinyServer(function(input,output){

output$text1<- renderText({
paste("Template 1 is:",input$var)})

output$stack<-renderPlot({

sum1$TicketType<- reorder(sum1$TicketType, -sum1[,input$var])

p<-ggplot(data = sum1,aes_string("TicketType",input$var,fill="TicketType"))+
  geom_bar(stat="identity") +
  scale_colour_continuous(low="#56B4E9",high ="#009E73") + ggtitle("Distribution for Templates")+
  geom_text(aes_string(label=input$var),size=3.5,vjust=1,colour="black")

print(p+a)})

output$table1<-renderDataTable({sum1})

})
oliver13
  • 996
  • 2
  • 7
  • 19