Reputation:
csvToJSON <- function (fileUrl, fileName) {
library(rjson)
data <- read.csv(fileUrl)
jsonData <- toJSON(as.list(data))
fileName <- paste0(fileName, ".json")
write(jsonData, cat(fileName))
}
I created this function to convert csv files into JSON files. For the csvToJSON function that I had, one of the input parameters is fileName, and I want that to be the name of the json file to be outputted, so I have passed fileName to the write() function at the end. However, the write() function requires it to be in the form of write(jsonData, "fileName"). How can I solve this problem? In another word, how can I add quotation marks over the input fileName?
Upvotes: 0
Views: 49
Reputation: 388862
To pass unquoted column names and use it as a string in the function we can use deparse
and substitute
csvToJSON <- function (fileUrl, fileName) {
fileName <- deparse(substitute(fileName))
data <- read.csv(fileUrl)
jsonData <- toJSON(as.list(data))
fileName <- paste0(fileName, ".json")
write(jsonData, fileName)
}
csvToJSON("data.baltimorecity.gov/api/views/dz54-2aru/", jsonData)
Upvotes: 1