library(crunch) load("vignettes.RData") options(width=120)
Sometimes you only want to work with a subset of your data. With the
crunch package, you can both filter the views of data you work with in your R session and manage the filters that you and your collaborators see in the web application.
As we've seen in previous vignettes, making logical expressions with Crunch datasets and variables is natural. We showed how to update a selection of values on the server, as well as how to crosstab in a subset of a dataset. Other applications work just as intuitively.
Filtering like this works by creating a dataset or variable object that has the filter embedded in it:
dems <- ds[ds$pid3 == "Democrat",] dems
round(crtabs(mean(track) ~ educ + gender, data=dems), 2)
When you extract a variable from a filtered dataset, it too is filtered. So
is the same as
table(ds$educ[ds$pid3 == "Democrat",])
As an aside, if you prefer using the
subset function, that works just the same as the
[ extract method on datasets:
identical(subset(ds, ds$pid3 == "Democrat"), dems)
In the web application, you can save filter definitions with names for easy reuse. You can also share these filter definitions with other viewers of the dataset.
To do so, we work with the dataset's filter catalog. To start, our filter catalog will be empty:
Create a filter by assigning a Crunch logical expression to the catalog by the name we want to give it, using
filters(ds)[["Young males"]] <- ds$gender == "Male" & ds$age < 30 filters(ds)[["Young males"]]
This new filter now appears in our filter catalog.
You could also have made the filter with the
f <- newFilter("Young males", ds$gender == "Male" & ds$age < 30)
This filter is now available for you to use in the web application. If you want to make the filter available to all viewers of the dataset, make it "public":
is.public(filters(ds)[["Young males"]]) <- TRUE filters(ds)
You can also edit the filter expressions by assigning a new one in, like:
filters(ds)[["Young males"]] <- ds$gender == "Male" & ds$age < 35 filters(ds)[["Young males"]]
One other application for filtering is the dataset exclusion filter. The exclusion allows you to suppress from view rows that match a certain condition. Exclusions are set on the dataset with a Crunch logical expression:
exclusion(ds) <- ds$perc_skipped > 15 exclusion(ds)
All viewers of the dataset will see the dataset as if those rows do not exist; however, as the editor of the dataset, you can remove the exclusion filter to see them if you need:
exclusion(ds) <- NULL dim(ds)
If you do know that you never want to see those rows again, you can permanently delete them with
## Not run ds <- dropRows(ds, ds$perc_skipped > 15)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.