Garrick Aden-Buie
rstudio::conf(2020, "JavaScript for Shiny Users")
.dot
or [bracket]
notationconst
means can't reasign the whole objectconst stats = {pkg: 'dplyr', downloads: 893}stats.pkgstats['downloads']const propName = 'downloads'stats[propName]stats.rankstats.downloads = 893stats
{"pkg":"dplyr","downloads":893}
let pkg = 'dplyr'let downloads = 893const stats = {pkg: 'dplyr', downloads: 893}stats = {pkg, downloads}
[TypeError] Assignment to constant variable.
Object.keys()
let stats = { pkg: 'dplyr', downloads: 893, depends: ['glue', 'magrittr', 'rlang']}let future = {}stats.version = {major: 0, minor: 8, patch: 3}Object.keys(stats)Object.keys(stats.depends)Object.keys(future)
[]
let pkgs = ['dplyr', 'ggplot2', 'tidyr', 'shiny']let downloads = [893, 762, 679, 395]
let random = [1, 2, 'three']let nothingYet = []
pkgs.length
4
let pkgs = [['dplyr', 'tidyr'], 'ggplot2', 'shiny']pkgs[0]pkgs[0][1]pkgs * 1000
NaN
//propertypkgs.length// some methods don't change the arraypkgs.includes('shiny')pkgs.slice(1, 4)// some methods do!pkgs.push('glue')pkgs.pop()// some do not clearly state their intentionspkgs.sort()
["dplyr","ggplot2","shiny","tidyr"]
Object.keys(pkgs)let stats = {pkg: pkgs[0], downloads: 900}typeof pkgstypeof statsstats instanceof Objectpkgs instanceof Objectpkgs instanceof Array
true
Array.isArray(pkgs)
true
A common pattern you'll see often is to initialize an empty array []
that is built up over time.
Give yourself an empty pkgs = []
30 seconds to think of your favorite packages
using .push()
to add each package.
Follow up:
00:30
function increment(x, by) { return x + by}
function increment(x, by) { return x + by}
const increment = function(x, by) { return x + by}
function increment(x, by) { return x + by}
const increment = function(x, by) { return x + by}
function(x, by) { return x + by}
(function() {...})()
js4shiny::repl_js()
function increment(x, by) { return x + by}increment(1, 2)
const name = function()
const increment = (x, by) => { return x + by}increment(30, 12)
function increment(x, by) { return x + by}
~ { .x + by }
const inc = (x, by) => x + by
Create cities
: an array of objects with the city name and age for
Write a function updateCityAge
that takes a city
and age
and updates the document
In the browser, use the array and function to switch between San Francisco and Los Angeles.
03:00
repl_example("first-page-04")
const cities = [ {city: 'San Francisco', age: 38.8}, {city: 'Los Angeles', age: 35.9}]function updateCityAge(city, age) { console.log( `${city} residents are ~${age} years old` )}updateCityAge(cities[0].city, cities[0].age)updateCityAge(cities[1].city, cities[1].age)
You need
You need
element.addEventListener('click', function (event) {
// ... do things ...
// ... possibly info in event
})
<button id="next-city" value="0">Next City</button>
updateCityAge(cities[0])
const btn = document.getElementById('next-city')btn.addEventListener('click', function(event) { console.log(event)})
event.target
btn.addEventListener('click', function(event) { console.log(event.target.value)})
btn.addEventListener('click', function(event) { let btnValue = event.target.value console.log(cities[btnValue])})
Note we could have done btn.value
to get the value, too
What happens if you just do btnValue + 1
?
btn.addEventListener('click', function(event) { let btnValue = event.target.value btnValue = Number(btnValue) + 1 btn.value = btnValue updateCityAge(cities[btnValue])})
btn.addEventListener('click', function(event) { let btnValue = event.target.value btnValue = Number(btnValue) + 1 if (btnValue >= cities.length) { btnValue = 0 } btn.value = btnValue updateCityAge(cities[btnValue])})
[ {city: 'Fresno', age: 31.8}, {city: 'Santa Rosa', age: 41.4}]
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
const citiesAsJson = JSON.stringify(cities); citiesAsJson
[{"city":"San Francisco","age":38.8},{"city":"Los Angeles","age":35.9}]
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
JSON.parse(citiesAsJson)[0]
{"city":"San Francisco","age":38.8}
Run repl_example("first-page-07")
I added JSON data inside <script id="data-cities">
Find the element and save its .textContent
to a variable
Use JSON.parse()
to convert the JSON string to data in place of cities
.
Watch out! The data calls age median_age
, make sure you update your function.
Bonus: Search MDN for destructuring assignment and use the Object destructuring section to learn how to assign a property to a new variable name
Bonus: Add median_home_price
to your page, too.
05:00
Our index.html
is getting crowded.
style.css
<head> <link href="style.css" rel="stylesheet" /></head>
script.js
// last thing in <body><script src="script.js"></script>
repl_example("first-page-09")
Can discuss placement of either here
If I have extra time I can demo
repl_example("event-types")
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
Garrick Aden-Buie
rstudio::conf(2020, "JavaScript for Shiny Users")
.dot
or [bracket]
notationconst
means can't reasign the whole objectconst stats = {pkg: 'dplyr', downloads: 893}stats.pkgstats['downloads']const propName = 'downloads'stats[propName]stats.rankstats.downloads = 893stats
let pkg = 'dplyr'let downloads = 893const stats = {pkg: 'dplyr', downloads: 893}stats = {pkg, downloads}
Object.keys()
let stats = { pkg: 'dplyr', downloads: 893, depends: ['glue', 'magrittr', 'rlang']}let future = {}stats.version = {major: 0, minor: 8, patch: 3}Object.keys(stats)Object.keys(stats.depends)Object.keys(future)
let pkgs = ['dplyr', 'ggplot2', 'tidyr', 'shiny']let downloads = [893, 762, 679, 395]
let random = [1, 2, 'three']let nothingYet = []
pkgs.length
let pkgs = [['dplyr', 'tidyr'], 'ggplot2', 'shiny']pkgs[0]pkgs[0][1]pkgs * 1000
//propertypkgs.length// some methods don't change the arraypkgs.includes('shiny')pkgs.slice(1, 4)// some methods do!pkgs.push('glue')pkgs.pop()// some do not clearly state their intentionspkgs.sort()
Object.keys(pkgs)let stats = {pkg: pkgs[0], downloads: 900}typeof pkgstypeof statsstats instanceof Objectpkgs instanceof Objectpkgs instanceof Array
Array.isArray(pkgs)
A common pattern you'll see often is to initialize an empty array []
that is built up over time.
Give yourself an empty pkgs = []
30 seconds to think of your favorite packages
using .push()
to add each package.
Follow up:
00:30
function increment(x, by) { return x + by}
function increment(x, by) { return x + by}
const increment = function(x, by) { return x + by}
function increment(x, by) { return x + by}
const increment = function(x, by) { return x + by}
function(x, by) { return x + by}
(function() {...})()
js4shiny::repl_js()
function increment(x, by) { return x + by}increment(1, 2)
const name = function()
const increment = (x, by) => { return x + by}increment(30, 12)
function increment(x, by) { return x + by}
~ { .x + by }
const inc = (x, by) => x + by
Create cities
: an array of objects with the city name and age for
Write a function updateCityAge
that takes a city
and age
and updates the document
In the browser, use the array and function to switch between San Francisco and Los Angeles.
03:00
repl_example("first-page-04")
const cities = [ {city: 'San Francisco', age: 38.8}, {city: 'Los Angeles', age: 35.9}]function updateCityAge(city, age) { console.log( `${city} residents are ~${age} years old` )}updateCityAge(cities[0].city, cities[0].age)updateCityAge(cities[1].city, cities[1].age)
You need
You need
element.addEventListener('click', function (event) {
// ... do things ...
// ... possibly info in event
})
<button id="next-city" value="0">Next City</button>
updateCityAge(cities[0])
const btn = document.getElementById('next-city')btn.addEventListener('click', function(event) { console.log(event)})
event.target
btn.addEventListener('click', function(event) { console.log(event.target.value)})
btn.addEventListener('click', function(event) { let btnValue = event.target.value console.log(cities[btnValue])})
Note we could have done btn.value
to get the value, too
What happens if you just do btnValue + 1
?
btn.addEventListener('click', function(event) { let btnValue = event.target.value btnValue = Number(btnValue) + 1 btn.value = btnValue updateCityAge(cities[btnValue])})
btn.addEventListener('click', function(event) { let btnValue = event.target.value btnValue = Number(btnValue) + 1 if (btnValue >= cities.length) { btnValue = 0 } btn.value = btnValue updateCityAge(cities[btnValue])})
[ {city: 'Fresno', age: 31.8}, {city: 'Santa Rosa', age: 41.4}]
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
const citiesAsJson = JSON.stringify(cities); citiesAsJson
const cities = [ { city: 'San Francisco', age: 38.8 }, { city: 'Los Angeles', age: 35.9 }]
[ { "city": "San Francisco", "age": 38.8 }, { "city": "Los Angeles", "age": 35.9 }]
JSON.parse(citiesAsJson)[0]
Run repl_example("first-page-07")
I added JSON data inside <script id="data-cities">
Find the element and save its .textContent
to a variable
Use JSON.parse()
to convert the JSON string to data in place of cities
.
Watch out! The data calls age median_age
, make sure you update your function.
Bonus: Search MDN for destructuring assignment and use the Object destructuring section to learn how to assign a property to a new variable name
Bonus: Add median_home_price
to your page, too.
05:00
Our index.html
is getting crowded.
style.css
<head> <link href="style.css" rel="stylesheet" /></head>
script.js
// last thing in <body><script src="script.js"></script>
repl_example("first-page-09")
Can discuss placement of either here
If I have extra time I can demo
repl_example("event-types")