toJSON()
Apart from converting JSON to R with fromJSON(), you can also use toJSON() to convert R data to a JSON format. In its most basic use, you simply pass this function an R object to convert to a JSON. The result is an R object of the class json, which is basically a character string representing that JSON.
For this exercise, you will be working with a .csv file containing information on the amount of desalinated water that is produced around the world. As you'll see, it contains a lot of missing values. This data can be found on the URL that is specified in the sample code.
Deze oefening maakt deel uit van de cursus
Intermediate Importing Data in R
Oefeninstructies
- Use a function of the
utilspackage to import the.csvfile directly from the URL specified inurl_csv. Save the resulting data frame aswater. - Convert the data frame
waterto a JSON. Call the resulting objectwater_json. - Print out
water_json.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# jsonlite is already loaded
# URL pointing to the .csv file
url_csv <- "https://assets.datacamp.com/production/course_1478/datasets/water.csv"
# Import the .csv file located at url_csv
# Convert the data file according to the requirements
# Print out water_json