adomasb
adomasb

Reputation: 506

Writing and overwriting data to file (.csv)

is it possible to write data to .csv, without downloading it? There is a downloadButton and downloadHandler, but they write data and then downloads it.

If first thing is possible, is it possible to write data to csv and overwrite. For example, I have a .csv file with values and I want to change the values in that csv just overwriting them.

The idea is that, I want to store some values in the .csv which a loaded on start up. Even though, user wants to change some of them and save it for next time, therefore I need to change them. And next time altered values are loaded.

Upvotes: 0

Views: 3623

Answers (1)

adomasb
adomasb

Reputation: 506

Idea

The problem is that I would like allow user to select some option and shiny app should remember that options on the next application start up. Firstly, my idea was just to create empty variable on launch of app: savedItems<-NULL. All the time the app is hosted, I would add items like this savedItems<<-c(savedItems,'a1','a3','a5'). However, sometimes server, on which I will host, will be restarted, so savedItems would be again initialized as empty vector. Therefore, I decided to update my idea and save values not in vector, but in file.

Updated Idea & Solution

Firstly, I initialized empty .csv file, for example upload.csv, where I want to store data. I want to read this file each time I start application (not host), therefore firstly I just read my .csv, which will only save column vector in file:

shinyServer(function(input, output, session) { uselessRead <- read.table("upload.csv",header=FALSE) useless <<- as.vector(uselessRead[,1]) ...

Then, using selectInput I select some items and add them to my vector useless. I do this by following after clicking actionButton("addUseless",...): observe({ if (input$addUseless == 0) return () } isolate({ useless<<-input$uselessItems # from selectInput("uselessItems",...) }) }) After this, my vector is overwritten (if was populated) by new selected values. So, I want these values to appear in my upload.csv. So I do this after clicking actionButton("upload",...): observe({ if (input$upload == 0) { return () } isolate ({ write.table(useless,"upload.csv", col.names=FALSE,row.names=FALSE) }) })

This will write (and overwrite everything what was inside) everything, what was assigned to useless vector.

On the next start up, my app will firstly read upload.csv so application which values should be selected and user don't have to do that every single time.

Upvotes: 1

Related Questions