samedi 26 décembre 2015

How can I use a function in server.R using R, shiny?

I just started using shiny to design an app.

How can I design a function in server.R and use it?. For example, here is my code:

  datatable <- reactive({
    inFile <- input$file1
         if (is.null(inFile))
        return(NULL)
      read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
  })

So, here, datatable is a reactive object storing a data.frame from the .csv file.

  output$singletable <- renderDataTable({
    a <- subset(datatable(),!is.na(datatable()$actualfailureyear))
    b <- count(a,c(input$sfactor))
    c1 <- datatable()[!duplicated(datatable()$AssetID),]
    c <- ddply(.data = c1,.variables = input$sfactor,function(x){sum(x$length)})
    d <- merge(b,c,by=input$sfactor)
    e <- ddply(.data = d,.variables = input$sfactor,function(x){data.frame(failcount=x$freq,length=x$V1,failrate=x$freq/(x$V1*0.00001)/21)}

I will use the code a-e all the time, so I really need to put them into a function and use it anywhere I want. Could you please show me how to do it with more details?




Aucun commentaire:

Enregistrer un commentaire