<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/"></div></div><div><a data-url="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/" data-counturl="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/" data-count="horizontal" data-text="R Posting Multiple Answers to Google Forms" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://data-steve.github.io/post-multiple-answers-to-google-forms/"> data_steve</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a data-style="mega" target="_blank" href="https://github.com/data-steve/googelformr" data-icon="octicon-star">googelformr</a></p>
<div>
<img src="https://i0.wp.com/data-steve.github.io/images/googleformr_logo.png?w=200" data-recalc-dims="1">
</div>
<p>In <a target="_blank" href="http://data-steve.github.io/googleformr-5min-howto">previous posts</a> on <a target="_blank" href="http://data-steve.github.io/feedbackr-for-user-feedback">how to use</a> <strong>googleformr</strong>, I’ve always demonstrated the functionality on toy forms that had only one text box. User feedback from has suggested that functionality for posting answers to a form with multiple questions would be desirable.</p>
<p>Happily, googleformr already has that capability. Using the <code>gformr</code> function we can create a function <code>post_answers</code> links to our form via the form url or form id. To get the most recent development copy of <strong>googleformr</strong>:</p>
<div>
<pre><code>pacman::p_load_current_gh(&quot;data-steve/googleformr&quot;)
</code></pre>
</div>
<p>A demostration showing how to do this is a good idea. We’ll be posting a variety of example answers to this form here:</p>
<div>
<p><img src="https://i0.wp.com/data-steve.github.io/images/testform_form.png?resize=232%2C217" data-recalc-dims="1"></p>
</div>
<div>
<pre><code>form_id &lt;- &quot;11v2QeaQomevSyzBTHeIIK2uPJ2moby2IQT7jbk7UhgQ&quot;
post_answers &lt;- googleformr::gformr(form_id)
</code></pre>
</div>
<p>For ease, I’ve did add a pretty printing function <code>get_form_str</code> to <strong>googleformr</strong> that makes it easier to see the form questions and entry ids side-by-side.</p>
<div>
<pre><code>  get_form_str(form_id)
  
  	Order your post content
  	according to the Questions on Form
       entry.qs       entry.ids         
  [1,] &quot;mc&quot;           &quot;entry.2006129165&quot;
  [2,] &quot;check&quot;        &quot;entry.872386724&quot; 
  [3,] &quot;short answer&quot; &quot;entry.1508946521&quot;
  [4,] &quot;paragraph&quot;    &quot;entry.1764754128&quot;
</code></pre>
</div>
<p>Suppose we want to post the following content to our form. Here’s two illustrative examples.</p>
<div>
<pre><code>post_this &lt;- c(&quot;I'm not a robot&quot;, &quot;I'm a human&quot;, &quot;anything&quot;, NA)
dont_panic &lt;- c(&quot;Paranoid Android&quot;, 42, &quot;Meaning of life&quot;, NA)
</code></pre>
</div>
<p>To send this content we just</p>
<div>
<pre><code>post_answers(post_content=post_this)
post_answers(post_content=dont_panic)
</code></pre>
</div>
<p>We can see below the answers show up like this in the spreadsheet.</p>
<p><img alt="" src="https://i0.wp.com/data-steve.github.io/images/testform_entries.png?w=456" data-recalc-dims="1"></p>
<p>But you can confirm a 200 http status by using <code>check_form_works()</code>, like so:</p>
<div>
<pre><code>post_answers(post_content=post_this) %>% check_form_works()
</code></pre>
</div>
<p>The <code>All good.</code> is the assurance.</p>
<h3>Two important caveats</h3>
<p>First, as long as the number of elements in the <code>post_content</code> vector match the number of <code>entry.ids</code> (or names) it has scraped from the page source, it should go through. If they aren’t, it will error and you’ll need to make sure there’s equal length between post vector and entry.ids vector.</p>
<p>Second, while Google Forms accepts the post no matter its content data types (assuming the caveat above regarding length of elements and names), it does require that certain question types receive only certain answers. For example, the multiple choice and checkbox answer types only accept answers that are one of the listed options as answers; otherwise it leaves those cells in the spreadsheet empty, as the <code>dont_panic</code> list values demonstrate. The <code>All good</code> will still show up in the console, even if the cell rejects the data.</p>
<p><strong>googleformr</strong> currently doesn’t support error throwing for conflicts when a submitted answer isn’t on the list of options. So its good to double check answers generally and spelling specifically. The error triggered is not from the data types of the content sent, since the function coerces everything to character anyway, but rather is triggered by the regex requirements placed on the content.</p>
<p>That’s why it may be advisable to use the short answer and long answer text boxes when building your own forms to be used with <code>googleformr</code>; with these, we don’t face the same restrictions observed above. As proof, we can even turn the <code>mtcars</code> dataframe into a json string and post it to the textbox. This is one way to pass dataframes around without worrying about structuring a form to adhere to specific database requirements.</p>
<div>
<pre><code>empty_cars &lt;- c(&quot;&quot;, &quot;&quot;, &quot;&quot;, jsonlite::toJSON(mtcars) )
post_answers(post_content=empty_cars)
</code></pre>
</div>
<p>In the next post, I’ll be showing how to use googleformr to create an automatic homework checker that gives programmatic feedback.</p>
<p><a target="_blank" class="twitter-share-button" data-via="data_steve" data-size="large" data-hashtags="rstats,gapi,datascience" data-dnt="true" href="https://twitter.com/share">Tweet</a><a data-style="mega" target="_blank" href="https://github.com/data-steve/googelformr" data-icon="octicon-star">googelformr</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/"></div></div><div><a data-url="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/" data-counturl="https://www.r-bloggers.com/r-posting-multiple-answers-to-google-forms/" data-count="vertical" data-text="R Posting Multiple Answers to Google Forms" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://data-steve.github.io/post-multiple-answers-to-google-forms/"> data_steve</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/vjzwAC65dMY"></a></div>