2

I'm designing an R program to output different graphs of any csv file input. I am using Rstudio Shiny and ggplot2 to develop the program.

My problem involves ordering dates chronologically rather than alphabetically (which is the default apparently). Let's use this code as an example (my code is a bit different, but this is code from someone who helped me earlier):

related posts: Unable to change the graph form of my ggplot rshiny program, help me find the bug?

Sorting months in R How do you order a nominale variable. e.g month in R?

Boxplot with ggplot2 in R - returns by month

server.R

library(shiny)
library(datasets)
library(ggplot2)

X <- read.csv(file.choose())
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot",
                names(Y()) 
                )
  })

  output$opt.y <- renderUI({
    selectInput("ycolumn", "Y Column",
                names(Y()))
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- X
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(X, n = input$obs)
  })

  createPlot <- function(df, colx, coly) {
    p <- ggplot(data=df, aes(x=df[,colx],y=df[,coly]), environment = environment()) #+ geom_line(aes(x=df[,colx],y=df[,coly], group=colx))
    p <- p + geom_line(aes(group=colx)) 
    p <- p + xlab(names(df)[colx]) + ylab(names(df)[coly]) 
  }

  Y <- reactive({
    X
      })



  # create a basic plot
  output$plotBasic <- reactivePlot(function() {
    df <- Y()
    print(createPlot(df, colx=input$xcolumn, coly=input$ycolumn))

  })
})

ui.R

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
  # Application title
  headerPanel("My app!"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    numericInput("obs", "Number of observations to view:", 13),
    uiOutput("opt.x"), #dynamic UI
    uiOutput("opt.y") #value comes from Server.R    
    ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    tabsetPanel(
      tabPanel("Table", tableOutput("view")),
      tabPanel("BasicGraph", plotOutput("plotBasic"))
    )
  )
))

This can be taken care of easily with factor or as.Date functions if you started with a list that you knew of, but here I am taking in input (can assume the format is mm-yyyy) and I do not know how to set the column of x variable data to a variable. This is because the user can choose any column in the imported data as the X column.

enter image description here

Community
  • 1
  • 1
user2522217
  • 345
  • 1
  • 7
  • 20
  • 3
    (1) You got some bad advice in your previous question (see my comment on the answer there), (2) if you change the format to yyyy-mm it will sort just fine. Or you could add a day and convert to date. Or there's a yearmon class in the zoo package. – joran Jul 26 '13 at 01:56
  • Hi, in case you have not solved your question maybe this can help you http://stackoverflow.com/a/20128614/709777 I add datetime var by pasting date and time columns to dataset so it can plot chronologically – pacomet Nov 22 '13 at 07:52

0 Answers0