3

I built a shiny app which takes a CSV file uploaded by the user and adds headers to it and a couple of new columns, in order to do some calculation afterwards.

The uploaded CSV file consists in 2 columns like this:

1  0.21
1  0.20
1  0.23
2  0.40
2  0.42
2 ...

To upload the file I use this code (which is working):

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

Next step would be add the headers to this two columns and add two more columns in this way:

Dose  Response SquareRoot    Log  
1     0.21     sq(Response)  log(Response)
1     0.20     ...           ...
1     0.23     ...           ...
2     0.40     ...           ...
2     0.42     ...           ...
2 ...

In an R session the code I would use is this:

  data1<- read.csv(file=inFile$datapath,header =input$header,sep=input$sep)
  colnames(data1) <-c("Dose","Response")
  data1$ResponseLog <- log10(data1$Response + 1)
  data1$ResponseSqRoot <- sqrt(data1$Response + 1)

If I do this in shiny adding these lines into my Rshiny app it won't work and give me the error: ERROR:argument of length 0 even if I just define the column names using colnames().

So my question is, is there a way to edit the dataframe I just uploaded? I this question has already been asked could you redirect me there because I couldn't find the solution. I hope I gave you enough details to understand the problem.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Daniele Avancini
  • 899
  • 1
  • 10
  • 14
  • 1
    Check out this quetion - I think it may help http://stackoverflow.com/q/20201070/2679518 – John Paul Jan 09 '15 at 13:41
  • So, first I upload the file using `reactiveFileReader()` and then I create a new object which will allow me to modify the dataframe. Am I right? – Daniele Avancini Jan 09 '15 at 13:54
  • Basically yes, you would make the data.frame with the correct column names. Then you would reactively add the uploaded data to the columns. Then you can do any calculations etc. – John Paul Jan 09 '15 at 14:04

1 Answers1

7

Hope this helps: I added a button to it too

rm(list = ls())
library(shiny)

ui = fluidPage(
    sidebarPanel(
      fileInput('file1', 'Choose file to upload',accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),'Comma'),
      radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'Double Quote'),
      actionButton("Load", "Load the File"),width = 3),
    mainPanel(tableOutput("my_output_data"))
)

server = function(input, output) {

  data1 <- reactive({
    if(input$Load == 0){return()}
    inFile <- input$file1
    if (is.null(inFile)){return(NULL)}

    isolate({ 
    input$Load
    my_data <- read.csv(inFile$datapath, header = input$header,sep = input$sep, quote = input$quote,stringsAsFactors =FALSE)
    colnames(my_data) <-c("Dose","Response")
    my_data$ResponseLog <- log10(my_data$Response + 1)
    my_data$ResponseSqRoot <- sqrt(my_data$Response + 1)
    })
    my_data
  })
  output$my_output_data <- renderTable({data1()},include.rownames=FALSE)  

}
runApp(list(ui = ui, server = server))
Pork Chop
  • 28,528
  • 5
  • 63
  • 77