<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/minecharts-1-minecraft-r-edgar-andersons-iris-data/"></div></div><div><a data-url="https://www.r-bloggers.com/minecharts-1-minecraft-r-edgar-andersons-iris-data/" data-counturl="https://www.r-bloggers.com/minecharts-1-minecraft-r-edgar-andersons-iris-data/" data-count="horizontal" data-text="MinechaRts #1 (Minecraft + R + Edgar Anderson’s Iris Data)" 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://smarterpoland.pl/index.php/2016/09/minecharts-1-minecraft-r-edgar-andersons-iris-data/"> SmarterPoland.pl » English</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>How to use R to draw 3D scatterplots in Minecraft? Let’s see.</p>
<p><img src="https://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/09/iris1-1024x772.png?w=456" data-recalc-dims="1"></p>
<p>Minecraft is a game about placing blocks and going on adventures (<a target="_blank" href="https://minecraft.net/">source</a>). Blocks are usually placed by players but there are add-ons that allow to add/modify/remove blocks through external API.<br>
And this feature is being used in educational materials that show how to use Minecraft to learn Python (or how to use Python to modify Minecraft worlds, <a target="_blank" href="https://www.nostarch.com/programwithminecraft/">see this book for example</a>). You need to master loops to build a pyramid or a cube. And you need to touch some math to build an igloo or a fractal. Find a lot of cool examples by googling <a target="_blank" href="https://www.google.pl/search?q=minecraft%20python%20programming">‘minecraft python programming’</a>.</p>
<p>So, Python+Minecraft are great, but how to do similar things in R?<br>
You need to do just three things:</p>
<ol>
<li>Install the Spigot Minecraft Server along with all required dependencies. The detailed instruction how to do this <a target="_blank" href="https://www.nostarch.com/download/LTPWM_ch01_update_online.pdf">is here</a>.</li>
<li>Create a socket connection to the Minecraft Server port 4711. In R it’s just a single line

<pre>conn &lt;- socketConnection(host=&quot;localhost&quot;, port = 4711, blocking=T, server=F, open=&quot;r+&quot;)</pre>
</li>
<li>Send building instructions through this connection. For example
<pre>writeLines(&quot;world.setBlocks(0,70,0,10,80,10, 46)&quot;, conn)</pre>
<p>will create a cube 11x11x11 made of TNT blocks (id=46 is for TNT, see <a target="_blank" href="http://minecraft-ids.grahamedgecombe.com/">the full list here</a>) placed between coordinates (0,70,0) (10,80,10). You can add and remove blocks, move players, spawn entities and so on. See <a target="_blank" href="https://www.raspberrypi.org/learning/getting-started-with-minecraft-pi/worksheet/">a short overview of the server API</a>.

<p>The R code below creates a connection to the minecraft server, builds a flat grassland around the spawning point and plots 3d scatterplot with 150 blocks (surprise surprise, blocks coordinates correspond to Sepal.Length, Sepal.Width, Petal.Length variables from the iris dataset).</p>

<pre>#
# A useful function
addBlock &lt;- function(x, y, z, b, conn) {
  writeLines(paste0(&quot;world.setBlock(&quot;,round(x),&quot;,&quot;,round(y),&quot;,&quot;,round(z),&quot;,&quot;,round(b),&quot;)&quot;), conn)
}

#
# Connect to the server (install and run https://www.nostarch.com/download/LTPWM_ch01_update_online.pdf)
conn &lt;- socketConnection(host=&quot;localhost&quot;, port = 4711, blocking=TRUE, server=FALSE, open=&quot;r+&quot;)
baseline &lt;- 70

#
# Add two layers of grass and wipe out everything above
writeLines(paste0(&quot;world.setBlocks(-80,&quot;,baseline-2,&quot;,-80,180,&quot;,baseline,&quot;,180,2)&quot;), conn)
writeLines(paste0(&quot;world.setBlocks(-50,&quot;,baseline+1,&quot;,-50,150,&quot;,baseline+50,&quot;,150,0)&quot;), conn)

#
# And now add blocks based on iris data
for (i in 1:nrow(iris)) {
  addBlock(10*iris[i,&quot;Sepal.Length&quot;],
           baseline + 2 + 10*(iris[i,&quot;Sepal.Width&quot;] - min(iris[,&quot;Sepal.Width&quot;])),
           10*iris[i,&quot;Petal.Length&quot;],
           c(41,42,45)[as.numeric(iris[i,&quot;Species&quot;])], 
           conn)
}</pre>

<p>If you do not like scatterplots try barcharts <img alt=";-)" class="wp-smiley" src="https://i1.wp.com/smarterpoland.pl/wp-includes/images/smilies/icon_wink.gif?w=456" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/09/iris2-1024x520.png?w=456" data-recalc-dims="1"></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/minecharts-1-minecraft-r-edgar-andersons-iris-data/"></div></div><div><a data-url="https://www.r-bloggers.com/minecharts-1-minecraft-r-edgar-andersons-iris-data/" data-counturl="https://www.r-bloggers.com/minecharts-1-minecraft-r-edgar-andersons-iris-data/" data-count="vertical" data-text="MinechaRts #1 (Minecraft + R + Edgar Anderson’s Iris Data)" 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://smarterpoland.pl/index.php/2016/09/minecharts-1-minecraft-r-edgar-andersons-iris-data/"> SmarterPoland.pl » English</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/XOOT_FzA-vU"></a></div></ol>