Lesson 22: Changing Format
Lesson 22: Changing Format
Objective:
Students will learn how to convert XML files to the more familiar data table format and vice versa.
Materials:
-
There and Back Again: From XML to Data Tables handout (LMR_U3_L22_A)
Note: An electronic copy should be provided to students (see step 3 in lesson)
-
There and Back Again: From Data Tables to XML handout (LMR_U3_L22_B)
Essential Concepts:
Essential Concepts:
Converting XML to spreadsheet format helps us better understand and view our data.
Lesson:
-
Take a few minutes to compare the default views of XML code to HTML code (refer to steps 14 and 16 from Lesson 21).
-
Inform students that in today’s lesson, they will learn how to translate information from XML code into a data table.
-
Distribute the There and Back Again: From XML to Data Tables handout (LMR_U3_L22_A) to students.
Note: Provide an electronic copy of LMR_U3_L22_A for students to easily copy/paste the code later in the lesson.
-
Inform the students that XML code is provided on page 1 of the handout. Their goal is to translate the XML code in RStudio into a readable table. They will then transfer all of the information to the empty data table in the handout.
Note: It may be helpful to create a new RScript for the process.
-
Load the XML package by running the following code:
library("XML")
-
Next, assign the data the name
volunteers
and copy/paste the code within quotes. Verify that it is in your Environment. -
Ask students: "How does our data look? Can we analyze it?" Answers will vary but students should notice that RStudio/Posit Cloud is displaying it in XML format (
\n
means new line). -
We need to translate our XML data into something that RStudio recognizes. The
xmlParse()
function will do this for us. Run the following line of code to translate our data into something RStudio/Posit Cloud will recognize.volunteers <- xmlParse(volunteers)
-
Ask students: "How is RStudio recognizing our
volunteers
data now?" Answers will vary but students should see that RStudio/Posit Cloud recognizes our data as an XML file. Their Environment pane should show the data as an 'XMLInternalDocument'. -
We're getting closer - RStudio now recognizes our data as an XML file. Let's do one final translation by using the
xmlToDataFrame()
function to translate our xml file into an R data frame. Run the code below:volunteers <- xmlToDataFrame(volunteers)
-
Ask students: "What happened when we translated our xml file into an R data frame?" Answers will vary but the data should look familiar now - it displays in the Environment pane like datasets we have worked with throughout the year (
cdc
,atus
,slasher
, andtitanic
). -
Have students
View
thevolunteers
data and fill in their table in LMR_U3_L22_A. -
Provide time for students to complete the handout individually.
-
Then, conduct a whole class discussion regarding student responses to the questions on page 2 of the handout.
-
Distribute the There and Back Again: From Data Tables to XML (LMR_U3_L22_B) to student teams and allow them time to complete it.
-
Once teams have finished, teams will guide you to write the correct XML code.
-
Using a Whip Around, teams will tell you the first line of the XML code you need to write. Teams waiting their turn will check if the team is guiding you correctly. If not, they need to stop you and propose their line of code. Do not continue writing the lines of code until all teams agree.
Class Scribes:
One team of students will give a brief talk to discuss what they think the 3 most important topics of the day were.
Homework & Next Day
Students will continue to collect data using the class’s Participatory Sensing campaign (see Lessons 17- 19). They will analyze the data the next day during the What Does Our Campaign Data Say? Practicum.