<p>Opening day is on the way Time to set up a persistent database to collect every pitch thrown in this year’s baseball season.</p>
<p>The <code>mlbgameday</code> package is designed to facilitate extract, transform and load for MLBAM “Gameday” data. The package is optimized for parallel processing of data that may be larger than memory. Learn more about the project <a rel="nofollow" href="https://github.com/keberwein/mlbgameday" target="_blank">here</a>.</p>
<h2>Install from CRAN</h2>
<p><code>install.packages(&quot;mlbgameday&quot;)</code></p>
<h2>Creating a Database</h2>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>mlbgameday</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RSQLite</span><span>)</span><span>
</span><span>library</span><span>(</span><span>doParallel</span><span>)</span><span>
</span><span># Create an empty database instance.</span><span>
</span><span>con</span><span> </span><span>&lt;-</span><span> </span><span>dbConnect</span><span>(</span><span>RSQLite</span><span>::</span><span>SQLite</span><span>(),</span><span> </span><span>dbname</span><span> </span><span>=</span><span> </span><span>&quot;mlbgameday.sqlite3&quot;</span><span>)</span><span>
</span><span># Optional: Use parallel processing to populate the database with Spring Training Games.</span><span>
</span><span># Set the number of cores to use as the machine's maximum number of cores minus 1 for background processes.</span><span>
</span><span>no_cores</span><span> </span><span>&lt;-</span><span> </span><span>detectCores</span><span>()</span><span> </span><span>-</span><span> </span><span>2</span><span>
</span><span>cl</span><span> </span><span>&lt;-</span><span> </span><span>makeCluster</span><span>(</span><span>no_cores</span><span>)</span><span> </span><span>registerDoParallel</span><span>(</span><span>cl</span><span>)</span><span>
</span><span>get_payload</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>&quot;2018-01-01&quot;</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>&quot;2018-03-28&quot;</span><span>,</span><span> </span><span>db_con</span><span> </span><span>=</span><span> </span><span>con</span><span>)</span><span>
</span><span># Stop and remove cluster.</span><span>
</span><span>stopImplicitCluster</span><span>()</span><span>
</span><span>rm</span><span>(</span><span>cl</span><span>)</span></code></pre>
</figure>
<h2>Extract Transform Load of MLB Advanced Media Data</h2>
<p>Once you have a database in-place, you can get started quickly. The <code>mlbgameday</code> package will work if your current database was gathered using the <code>pitchRx</code> package.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>mlbgameday</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RSQLite</span><span>)</span><span>
</span><span># Log into your database and retreive the most recent date.</span><span>
</span><span>con</span><span> </span><span>&lt;-</span><span> </span><span>dbConnect</span><span>(</span><span>RSQLite</span><span>::</span><span>SQLite</span><span>(),</span><span> </span><span>dbname</span><span> </span><span>=</span><span> </span><span>&quot;mlbgameday.sqlite3&quot;</span><span>)</span><span>
</span><span>db_end</span><span> </span><span>&lt;-</span><span> </span><span>dbGetQuery</span><span>(</span><span>con</span><span>,</span><span> </span><span>&quot;SELECT MAX(date) FROM atbat&quot;</span><span>)</span><span>
</span><span># Use the max date +1 as the start date and today -1 for the end date for your new payload.</span><span>
</span><span>get_payload</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>bd_end</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>Sys.Date</span><span>()</span><span> </span><span>-</span><span> </span><span>1</span><span>,</span><span> </span><span>db_con</span><span> </span><span>=</span><span> </span><span>con</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Error in get_payload(start = bd_end, end = Sys.Date() - 1, db_con = con): object 'bd_end' not found</code></pre>
</figure>
<h2>Task Scheduling</h2>
<p>I prefer to pull the day’s data early in the morning (for the day before.) What ever time you choose, you want to consider time zones and allow enough additional time to cover rain delays for late games, as not to miss any information. There are various task scheduling tools, depending on your operating system.</p>
<ul>
<li>
<p>Linux or OSx: Cron is pretty much the universal standard. Cron is command line driven, but GUI interfaces exist for both operating systems.</p>
</li>
<li>
<p>Windows: Several options, but the built-in task scheduler is probably the best.</p>
</li>
</ul>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/keberwein/mlbgameday/master/man/figures/mlbgameday_hex.png?w=456&ssl=1" data-recalc-dims="1"></p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/T4l3k-ytJl4" height="1">