Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (5.3k points)

I am trying to output a data frame in R as a json file to use for highcharts plots that I am making outside R. This is what my output looks like:-


However, I want my output to look like this:-

[{name:"alpha",value:1},{name:"brave",value:2} {name:"charlie",value:3}] 

What should I do so that the names of my data frame (in this case name and value) are not put into quotes? If converting my data into a json file is not the best way, what else can/should I do?



data = tibble(name = c("alpha", "bravo", "charlie"), 

              value = c(1, 2, 3))

output = toJSON(data, dataframe="rows")

write(output, "output.txt")

1 Answer

0 votes

To remove quotes from the output data frame, you can use the regex function from the base package as follows:



json_string <- toJSON(data, dataframe="rows")

temp <- str_replace_all(json_string, '"(\\w+)"\\s*:', '\\1:')


write(temp, "output.txt")

Browse Categories