Platform
Resources
Pricing
Sign in
Get started
Yichun Fan
Workspace
Fork
Published
By
Yichun Fan
Edited
Mar 30, 2019
Insert cell
Insert cell
Insert cell
Insert cell
//API: make the inquiry and will return information in a given structure
Insert cell
Insert cell
//https://developer.twitter.com/en/docs/tweets/search/overview/standard.html
Insert cell
Insert cell
Insert cell
resolveAfter2Seconds
=
function
(
x
)
{
return
new
Promise
(
resolve
=>
{
setTimeout
(
(
)
=>
{
resolve
(
x
)
//resolve if the API successfully returned tweets
//or it might be rejection
}
,
2000
)
}
)
}
Insert cell
f1
=
async
function
(
number
)
{
//async: executing at different times, have a delay, not syncrocize, don't execute anything else before we successfully executed it.
var
x
=
await
resolveAfter2Seconds
(
number
)
;
//waiting 2 seconds
console
.
log
(
x
)
return
x
}
Insert cell
f1
(
5
)
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.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
resolveAfter2Seconds
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
f1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
vegalite
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
z
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
text
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tweetsStrings
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tweetsDF
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
count
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
countSorted
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
notNull
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
newTweets
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML