Published
Edited
Mar 13, 2019
Insert cell
//See requ
//https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//a function that is returning a promise once something is resolved
Insert cell
resolveAfter2Seconds = function(number) {
return new Promise(resolve => {
setTimeout(() => {
resolve(number)
}, 2000)
})
}
//this promise is waiting 2 seconds then printing something on my console
//set Timeout executes a function after the time set
//above resolveing the value of x after 2 seconds (2000)
Insert cell
f1 = async function(number) {
var x = await resolveAfter2Seconds(number); //note why we need the await here
console.log(x)
return x
}
//then create a new promise with a resolved( if a twitter API actually returns tweets) variable
//will wait for teh result of my function sychronously
//async telling js that that function below will be a asynchronous function - things will get executed at different times.
//if we didnt have this function here the x returned would be undefined becasue the resolve function has not caught up yet
Insert cell
f1(8)
Insert cell
Insert cell
Insert cell
Insert cell
vegalite = require("@observablehq/vega-lite@0.1")
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
Insert cell
// Load a file locally
viewof text = html`<input type=file * ">`
Insert cell
tweetsStrings = Files.text(text)
Insert cell
tweetsDF = Object.values(JSON.parse(tweetsStrings))
Insert cell
Insert cell
count = {
let countArray = []
let groupedLocations = z.groupBy(x => x.location, tweetsDF)
for (const key of Object.keys(groupedLocations)) {
countArray.push({location: key, count: groupedLocations[key].length})
}
return countArray
}
Insert cell
Insert cell
countSorted = z.sortByCol('count', 'des', count)
Insert cell
vegalite({
data: {values: countSorted.slice(0,40)},
mark: "bar",
encoding: {
x: {field: "location", type: "nominal", sort:'*'},
y: {field: "count", type: "quantitative"},
color: {"field": "location", "type": "nominal"}
}
})
Insert cell
Insert cell
notNull = z.filter(r => r.lat != null, tweetsDF)
Insert cell
vegalite({
data: {values: notNull},
mark: "point",
encoding: {
x: {field: "lon", type: "quantitative"},
y: {field: "lat", type: "quantitative"}
}
})
Insert cell
Insert cell
// Loop through all the locations, and see if they are similar to Boston, then create a new column
newTweets = {
let newLocations = []
let locations = z.getCol("location", tweetsDF)
for (let id in locations) {
if (locations[id].toLowerCase().includes('boston')){
newLocations.push('Boston, MA')
} else {
newLocations.push(locations[id])
}
}
return z.addCol("cleanLocations", newLocations, tweetsDF)
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more