Code
R code
<- iris
irism colnames(irism) <- gsub("[.]","_",tolower(colnames(irism)))
ojs_define(ojsd = irism)
Convert R data.frame to OJS object. This is the only code chunk that is R. All other chunks are ojs.
R code
<- iris
irism colnames(irism) <- gsub("[.]","_",tolower(colnames(irism)))
ojs_define(ojsd = irism)
Transpose JS object.
= transpose(ojsd) ojsdata
Raw JSON object.
ojsdata
Display as table.
= Inputs.table(ojsdata) viewof raw_table
Computing a new variable using an arrow function. ...
keeps all old variables and the new variable ratio
is added.
= ojsdata.map(d => ({...d, ratio: d.sepal_width / d.petal_width})) ojsdatamod
= Inputs.table(ojsdatamod) viewof mod_table
Define checkbox input.
= Inputs.select(["setosa", "versicolor", "virginica"], {value: ["setosa"], multiple: false, label: "Species"}) viewof grp
Filter data based on checkbox inputs.
= ojsdata.filter(d => d.species.includes(grp)) ojsdata_filtered
Display filtered data as table.
= Inputs.table(ojsdata_filtered) viewof filtered_table
Display a scatterplot. X and Y axes are defined by select inputs.
= Inputs.select(Object.keys(ojsdata[0]), {value: "sepal_length", multiple: false, label: "X axis"}) viewof x
= Inputs.select(Object.keys(ojsdata[0]), {value: "sepal_width", multiple: false, label: "Y axis"}) viewof y
.plot({
Plotmarks: [
.dot(ojsdata, {
Plotx: x,
y: y,
fill: "species",
title: (d) =>
`${d.species} \n Petal length: ${d.petal_length} \n Sepal length: ${d.sepal_length}`
}),
]grid: true
})
Search through a table.
= Inputs.search(ojsdata,
viewof ojsdata_filtered_search
{ placeholder: "Search for species",
datalist: ojsdata.map(d => d.species),
label: "Search for species: "
})
= Inputs.table(ojsdata_filtered_search) viewof filtered_search_table
Data wrangling using Arquero.
Import dependencies and convert data to Arquero format.
import { aq, op } from '@uwdata/arquero'
= aq.from(ojsdata)
ojsdata_aq
ojsdata_aq.groupby('species')
.filter(p => p.sepal_length > 5)
.rollup({
count: op.count(),
avg_mass: op.average('sepal_length')
}).view()