Home > front end >  Why does write.csv transform 4 columns to 1 column in R shiny?
Why does write.csv transform 4 columns to 1 column in R shiny?

Time:12-15

I am currently working on a project using in which I need to import an export a data frame as a .csv file. Here is the code:

ui.R

fileInput(
  "file1",
  "Importer depuis un fichier",
  accept = c(
    "text/csv",
    "text/comma-separated-values,text/plain",
    ".csv")
),
rHandsontableOutput("tabl")
downloadButton(
  "imprCsv",
  "Imprimer en CSV"
)

server.R

reactDataBase <- reactiveValues(data = dataBase)
output$tabl <- renderRHandsontable({
  rhandsontable(reactDataBase$data, useTypes = TRUE, stretchH = "all") %>%
  hot_col("Pièce", readOnly = TRUE) %>%
  hot_col("Désignation", readOnly = TRUE)
})

observe({
  inFile <- input$file1
  if(is.null(inFile)) {return(NULL)}
  reactDataBase$data<<-read.table(file=inFile$datapath, header=TRUE, sep=";", na.string="")
})

output$imprCsv <- downloadHandler(
  filename = function() {paste0("Rapport ", str_replace_all(Sys.time(), ":", "_"), ".csv")},
  content = function(file) {
    print(reactDataBase$data)
    write.csv(reactDataBase$data, file)
  }
)

The problem, here is that the csv file isn't of type

    Piece   Name   Value   Minim
1   P2      Picj   12      TRUE
2   P5      Picj   23      TRUE
3   P6      Picj   11      TRUE
4   C15     Calg   9       TRUE
5   X34     Xanol  878     TRUE

But it looks more like:

,"Piece","Name","Value","Minim"
1,"P2","Picj","12","TRUE"
2,"P5","Picj","23","TRUE"
3,"P6","Picj","11","TRUE"
4,"C15","Calg","9","TRUE"
5,"X34","Xanol","878","TRUE"

Any solution?

CodePudding user response:

Ok, I found the solution.

I just used write.csv2 instead of write.csv

  • Related