×

Our next R course is at University of Queensland (Australia) from 18th Feburary 2020. Beginner, intermediate and advanced classes available.

seascape models

Survey your audience and visualise the results with R and Google forms

I wanted to make my presentation on dataviz at the UQ School of Bioinformatics more interactive.

A quiz is a good way to engage your audience. Given I was giving a talk about R datavisuals I thought it would be fun to visualise the quiz results using R live with the audience. To top it off, we posted the results to Twitter.

This blog describes is how I did that.

You could also use this system to survey our audience and share the results live. Just prepare you R code and set it to run at a certain time during your talk with a task scheduling algorithm.

Setting up the survey

I used Google Forms to do my quiz. You can take it here. I posed a few questions that challenged the audience to think about the best way to visualise data.

It is pretty easy to set up a survey if you have a gmail account. A few tips:

Connecting to your survey answers in R

I used the googlesheets package to read my survey answers from the (public) spreadsheet. You will need to authenticate yourself first:

library(googlesheets)
gs_ls()

This will prompt you to login to your google account and authenticate an app that allows the connection to happen.

Now we can load our data:

sheet_url <- "https://docs.google.com/spreadsheets/d/10i3v3NIVpgmURyLVzsiadPAMGeqa7dLFcDb9sqFe8KA/edit#gid=1513779153"
dataviz <- gs_url(sheet_url) #creates connection

If you want to keep your sheet private you can use gs_ls() to list all your sheets, and then pick a name to read it in. e.g. like this:

dataviz <- gs_title("Dataviz quiz 2018 v2 (Responses)")
dat <- gs_read(dataviz)

Analysing your data

The file dat we just read in is a dataframe like object (actually a tibble) where each column is a question and each row is a response. The first column is a time stamp.

All other columns are titled with your questions.

It will make life easier if we rename the columns to shorter (but still descriptive) names.

newnames <- c("timestamp", "shopping",
              "bar_percent",
              "pie_percent",
              "room",
              "cb_age")
names(dat) <- newnames

Now let’s create some dataviz

library(ggplot2)
datplot <- na.omit(dat)
ggplot(datplot, aes(x = room, y = cb_age)) +
  geom_boxplot() +
  xlab("Position in room") +
  ylab("Guess at CB's age") +
  ylim(0, 75) +
  theme_bw()

A boxplot of the audience’s guesses at my age by their position in the room. I limited the y-axis because there were some outrageously large numbers!

Share the results

We could show the audience the results on our screen. But why not let Twitter know too!

For this, I used the rtweet package. rtweet is pretty simple to use once you’ve set up an app on Twitter’s API and authorised R to access it. So get rtweet then look at the vignette vignette("auth"). Follow the instructions to the letter and you shouldn’t have any problems.

Once authorisation is done, its a simple matter to save our plot as a png to use in a tweet:

myplot <- ggplot(datplot, aes(x = room, y = cb_age)) +
  geom_boxplot()
ggsave(filename = "myplot.png", myplot)

Now just write your tweet and send it off to twitter.

library(rtweet)
newstatus = "Chris age as surveyed at  #UQwinterSchool
@DoktrNick @UQwinterSchool"

post_tweet(status = newstatus,
           media = "myplot.png")

Next steps?

So I tried this as a way of doing a live R tutorial. Next step would be to try and integrate it into a talk without showing the R coding. For that you would either need to get a friend to run the code or use a scheduler (like the taskscheduleR R package).

Be careful though! You never know what answers people may give if allowed. So design you code to be robust to strange answers (like that I am 100 years old).



Contact: Chris Brown

Email Tweets Code on Github

home

Designed by Chris Brown. Source on Github