Category Archives: How-to

7 Easy Steps to Building your Own Shiny App from Scratch

Shiny is a powerful tool in R for you to show off your work to the world, without explaining all the complicated code behind your analysis. Because of its free and open-source development and deployment structure, sharing your methods or work online was never easier. For example, in our recent publication in the Journal of European Psychology Students my colleagues and me used Shiny to implement a network method in which we used the concept of network centrality to determine the most relevant articles in a research field. Because I believe there are a lot of benefits in sharing one’s methods, my hopes are that this blog post has the possibility to also inspire you to share your own work through Shiny. I will walk you though developing your own R Shiny application from scratch, tailoring it to your design choices, and publishing it online, in 7 easy steps. Together we will create an application that has a pleasant layout, allows the user to upload a data file, performs some calculations on selected variables and returns the output in a nice format. Code snippets will be given at every step along the way, which you can paste directly into R to see how the application develops from basic functionality to a fully functioning web app. The result can be found here.

Step 1: The Idea

The first step in any design process is thinking of an idea. This, for me, is also the most important step in the process. I that it helps me a great deal when the application and the features that I want in it are completely defined in my head. I will often draw the layout and the features that I have in mind on a piece of paper. The reason I do this is that, because you will build your Shiny application step-by-step, it would be a waste if you would have to go back and change things in the layout. So, I drew our imagined application on a piece of paper. I will make a one-page application that allows the user to upload some data, perform a correlation analysis and inspect the results.

Step 2: Launching Shiny

Shiny itself is an R package that works best in RStudio, which is the most used R environment among users. For RStudio to work properly, you need to separately install R on your computer. Once you open RStudio, in the new file menu, click the option to open a new Shiny Web App. RStudio will now prompt you with the screen below, asking you to specify the folder name for the application, whether you want a single file app or multiple file app, and the directory on your computer in which you want the app folder to be created. For ease of illustration we will create the application in a single file, but both options are okay to use. I am going to name the app directory “correlation” and it will go into one of my many folders, probably never to see the light of day again.

Tip. If you are familiar with Github, this is probably a good time to set up a repository in the application directory for version control.

Step 3: The Shiny Structure

Any Shiny application has two parts, the user interface (UI) function and the server function, which interact with each other. The UI function has an important visual task. It creates the html environment that allows the user to interact with the server function using buttons, sliders and other custom inputs while also rendering the visual output. All these components are placed in a layout in the UI function. The server function is responsible for all the calculations according to the input options that is receives from the UI function. The server function is where all the computational heavy-lifting is performed, like calculating the statistics for our correlation analysis. For example, if the user enables a checkbox (UI) that controls whether a regression line should be drawn in a scatter plot, the plot should be created (server) with a regression line and rendered in html (UI) to be displayed on the screen. The picture below shows how input and output are bounced around between the UI function and the server function and hopefully gives you an idea about the interaction between the two.

Tip. Try to get a feeling of the interaction between the UI and server functions. Ask questions like: what options should I have, and what should happen to the output when the user changes these options?

Step 4: Creating Your Layout

Let me say that there is a lot of packages out there that make shiny applications look pretty, and I agree that these downloadable add-ons can really improve the quality to your work and are worth looking into. Packages like Shiny Dashboard will make your applications look and feel fantastic. For now, I consider these topics advanced knowledge and I will provide you with a list of them at the end, so you can explore them yourself. However, they still require a basic understanding of Shiny concepts and therefore I will stick to the basics in this post.

When you first create a new Shiny app, R presents you with a default template application drawing a histogram. Check it out if you want, by pressing the “Run App” button in RStudio. However, the first step to any good and original app is to start from scratch. So, to start off, completely empty the pre-defined ui() and server() functions that R presents you with. We are going to make our own, better functions. All the information that I discuss here can also be found at the Shiny layout guide, which is a great resource when starting with Shiny.

The number of pages or tabs of your application will be defined by one of the page functions. Page functions are the top level of your UI function. For an application with multiple tab-pages, you can assign the navbarPage() function to the UI object. However, the default page equals a fluidPage() layout, which gives you a single page to work with. Since our application requires only one tab, let’s keep the default page function. The page will be assigned to the UI function. You can check out code snippet #1 for how I applied this in R. Remember you can check the application at any time by running the app through RStudio.

We are now going to further layout our page, since it still empty. In my applications, I often use a sidebar to indicate the difference between the input (options) and the output (result). With a sidebar, the application is divided in two parts; the sidebar for the input options and the main panel for the output display. The sidebar layout can be set by inserting the sidebarLayout() function into the fluid page. Since this layout can include a sidebar and a main panel, we will insert them into the sidebar layout by calling their appropriate layout functions as well, see code snippet #2. Finally, a title panel can be inserted above the sidebar layout with titlePanel(“Correlation”).

This skeleton will be the framework of the application. We will fill these layouts with drop-down menus, upload buttons, tables and plots. Remember that at this point, the server() function is still empty. We don’t need it yet, since we want to build up the entire user-interface first.

The Sidebar

Let’s start with filling the sidebar. Shiny is rendered in an html environment and uses recognizable html functions for text rendering. For example, we can display our intro text with the p() function, displaying regular text. However, if we would like to adjust the font size of the text, we could also use one of h1() through h6() to enlarge the font size. We can add our first interactive element, the upload button, by inserting the fileInput() function in the sidebar. Every interactive UI element has an inputId argument, which basically names the element. This name is used to refer to the element in the server function and should always be filled. For our data input, inputid=”datafile” will be an appropriate name. The file input allows the user to browse their computer for a file to upload to your application. The two drop-down menu’s for selecting variables can be inserted via the selectInput() function. We’ll leave the choices argument in these functions empty for now, I’m going to show you a neat little trick I’ve come across to automatically read in the variable names from the uploaded data set. Going down, we are going to insert a bold title for the plot header with p(strong(“Plot”)). Underneath this header there are two checkboxes, one for the regression line and one for the residuals, for which we use checkboxInput(). We repeat this process for the table header. Lastly, we add an update button using the actionButton() function. I find these buttons working very nicely, as they allow the application to only perform a calculation when all options are set correctly by the user. This saves computation time and makes your application faster, and easier to work with.

The Main Panel

The main panel is where we are going to display the output of our correlation analysis, namely the plot and the results table. It basically works the same as filling the sidebar panel. First, we want to display a header for the results which is slightly larger than regular text, so we’ll use h3(“Result”) for that. Adding plots and tables is extremely easy in Shiny and is done by output functions. These functions reserve a spot for the specific output in the main panel. The plot is reserved with the plotOutput() function and the table with tableOutput(). Remember that we have to give them appropriate input id’s. The result when we run our application is now a nice and clean layout, visually and in code.

Tip Online resources are your friend. Try to use them as much as possible, as they will increase your understanding of the process of making a Shiny application. Look for YouTube clips and blog posts to clarify the material for you.

Step 5: Creating Functionality

Let’s give the application some functionality by filling in the server function. The first logical step is to make sure that the data is read in correctly. For this purpose, we are going to make the drop-down menus recognize the variable names by performing a neat little trick in the server function. When we insert the following code snippet in our server function and run the application, you can see that it now recognizes our variable names in the data file as inputs.

Now it’s time to create the true functionality within the app, namely creating a scatter plot of two user-specified variables, inserting an optional regression line and residuals, and creating a table of the results. As stated in the previous paragraphs, an action button is ideal for monitoring user actions. Only when this button is clicked, the output will be updated. Monitoring buttons is done with the observeEvent() function, which links functionality to an event. In our case, the event is the click on the action button and the functionality is all that I mentioned above. The functionality will only get activated when the action button is clicked by the user. The code for what happens when the button is clicked is inside the second part of the observeEvent() function. This way of thinking makes Shiny a very intuitive and chronological way of working for me. For example, when the button is clicked, the following happens in order (see code snippet #4): 1) a data frame is made with both variables in it, 2) the correlation analysis is run, 3) the output data frame is made, 4) the table is rendered, 5) the plot is created, 6) the additional elements are drawn conditionally and, 7) the plot is rendered. Code snippet #4 shows this chronological order within the server function. Check out how it works by running the app locally if you feel like it.

Tip Programming in Shiny will be a lot of trial-and-error when you do it for the first couple of times. Refresh the browser page in which you are running the application to see the effect of your code changes immediately, instead of closing and opening it each time.

Step 6: Fine-tuning

When the functionality of your application is finished, it is time for the most fun part: making your Shiny app pretty. For example, use shinycssloaders to display some pretty graphics as plots are being created within the server function. It is extremely easy to use, just put withSpinner() around the plotOutput() part of code in the UI function. Other R packages that are be very useful for making your app beautiful are Shinyjs for JavaScript features like showing and hiding options, ShinyWidgets for upgraded and additional widgets, like turning checkboxes into toggle switches (picture below) and ShinyBS to add bootstrap html elements to your app. It is important to remember to load additional packages into your application by calling the library() function at the top of your R file.

Step 7: Publishing Your App

When every component of your application is finished, you are ready to publish your shiny app online. Next to the “Run App” button in RStudio you find a little blue icon   which, when clicked, brings you to the next screen:

If you do not have an account on https://shinyapps.io, create one as it is needed for publishing your application, and connect it to RStudio. When this is done, life gets easy. You just have to click the “Publish” button and all work will be done for you. The application will be uploaded to the shiny server and is accessible from your account, but also from a specific URL. The URL for my application for example, is https://koenderks.shinyapps.io/correlation/. Share your work and enjoy!

Continue reading

Koen Derks

Koen Derks is a PhD student at Nyenrode Business University. Before he started researching Bayesian statistics in financial auditing, he studied psychological methods at the University of Amsterdam (UvA), where his interests for statistics and programming developed itself. Today, he is still collaborating with the UvA to develop software tools for students and practitioners to make data analysis fun and intuitive.

More Posts

Facebooktwitterrss

Writing a Systematic Literature Review

Investigating concepts associated with psychology requires an indefinite amount of reading. Hence, good literature reviews are an inevitably needed part of providing the modern scientists with a broad spectrum of knowledge. In order to help, this blog post will introduce you to the basics of literature reviews and explain a specific methodological approach towards writing one, known as the systematic literature review. Continue reading

Eva Štrukelj

Eva Štrukelj is currently studying Clinical and Health Psychology at the University of Algarve in Portugal. Her main areas of interest are social psychology and health psychology. Regarding research, she is particularly curious about stigma and with it related topics.

More Posts

Facebooktwitterrss

Magical 7±2 Tips for Psychologists Participating in a Hackathon

A hackathon is an event, typically lasting for 24-48 hours, in which a group of people with diverse backgrounds come together to solve a problem by building a first working prototype of a solution (usually a web app, program or a utility).

There is something inherently likable, or dare I say, smart, about hackathons. They have a specific goal, your progress and results are measurable, getting a first working prototype is both achievable and realistic, and it will all be over in 24-48 hours. I have come to appreciate hackathons a lot over the last five months where I’ve participated in five, and won two of them with my teams. I would like to invite you to participate in one as well by giving you 7±2 tips to make your hackathon experience especially enjoyable. Continue reading

Taavi Kivisik

Data scientist and developer at Qlouder. While at the University of Tartu and University of Toronto, I was inspired to learn more about efficient learning and mnemonics. Midway through the studies I discovered my passion for research methodology and technical side of research, statistics and programming, also machine learning. I’m volunteering as a Lead Archivist for the Nordic Psychology Students’ Conference (NPSC). I'm former President of the Estonian Psychology Students’ Association and former Junior Editor at the Journal of European Psychology Students’ (JEPS). I sometimes tweet @tkivisik .

More Posts

Follow Me:
TwitterLinkedIn

Facebooktwitterrss

Open online education: Research findings and methodological challenges

With a reliable internet connection comes access to the enormous World Wide Web. Being so large, we rely on tools like Google to search and filter all this information. Additional filters can be found in sites like Wikipedia, offering a library style access to curated knowledge, but it too is enormous. In more recent years, open online courses has rapidly become a highly popular method of gaining easy access to curated, high quality, as well as pre-packaged knowledge. A particularly popular variety is the Massive Open Online Course, or MOOC, which are found on platforms like Coursera and edX. The promise – global and free access to high quality education – has often been applauded. Some have heralded the age of the MOOC as the death of campus based teaching. Others are more critical, often citing the high drop-out rates as a sign of failure, or argue that MOOCs do not or cannot foster ‘real’ learning (e.g., Zemsky, 2014; Pope, 2014). Continue reading

Tim van der Zee

Skeptical scientist. I study how people learn from educational videos in open online courses, and how we can help them learn better. PhD student at Leiden University (the Netherlands), but currently a visiting scholar at MIT and UMass Lowell. You can follow me on Twitter: @Research_Tim and read my blog at www.timvanderzee.com

More Posts - Website

Follow Me:
Twitter

Facebooktwitterrss

Introduction to Data Analysis using R

R Logo

R is a statistical programming language whose popularity is quickly overtaking SPSS and other “traditional” point-and-click software packages (Muenchen, 2015). But why would anyone use a programming language, instead of point-and-click applications, for data analysis? An important reason is that data analysis rarely consists of simply running a statistical test. Instead, many small steps, such as cleaning and visualizing data, are usually repeated many times, and computers are much faster at doing repetitive tasks than humans are. Using a point-and-click interface for these “data cleaning” operations is laborious and unnecessarily slow: Continue reading

Matti Vuorre

Matti Vuorre

Matti Vuorre is a PhD Student at Columbia University in New York City. He studies cognitive psychology and neuroscience, and focuses on understanding the mechanisms underlying humans' metacognitive capacities.

More Posts - Website

Facebooktwitterrss

Python Programming in Psychology – From Data Collection to Analysis

Why programming?

Programming is a skill that all psychology students should learn. I can think of so many reasons on why, including automating boring stuff, and practicing problem solving skills through learning to code and programming.  In this post I will focus on two more immediate ways that may be relevant for a Psychology student, particularly during data collection and data analysis. For a more elaborated discussion on the topic read the post on my personal blog: Every Psychologist Should Learn Programming.

Here is what we will do in this post:

  • Basic Python by example (i.e., a t-test for paired samples)
  • Program a Flanker task using the Python library Expyriment
  • Visualise and analyse data Continue reading
Erik Marsja

Erik Marsja

Erik Marsja is a Ph.D. student at the Department of Psychology, Umeå University, Sweden. In his dissertation work, he examines attention and distraction from a cross-modal and multisensory perspective (i.e., using auditory, visual, and tactile stimuli). Erik is teaching in both qualitative and quantitative research methods, applied cognitive psychology, cognitive psychology, and perception. In the lab group he has been part of since his Bachelor's thesis he has been responsible for programming his own, and some of the other members and collaborators, experiments. Programming skills have been, and will be, something valuable for his research and his career. Some of the code that have been used can be found on his GitHub page.

More Posts - Website

Facebooktwitterrss

Meet the Authors

Do you wish to publish your work but don’t know how to get started? We asked some of our student authors, Janne Hellerup Nielsen, Dimitar Karadzhov, and Noelle Sammon, to share their experience of getting published. Continue reading

Leonor Agan

Leonor is a postgraduate student at the Centre for Clinical Brain Sciences (University of Edinburgh), pursuing a MSc in Neuroimaging for Research. She holds a BSc in Psychology from the Ateneo de Manila University in the Philippines and a BA in Psychology from Maynooth University in Ireland.  She worked as a Research Assistant in Trinity College Institute of Neuroscience, Complex and Adaptive Systems Laboratory (University College Dublin), and Psychology Department (University College Dublin). Her research interests include cognition, memory, and neuroimaging techniques, specifically diffusion MRI and its applications in disease. She is also an Editor of the Journal of European Psychology Students. Find her on Twitter @leonoragan and link in with her.

More Posts

Facebooktwitterrss

Bayesian Statistics: Why and How

bayes_hot_scaled

Bayesian statistics is what all the cool kids are talking about these days. Upon closer inspection, this does not come as a surprise. In contrast to classical statistics, Bayesian inference is principled, coherent, unbiased, and addresses an important question in science: in which of my hypothesis should I believe in, and how strongly, given the collected data?  Continue reading

Fabian Dablander

Fabian Dablander is doing a PhD at the Department of Psychological Methods at the University of Amsterdam. You can find more information at https://fdabl.github.io/.

More Posts - Website

Follow Me:
Twitter

Facebooktwitterrss

How not to worry about APA style

If you have gone through the trouble of picking up a copy of the Publication Manual of the American Psychological Association (APA, 2010), I’m sure your first reaction was similar to mine: “Ugh! 272 pages of boredom.” Do people actually read this monster? I don’t know. I don’t think so. I know I haven’t read every last bit of it. You may be relieved to hear that your reaction resonates with some of the critique that has been voiced by senior researchers in Psychology, such as Henry L. Roediger III (2004). But let’s face it: APA style is not going anywhere. It is one of the major style regimes in academia and is used in many fields other than Psychology, including medical and other public health journals. And to be fair, standardizing academic documents is not a bad idea. It helps readers to efficiently access the desired information. It helps authors by making the journal’s expectations regarding style explicit, and it helps reviewers to concentrate on the content of a manuscript. Most importantly, the guidelines set a standard that is accepted by a large number of outlets. Imagine a world in which you had to familiarize yourself with a different style every time you chose a new outlet for your scholarly work. Continue reading

Frederik Aust

Frederik Aust

Frederik Aust is pursuing a PhD in cognitive psychology at the University of Cologne. He is interested in mathematical models of memory and cognition, open science, and R programming.

More Posts - Website

Facebooktwitterrss

Make the Most of Your Summer: Summer Schools in Europe


11051177_10205216017873360_1194271846_mWhy should you attend Summer Schools?

To put it simply: there is no better way to learn about psychology (and related disciplines), to travel, and to meet new people, all at the same time! Summer schools offer the opportunity to explore areas of psychology that might not be taught at your university, or to really explore a subject, seeing as this scheme allows you to  focus your work on one topic in the company of students who are enthusiastic about the same subject. Last year, I attended a summer school on Law, Criminology and Psychology – coming from Germany, where Criminology is in the Law faculty, that was my opportunity to learn more about eye-witness accounts, lie detection, psychopathy, and how to interrogate children. Aside from classic lectures, summer schools often include seminars and group work. Continue reading

Katharina Brecht

Katharina Brecht

After finishing her PhD at the University of Cambridge, Katharina is currently a Postdoc in the Institute of Neurobiology at the University of Tübingen. Her research interests revolve around the mechanisms of social and causal cognition in animals.

More Posts

Facebooktwitterrss