📖 Scenario: You are working with data in R and want to convert it to JSON format to share with others or save it in a file.
🎯 Goal: Learn how to create a simple data frame, convert it to JSON using the jsonlite package, and display the JSON output.
📋 What You'll Learn
Create a data frame with exact values
Load the jsonlite package
Convert the data frame to JSON format
Print the JSON output
💡 Why This Matters
🌍 Real World
Sharing data between R and web applications or APIs often requires JSON format.
💼 Career
Data analysts and scientists frequently convert data to JSON to integrate with other tools and services.
Progress0 / 4 steps
1
Create a data frame
Create a data frame called students with two columns: name and score. The data frame should have these exact rows: 'Alice', 58, 'Bob', 75, 'Charlie', 62.
R Programming
Hint
Use data.frame() with name and score vectors. Set stringsAsFactors = FALSE to keep names as strings.
2
Load the jsonlite package
Load the jsonlite package using the library() function.
R Programming
Hint
Use library(jsonlite) to load the package before using its functions.
3
Convert data frame to JSON
Use the toJSON() function from jsonlite to convert the students data frame to JSON format and save it in a variable called students_json. Use pretty = TRUE to format the JSON nicely.
R Programming
Hint
Use students_json <- toJSON(students, pretty = TRUE) to convert and format the JSON.
4
Print the JSON output
Print the students_json variable to display the JSON string.
R Programming
Hint
Use print(students_json) to show the JSON string in the console.
Practice
(1/5)
1. What is the main purpose of the jsonlite package in R?
easy
A. To manage databases
B. To create graphical plots
C. To perform statistical tests
D. To convert R objects to JSON format and back
Solution
Step 1: Understand jsonlite's functionality
The jsonlite package is designed to handle JSON data in R, allowing conversion between R objects and JSON text.
Step 2: Identify the correct purpose
Among the options, only converting R objects to JSON and back matches jsonlite's main use.
Final Answer:
To convert R objects to JSON format and back -> Option D
Quick Check:
jsonlite = convert R and JSON [OK]
Hint: jsonlite = R to JSON and back conversion [OK]
Common Mistakes:
Confusing jsonlite with plotting packages
Thinking jsonlite manages databases
Assuming jsonlite performs statistical tests
2. Which of the following is the correct syntax to convert an R list my_list to JSON text using jsonlite?
easy
A. jsonlite::toJSON(my_list)
B. jsonlite::fromJSON(my_list)
C. toJSON(my_list, jsonlite)
D. fromJSON(my_list, jsonlite)
Solution
Step 1: Identify the function to convert R to JSON
The function to convert R objects to JSON in jsonlite is toJSON().
Step 2: Check correct usage with namespace
Using jsonlite::toJSON(my_list) correctly calls the function from the package.
Final Answer:
jsonlite::toJSON(my_list) -> Option A
Quick Check:
toJSON converts R to JSON [OK]
Hint: Use toJSON() to convert R data to JSON [OK]
Common Mistakes:
Using fromJSON() to convert R to JSON
Passing package name as argument
Mixing function names and arguments
3. What will be the output of the following R code?