Setting up macOS for data analysis with R

I’m using R on a regular basis on my Mac. Before macOS Sierra, I used to install everything from official web sites, but I found it hard to keep track of at some point. When I was switching to Sierra, I decided to make a clean install and then reinstall all 3rd party software either from AppStore or via Homebrew, as long as possible.

Homebrew is a package manager for macOS. Many Unix-like OSs use some form of a package manager to centrally install and maintain 3rd party software. macOS normally doesn’t come with any, but Homebrew fills that gap really well.

The good thing about Homebrew is that, it keeps track of all files that an application installs. If there’s a problem, it’s easy to find it out afterwards. It also makes it super easy to update programs to their latest versions, even those that come with their own auto-update functionality.

In this post, I’ll walk you through how to install R into macOS using Homebrew.

DISCLAIMER: These all work on my Mac but I cannot promise anything for you. Use at your own risk and always keep a TimeMachine backup!

First, we need to install and set up Homebrew. Open a shell session using the Terminal app (you can search for it in Spotlight or find it among Utilities). Then give the following commands in that order:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew upgrade
brew doctor

Next, we need to add some extra functionality to Homebrew:

brew tap caskroom/cask
brew tap caskroom/fonts
brew tap buo/cask-upgrade
brew tap homebrew/science

The command line that you’re working now is called Bash. It’s an old version that comes with macOS, due to some license issues. However, one can switch to a more advanced version via Homebrew. This is optional, but I recommend it.

brew install bash
sudo echo /usr/local/bin/bash >> /etc/shells
chsh -s /usr/local/bin/bash
logout

If you do this, you need to restart your Terminal session.

Now we’re going to install R and RStudio. RStudio is the best IDE for R.

brew cask install xquartz
brew install r
brew cask install rstudio
brew cask install font-fira-code
brew install

Here we also install a new font (Fira Code) that is visually more informative when you’re programming. However, that’s optional.

And finally, to add more taste to macOS, we will install QuickLook plugins. These extend the QuickLook when you want to have a look at code files, images, etc. These are also optional.

brew cask install qlcolorcode
brew cask install qlmarkdown
brew cask install quicklook-csv
brew cask install qlstephen
brew cask install qlimagesize

The qlcolorcode can be extended by changing its parameters, such as:

defaults write org.n8gray.QLColorCode font "Fira Code"
defaults write org.n8gray.QLColorCode fontSizePoints 8
defaults write org.n8gray.QLColorCode extraHLFlags '-l -W -J=80'

All these have probably generated a lot of unnecessary files. We can get rid of them via:

brew cleanup
brew cask cleanup

Now you can start RStudio from Applications. If you want to use Fira Code, go to RStudio -> Preferences -> Appearance -> Editor font and choose Fira Code. Next, check for package updates.

update.packages()

I highly recommend that you work with Hadley’s packages collected under tidyverse. You can install them easily via:

install.packages("tidyverse")

Now you’re ready to go. Enjoy!

If you have any problems, please let me know.

Analyze your own data!

As a scientist I collect data in various forms out of my experiments. Unlike some pillars such as physics and engineering, biological data are often qualitative. For example, a micrograph or a gel image have to be first interpreted by eye. After that, one can also quantify this sort of data and deal with the numbers. However, in my line of work, I also get lots of quantitative data. For the last couple of years I’ve been doing gene expression profiling using microarrays, for example, which yield huge amounts of numerical data. In many labs, such methods are completely outsourced to specialists. In a way, this is a valid attitude because an expert knows exactly how to deal with such data and how to represent the results that he achieves. However, there is also a downside for that approach. Such a collaboration is not always very efficient because of the nature of the communication between the scientist who conducts the project and the scientist who analyzes the data. It is easy if you are aiming at answering a number of questions using such a method and building your subsequent experiments based on those answers. But, such an exploratory method can potentially answer many more questions than you ask, if you approach the data from different perspectives. Communication goes into the game right at this moment. The two scientists have to sit together and analyze the data once again, together, and discuss over the results, then perhaps do this all over again. That’s really slow and eventually it gets on people’s nerves.

The alternative that I’ve been trying is to learn how to analyze my data myself. I’m definitely not as competent as a specialist, but whenever a new idea pops in my mind (or my colleagues’) I sit in front of my computer and do whatever I can to address new questions, using the same pile of data. The reason why I’m writing this article is to say that, my biggest ally in this quest of mine is R.

R is an open-source statistical analysis platform. It is designed as an interactive environment, like other statistics softwares, but most of the time you have to work on its command line. This means that you need to learn its programming language, the syntax, basic functions, data types, etc. However, you can then take advantage of its full power provided by thousands of high-level functions organized in hundreds of packages. Let me elaborate this a little bit.

R is a very high level programming language. Languages such as C or its derivatives are called low level, because they have a basic command structure and you either have to code higher level functions yourself or import them from libraries. That’s the con. The pro is that programs written in such languages run really fast because they execute only the code that’s necessary to do the job. R is on the other end of the spectrum. With a single command, you can read tons of data into a matrix-like data structure and operate with it in many different ways, but probably more slowly than C. If you don’t exaggerate the size of data or use a 10-year-old computer, you may not feel the difference at all. So here, being a high-level language is the power of R.

Secondly, R has a modular structure. All such functions (commands) are delivered in different packages. These packages are stored and distributed over the Internet via The Comprehensive R Archive Network (CRAN). Every package has its own documentation that explains how the functions work, what they do, and how you should use them.

At this point, one (especially a computer programmer) can argue that using R is for lazy people because they simply don’t want to spend time in implementing functions for themselves. I disagree with this idea. A function that is implemented by one person and deposited into a repository like CRAN is tested heavily by many other people in a very short time. Because R functions are also open source, any mistake or bug in the code is quickly caught and corrected. As an end-user, it is to my advantage that I don’t have to think about if the underlying function is reliable or not. The longer it stays online, the more robust it becomes.

For those who do bioinformatics, CRAN is definitely not the only repository. Bioconductor is a similar platform where particularly bioinformatics packages designed for R are distributed. The nice thing about both CRAN and Bioconductor packages is that, most of the time they are first published as a part of a research paper. This makes them first of all peer-reviewed. Secondly, the entire theory behind the functioning of the package, and many simulations are also published in the same articles. This makes R packages even more reliable from a scientific point of view.

After this much advertisement (for which I’m not paid of course) I’d like to recommend every biologist to build up the courage to start learning how to use R. Perhaps the best start would be downloading R as well as RStudio, another free software that makes it very easy to code in R. Then you can start reading the articles in Quick-R.

I’ll probably write more about this topic in the future. For the moment, I wish you enjoy the power of analyzing and plotting your own data just the way you always wanted to!

Occupy Gezi: What’s happening in Istanbul?

This post is about the recent events (May 31 – Jun 2, 2013) that started in a small recreational park in Taksim, İstanbul and expanded to entire Turkey.

tumblr_mnnreepM0V1ste7qoo1_1280

A protestor reading a book to the police who is ordered to disperse them.

There is a small green park adjacent to Taksim Square. Taksim is one of the most touristic and social areas in İstanbul, Turkey. In addition to its historic texture, it is where the majority of the pubs, theaters, cafes, restaurants, concert halls are located. It is simply the heart of the city. And this small park (Gezi Parkı, literally Wander Park), which is engulfed by the city, is as old as Taksim. Recently, the government decided to demolish the park and build yet another shopping mall in its place. (İstanbul already has more than 90 malls.) A group of environmentalists went to the park and started a sitting protest. They simply sat down on the park. They built tents and slept there over night, for about 4 days. Meanwhile, every day the construction team (actually in this case the destruction team) came to the park but the protestors did not allow them to do their jobs. Then the police took over and tried to convince people to leave. They stayed.

Here I should make a brief pause and explain the evolution of the attitude of the police in Turkey. Honestly, the police has never been the most trusted organization in the country. Stories were always told about physical and psychological torture in police stations when people were under custody. In the recent years, as the Adalet ve Kalkinma Partisi (Development and Justice Party, from this point on will be referred as AKP) and his leader the prime minister Recep Tayyip Erdoğan gained more political power, they reshaped the police force and started to direct them for their own agenda. This topic is much deeper than I can explain here, but you can find more about it in various blogs if you’re interested. In this process, the police developed a superiority complex just like the AKP prime ministers and the governors assigned by them. In public events, they used tear gas and batons against the public with no hesitation.

971214_10151402734891423_1738451402_n

Police teargassing unarmed protestors.

This attitude triggered the situation in Istanbul. Early morning on May 31st, the police attacked the protesters sleeping in their tents. The purpose was to dissipate them. They stayed.

Turkish people are used to such events, especially when the protest is political and the protestors use violence. But this time, it was much different. These people only wanted to protect the park, the trees, the nature. They had no knives, no guns, no flammable or explosive substances. They also didn’t carry any sign or flag of a political party or an organization. They were just the people, putting aside all the differences in ideas and acts, representing the only common element of every single human being on the planet.

That was the breaking point. Anyone with conscience and humanity in their heart went to Taksim to support them. At first, the group was small. People were still reluctant. They waited to see how things would develop. In addition, the TV was acting as if this never happened. The only source of true information was the social networks, especially Facebook and Twitter.

My personal opinion is that, if the police retreated on the first day, they would have won. But they were ordered to stay and spray! Whoever gave this order should now understand that he changed the course of history.

tumblr_mno1wulyeX1ste7qoo1_1280

A police officer shooting a pepper gas bomb onto the protestors.

As the police insisted, more and more people came to Taksim. The sprays were not enough, so the police switched to gas bombs. These are shot through special rifles and start releasing capsaicin in the gas form, the same substance in chilly pepper that burns your tongue and eyes.

After a couple of hours, the entire area was under the smoke of the pepper bombs. At that time, the police tanks (known as Panzer or TOMA in Turkish) reached the area. These are shielded heavy vehicles that can shoot pressured water on protestors, used to dissipate people. When targeted directly on a person, it can knock him down, wound him, cause head trauma and even death. The attack of the police continued the whole day and night with tanks, gas bombs and rubber bullets. There are unfortunately fatalities and severe injuries.

tumblr_mnotf0y5dy1stgjbvo1_1280

Overuse of pepper gas bomb in Taksim Square.

tumblr_mnprmefZqz1ste7qoo1_1280

A woman shot by a police panzer with pressurized water.

Meanwhile, people watching the events on the social media felt more and more urged to join the protest. Not only the residents of Istanbul, but also people from many other cities in Turkey got into busses to drive to Istanbul. They knew they couldn’t be there until Saturday and the police could have succeeded by then, but they would make the journey anyways. Those who couldn’t come arranged similar protests in their hometowns. As of the second day, the protests spread to the country.

tumblr_mnpe8oOtMQ1ste7qoo1_1280

People crossing the bridge from the Anatolian side of the Bosphorus Strait to reach Taksim Square (click to enlarge).

I was born and raised in Istanbul. I’ve lived there for 25 years. The city nowadays has a population of around 13 million. There are many crowded areas in the city and I’ve seen them all. I thought I saw crowd, real crowd, until seeing this image. These people were coming for a support on the second day of the protest.

tumblr_mnpnve7p7V1ste7qoo1_500

Medical students and doctors volunteering to help people.

Since the first day, many medical doctors volunteered to help the people in case of injuries. There were also many lawyers to support those who were taken under custody. They founded operation centers in wherever was safe and used the social media to drive the people in need to their location. They discovered a simple but effective method to neutralize the pepper spray using lemon juice and anti-acid solutions that many people keep at home.

What was really disappointing at that moment were the TV channels. Except for 2 minor but honorable channels, not a single network in Turkey broadcast the videos. The surveillance cameras in Taksim Square were already taken offline earlier on the first day. The only source of getting information were the video clips taken and uploaded by the people in the area. Some foreign agencies, on the other hand, cast live news from time to time all through the night. This is a perfect example of how enslaved the media in Turkey is.

981200_473238896086319_853185481_o

The left one with the penguins is CNN Turk, the Turkish branch of the international news network CNN. The right one is the original CNN. I really like penguins, by the way, but I think their documentary could wait for a few more days.

tumblr_mnqqiz3Xzv1ste7qoo1_1280

A photo from Izmir. Do you see the civilian guy with a wooden stick standing by the police? What is his purpose?

Another disappointment came from within. Some people initiated provocations during the protests. They vandalized shop windows and ATMs, they burnt fires. These were recorded by cameras to be used afterwards by the government to prove their claims that the protestors were terrorists. Other than that, there were no violent acts except for the police terror.

After the police forces already stepped back from Taksim, people remained in the area to clean up the mess of the police.

480227_10151523267078171_923865483_n

Protestors cleaning up the area after the police forces were withdrawn.

485451_10151542751812771_923675148_n

Coming to the significance of this event once more, I should say that it is very distinctive from the Middle Eastern “spring” movements. First of all, there was no fight against the state, it was only a protest. Although it started with environmental concerns, soon it became an act against the suppression and bullying of the government and its police forces. Secondly, although people chanted all the time “Tayyip istifa!” (Tayyip resign!), their purpose was not to overthrow the government. He is the elected prime minister of Turkey and he will remain so until he resigns or someone else is elected in his place. Turkey is already a democratic state since its own “spring” in 1923, it does not require another one. Therefore, please do not misinterpret this event.

Being a Turk myself, I can clearly see the motivation and mentality behind it. Let me explain. We have a saying, “söz gümüşse, sükut altındır” (were speech silver, silence is gold). Unlike western cultures, here in the east, wisdom requires to speak up only when necessary. Turkish people were silent until now, because it wasn’t the right time to speak. The government defied the law many times, suppressed many pillars of the society (especially journalists and intellectuals) and intervened in so many ways with people’s personal lives (anti-abortion law, alcohol limitation, etc.). Every time, they were given another chance because wisdom required patience until they realized that they were wrong. But this time, they went just too far. The prime minister, in his unfortunate speech on the TV during the events, defined the protestors as marginal, terrorist groups and explicitly stated that “he made up his mind” about the shopping mall project. Moreover, he said he would dissipate the protestors for whatever it takes. These protests all over the country were just a message to him that his power is only virtual, as much as the people bestow it to him.

270094_10151635680814769_105430253_n

A rightist (nationalist) and leftists side by side

Another remarkable thing in these events was the unification of people from very different political views. These people who normally exclude each other put aside their political views and stood side by side in this “metapolitical”, humanitarian action. Turkey has a deep political symbolism as any other symbolism it has. In the pictures from the area, one could see various hand gestures such as the ones on the left (the V-sign and closed fists usually used by the leftists, the “gray wolf” sign exclusively by the nationalists), specific clothing (many muslim women choose to cover their heads), etc. For the first time since decades, I see Turkish people so unified despite all the efforts of the foreign forces to separate them based on religion, ethnicity and political orientation, etc.

As I’m writing this post, the protests and police violence is still going on in other cities. Nevertheless, nothing will be the same in Turkey when this is over. It is too soon to say where it goes to, but it will definitely go somewhere. We’ll see that together.

P.S. During all these events, I was unfortunately watching them from abroad. In this post, I tried to be as objective and peaceful as I could. If you think there’s something wrong, please don’t hesitate to leave a comment.

P. P. S. For more images taken during the protests, see this tumblr page. Parental discretion advised. I also share the following images and try to explain what they are.

tumblr_mnpiuujnK41ste7qoo1_1280

Home-made recipe to neutralize pepper gas.

tumblr_mnoqbpi6bM1stgjbvo1_1280

“The revolution won’t be televised”, a reaction against the mainstream media that stood on the side of the government.

tumblr_mno6e35PC31ste7qoo1_1280

An unarmed protestor in surrendering position in front of a heavily shielded police troop.

984173_657789074236943_1938451066_n

International support from Turkish people and their foreign friends all around the world. Pictures show only some cities.

942928_667362503279248_1364720008_n

People cleaning up the park and its surrounding after the police withdrew.

935615_10151608109838810_880232840_n

“Dedicated to those who think it’s only about a tree”, a poster implying that the protest was actually to protect Atatürk’s principles such as secularism, republicanism, etc.

931216_464145747011486_541199532_n

The picture clearly shows that it’s all about simple maths.

6045_553578971350191_1746087900_n

Celebrities joined the protest and encouraged the people.