Errors and Debugging in RStudio. Diagnosing and fixing errors in your code can be time-consuming and frustrating. There are two ways you can make your life easier. The first is knowing the tools at your disposal in RStudio to debug errors. RStudio provides a variety of tools to help you diagnose the problem at its source and come up with a solution as quick as possible. The second is knowing how to write functions that return clear yet detailed errors using condition handling. This post will walk through both of these topics so that you can become better at handling errors when writing your own code as well as working with errors in someone else’s code. Debugging Errors The following general strategy can be applied to debug an error, as outlined by Hadley Wickham in his Advanced R book: These four steps should be followed each time you encounter an unexpected error in a function.
Let’s look at an example function to demonstrate the use of these tools. Chi_fisher_p(treatment, "gender", "treatment") chi_fisher_p is_prime(-3) Building a website with pkgdown: a short guide | R-bloggers. As promised in my last post, here is a short guide with some tips and tricks for building a documentation website for an R package using pkgdown. In the end, this guide ended up way longer than I was expecting, but I hope you’ll find it useful, although it often replicates information already available in pkgdown documentation ! Prerequisites To build a website using pkgdown, all you need to have is an R package hosted on Git Hub, with a file structure “tweaked” with some functionality provided by devtools.
Assuming you are using RStudio, and that you didn’t already do this, open the project corresponding to your package and (as a minimum) run: require(devtools)use_readme_rmd()use_news_md()use_vignette("test") #substitute with the name of your package Since to use pkgdown your package must be on Git Hub, you may also want: , which will populate automatically some fields in the DESCRIPTION file successively used to build the home page of your website. use_travis()use_cran_badge() Navbar Section.
Tonsky/FiraCode.
Strcode - structure your code better. I am pleased to announce my package strcode, a package that should make structuring code easier. You can install it from GitHub, a CRAN submission is planned at a later stage. devtools::install_github("lorenzwalthert/strcode") The main feature of the package is its function to insert code breaks. These are helpful in breaking the code down into smaller sections. We suggest three levels of granularity for code structuring, wherein higher-level blocks can contain lower-level blocks: You can notice from the above that: The number of #’s used in front of the break character (___, ..., . .) correspond to the level of granularity for a code separator. Every title line ends with ####. In addition, it means that you can fold sections as you can fold function declarations or if statements.
The package strcode provides an RStudio Add-in to insert each of the three separators presented above - with a single click. Sometimes it is required to refer to a code section, which can be done by a title. Reading data from google sheets into R. Reading data from google sheets into R is something you imagine should be really simple, but often is anything but. However, package googlesheets goes a long way to solving this problem. Let’s crack on with an example.
First, install the software: install.packages("googlesheets") We then need an example sheet to work with, and I’m going to use one from Britain Elects: So go here and add this to your own collection (yes you’ll need a google account!) Right, let’s play around a bit! # load package library(googlesheets) # which google sheets do you have access to? This should show you all of the sheets you have access to, and also select the one we want. Now we can see which worksheets exist within the sheet: # list worksheets gs_ws_ls(be) We can "download" one of the sheets using gs_read() # get Westminster voting west <- gs_read(ss=be, ws = "Westminster voting intentions", skip=1) # convert to data.frame wdf <- as.data.frame(west) And hey presto, we have the data. Now let's plot it: Tracking progress in R – Bioinformatics Playground. Admit it or not, we human beings become anxious and impatient when it comes to wait. Especially when we are blindfolded — that is, we are unaware of how long we have to suffer the endless wait.
As pointed out by Brad Allan Myers, arguably the designer of progress bar in 1980s, being able to track the progress during the waiting can significantly improve the user experience (Myers, 1985). As an R programmer in bioinformatic research, often my codes are not designed for the general public, but it is important to make sure that my users, namely my fellow colleagues and researchers, are as happy as possible. However, tracking process in R can be tricky. In this article, I am going to present you some approaches and my solution (pbmcapply). Print The easiest way to handle progress tracking in R is to periodically print the percentage of completion to the output, which is the screen by default, or write it to your log file located somewhere on the disk. Pbapply Pbmcapply Conclusion Reference. What is the cost of a progress bar in R? – Péter Sólymos.
The pbapply R package adds progress bar to vectorized functions, like lapply. A feature request regarding progress bar for parallel functions has been sitting at the development GitHub repository for a few months. More recently, the author of the pbmcapply package dropped a note about his implementation of forking functionality with progress bar for Unix/Linux computers, which got me thinking. How should we add progress bar to snow type clusters? Which led to more important questions: what is the real cost of the progress bar and how can we reduce overhead on process times? Parallel workflow Fist off, let’s review how a snow type cluster works.
(This is actually implemented in the parallel package, but I tend to refer to snow as that was the original package where the parallel code was taken from, see here.) library(parallel)parLapply The cl argument refers to a cluster object that can be conveniently set up via the makeCluster function. SplitIndices(nx = 10, ncl = 2) Conclusions. How to create a free distributed data collection "app" with R and Google Sheets · Simply Statistics.
26 Aug 2016 By Jeff Leek Share this on → Twitter | Facebook | Google+ Jenny Bryan, developer of the google sheets R package, gave a talk at Use2015 about the package. One of the things that got me most excited about the package was an example she gave in her talk of using the Google Sheets package for data collection at ultimate frisbee tournaments. One reason is that I used to play a little ultimate back in the day. Another is that her idea is an amazing one for producing cool public health applications. What you will need A Google account and access to Google Sheets R and the googlesheets package. The “app” What we are going to do is collect data in a Google Sheet or sheets. Making the Google Sheet work with googlesheets After you have a first thing to do is to go to the Google Sheets I suggest bookmarking this page: which skips the annoying splash screen.
Create a blank sheet and give it an appropriate title for whatever data you will be collecting. Profiling with RStudio and profvis | RStudio Blog. Scheduling R Markdown Reports via Email |analytics for fun. R Markdown is an amazing tool that allows you to blend bits of R code with ordinary text and produce well-formatted data analysis reports very quickly. You can export the final report in many formats like HTML, pdf or MS Words which makes it easy to share with others. And of course, you can modify or update it with fresh data very easily. I have recently been using it R Markdown for pulling data from various data source such Google Analytics API and MySQL database, perform several operations on it (merging for example) and present the outputs with tables, visualizations and insights (text).
But what about automating the whole report generation and emailing the final report as an attached document every month at a specific time? In this post I am going to explain how to do it in Windows. If you do a search on google, you will find several threads on stackoverflow and a few good specific posts on it. However it took me sometimes to get it working and had to try different options before. Programmatically create interactive Powerpoint slides with R. Login - DataJoy - Online Python and R editor. R for Public Health: Basics of Lists. You don’t need icons! Here are 100+ unicode symbols that you can use. Danny Markov Ever needed to add an icon to your design, but you didn’t want to include images or an entire icon font like Font Awesome into your page? We’ve got good news for you – there is a vast library of available icons and glyphs already in your browser.
It is called Unicode, and it is a standard which assigns a unique identifier for an ever expanding number (currently over 110 000) of characters, symbols and icons. This doesn’t mean that you have a choice of a hundred thousand icons, though. It is up to the browser to render them, and it uses the fonts which are installed on the system to do so. Tip: There is a good article which explains everything you need to know about character encodings and unicode, which we recommend for every software developer to read. How to use these icons The icons given in the tables below, are regular characters, which you can copy and paste as if they are letters of text. Find an icon that you like.
Icon-like Arrows Special Currency Weather Pointers Zodiac.
REGEX. High performance & Parallel. Using Dates and Times in R. Today at the Davis R Users’ Group, Bonnie Dixon gave a tutorial on the various ways to handle dates and times in R. Bonnie provided this great script which walks through essential classes, functions, and packages. Here it is piped through knitr::spin. The original R script can be found as a gist here. Date/time classes Three date/time classes are built-in in R, Date, POSIXct, and POSIXlt. Date This is the class to use if you have only dates, but no times, in your data. create a date: dt1 <- as.Date("2012-07-22") dt1 non-standard formats must be specified: dt2 <- as.Date("04/20/2011", format = "%m/%d/%Y") dt2 dt3 <- as.Date("October 6, 2010", format = "%B %d, %Y") dt3 see list of format symbols: `?
Calculations with dates: find the difference between dates: dt1 - dt2 ## Time difference of 459 days difftime(dt1, dt2, units = "weeks") ## Time difference of 65.57 weeks Add or subtract days: dt2 + 10 dt2 - 10 create a vector of dates and find the intervals between them: diff(three.dates) unclass(dt1) POSIXct chron. Building email reports with R. Communicating your results Most of the time just writing a bunch of code to analyze data isn't enough, you need to be able to communicate your results to the rest of your company. This process can take many forms, but we're going to focus on a tried and true method--the automated email. I'll just say upfront that it's your job not to get too carried away. Nobody wants their inbox completely stuffed with your ggplots (no matter how pretty they might be). A brief aside If you are someone at an organization that is drowing in automated email, check out eventifier.
It's a simple push API with node.js that comes with a built in Chrome Extension. The package The package we're going to be using is (shockingly) called sendmailR. Install.packages("sendmailR") library(sendmailR)? Credentials Specifying your credentials is always the hard part. Sendmail_options(smtpServer="ASPMX.L.GOOGLE.COM") Your first email Ok! # gmail requires the angle bracket syntax for from/to (i.e. Sending plots Before you go.
Iterators in R | Exegetic Analytics. According to Wikipedia, an iterator is “an object that enables a programmer to traverse a container”. A collection of items (stashed in a container) can be thought of as being “iterable” if there is a logical progression from one element to the next (so a list is iterable, while a set is not). An iterator is then an object for moving through the container, one item at a time. Iterators are a fundamental part of contemporary Python programming, where they form the basis for loops, list comprehensions and generator expressions.
Iterators facilitate navigation of container classes in the C++ Standard Template Library (STL). They are also to be found in C#, Java, Scala, Matlab, PHP and Ruby. Although iterators are not part of the native R language, they are implemented in the iterators and itertools packages. The iterators package is written and maintained by Revolution Analytics.
Iterating over a Vector The iter() function transforms a container into an iterator. . … or by row. Hmmmm. References. Producing Simple Graphs with R. Transforming a color scale. Download all Documents from Google Drive with R. Getting and Plotting Polls in R. The famous probabilist and statistician Persi Diaconis wrote an article not too long ago about the "Markov chain Monte Carlo (MCMC) Revolution. " The paper describes how we are able to solve a diverse set of problems with MCMC. The first example he gives is a text decryption problem solved with a simple Metropolis Hastings sampler. I was always stumped by those cryptograms in the newspaper and thought it would be pretty cool if I could crack them with statistics. So I decided to try it out on my own. The decryption I will be attempting is called substitution cipher, where each letter of the alphabet corresponds to another letter (possibly the same one).
The strategy is to use a reference text to create transition probabilities from each letter to the next. To create a transition matrix, I downloaded War and Peace from Project Gutenberg. After I have these counts, I normalize by dividing by the row total. Below is the code for creating the transition probability matrix.