<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/survey-your-audience-and-visualise-the-results-with-r-and-google-forms/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.seascapemodels.org/rstats/2018/07/05/survey-your-audience.html"> Bluecology blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Survey your audience and visualise the results with R and Google forms</h1>
<p>I wanted to make my <a rel="nofollow" href="http://www.seascapemodels.org/rstats/2018/07/04/dataviz2018_notes.html" target="_blank">presentation on dataviz at the UQ School of<br>
Bioinformatics</a><br>
more interactive.</p>
<p>A quiz is a good way to engage your audience. Given I was giving a talk<br>
about R datavisuals I thought it would be fun to visualise the quiz<br>
results using R live with the audience. To top it off, we posted the<br>
results to Twitter.</p>
<p>This blog describes is how I did that.</p>
<ul>
<li><a rel="nofollow" href="https://smp.uq.edu.au/event/session/5650" target="_blank"><br>
Our next R course is at University of Queensland (Australia) from Feburary 5th 2019. Beginner, intermediate and advanced classes available.<br>
</a></li>
</ul>
<p>You could also use this system to survey our audience and share the<br>
results live. Just prepare you R code and set it to run at a certain<br>
time during your talk with a task scheduling algorithm.</p>
<h2>Setting up the survey</h2>
<p>I used Google Forms to do my quiz. <a rel="nofollow" href="https://docs.google.com/forms/d/e/1FAIpQLSd5v5d15q8KO7VyfjRRrfGV1NadKVyLpyAdzqu2Fvreq40UXg/viewform" target="_blank">You can take it<br>
here</a>.<br>
I posed a few questions that challenged the audience to think about the<br>
best way to visualise data.</p>
<p>It is pretty easy to set up a survey if you have a gmail account. A few<br>
tips:</p>
<ul>
<li>
<p>You can add images, which is great posing questions about results.</p>
</li>
<li>
<p>I used the ‘short answer’ input for numeric answers. If you click<br>
the validation tab at the bottom of each ‘short answer’ question you<br>
can require users enter certain types of numbers (e.g. within a<br>
range).</p>
</li>
<li>
<p>Think carefully about limiting required inputs if you want to avoid<br>
bugs that might arise from unexpected answers.</p>
</li>
<li>
<p>There is a green button at the top of the form that let’s you link<br>
it to a google sheet. Do this.</p>
</li>
<li>
<p>You can make the sheet public, so other people can use it, but<br>
changing the sharing settings.</p>
</li>
</ul>
<h2>Connecting to your survey answers in R</h2>
<p>I used the <code>googlesheets</code> package to read my survey answers from the<br>
(public) spreadsheet. You will need to authenticate yourself first:</p>
<div>
<div>
<pre><code>library(googlesheets)
gs_ls()
</code></pre>
</div>
</div>
<p>This will prompt you to login to your google account and authenticate an<br>
app that allows the connection to happen.</p>
<p>Now we can load our data:</p>
<div>
<div>
<pre><code>sheet_url &lt;- &quot;https://docs.google.com/spreadsheets/d/10i3v3NIVpgmURyLVzsiadPAMGeqa7dLFcDb9sqFe8KA/edit#gid=1513779153&quot;
dataviz &lt;- gs_url(sheet_url) #creates connection
</code></pre>
</div>
</div>
<p>If you want to keep your sheet private you can use <code>gs_ls()</code> to list all<br>
your sheets, and then pick a name to read it in. e.g. like this:</p>
<div>
<div>
<pre><code>dataviz &lt;- gs_title(&quot;Dataviz quiz 2018 v2 (Responses)&quot;)
dat &lt;- gs_read(dataviz)
</code></pre>
</div>
</div>
<h2>Analysing your data</h2>
<p>The file <code>dat</code> we just read in is a dataframe like object (actually a<br>
tibble) where each column is a question and each row is a response. The<br>
first column is a time stamp.</p>
<p>All other columns are titled with your questions.</p>
<p>It will make life easier if we rename the columns to shorter (but still<br>
descriptive) names.</p>
<div>
<div>
<pre><code>newnames &lt;- c(&quot;timestamp&quot;, &quot;shopping&quot;,
              &quot;bar_percent&quot;,
              &quot;pie_percent&quot;,
              &quot;room&quot;,
              &quot;cb_age&quot;)
names(dat) &lt;- newnames
</code></pre>
</div>
</div>
<p>Now let’s create some dataviz</p>
<div>
<div>
<pre><code>library(ggplot2)
datplot &lt;- na.omit(dat)
ggplot(datplot, aes(x = room, y = cb_age)) +
  geom_boxplot() +
  xlab(&quot;Position in room&quot;) +
  ylab(&quot;Guess at CB's age&quot;) +
  ylim(0, 75) +
  theme_bw()
</code></pre>
</div>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/images/survey-your-audience_files/figure-markdown_strict/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>A boxplot of the audience’s guesses at my age by their position in the<br>
room. I limited the y-axis because there were some outrageously large<br>
numbers!</p>
<h2>Share the results</h2>
<p>We could show the audience the results on our screen. But why not let<br>
Twitter know too!</p>
<p>For this, I used the <code>rtweet</code> package. <code>rtweet</code> is pretty simple to use<br>
<em>once you’ve set up an app on Twitter’s API and authorised R to access<br>
it</em>. So get <code>rtweet</code> then look at the vignette <code>vignette(&quot;auth&quot;)</code>.<br>
Follow the instructions <em>to the letter</em> and you shouldn’t have any<br>
problems.</p>
<p>Once authorisation is done, its a simple matter to save our plot as a<br>
png to use in a tweet:</p>
<div>
<div>
<pre><code>myplot &lt;- ggplot(datplot, aes(x = room, y = cb_age)) +
  geom_boxplot()
ggsave(filename = &quot;myplot.png&quot;, myplot)
</code></pre>
</div>
</div>
<p>Now just write your tweet and send it off to twitter.</p>
<div>
<div>
<pre><code>library(rtweet)
newstatus = &quot;Chris age as surveyed at  #UQwinterSchool
@DoktrNick @UQwinterSchool&quot;

post_tweet(status = newstatus,
           media = &quot;myplot.png&quot;)
</code></pre>
</div>
</div>
<h2>Next steps?</h2>
<p>So I tried this as a way of doing a live R tutorial. Next step would be<br>
to try and integrate it into a talk without showing the R coding. For<br>
that you would either need to get a friend to run the code or use a<br>
scheduler (like the<br>
<a rel="nofollow" href="https://cran.r-project.org/web/packages/taskscheduleR/index.html" target="_blank">taskscheduleR</a><br>
R package).</p>
<p>Be careful though! You never know what answers people may give if<br>
allowed. So design you code to be robust to strange answers (like that I<br>
am 100 years old).</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.seascapemodels.org/rstats/2018/07/05/survey-your-audience.html"> Bluecology blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/T57_2UILpac" height="1"></a></div>