Reputation: 979
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.
Upvotes: 3
Views: 4964
Reputation: 29387
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))
Upvotes: 7