My first R Shiny Web Application using breast cancer data

[This article was first published on analytics for fun, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I love the idea of making non-R users playing with my datasets. Thanks to R Shiny package this is is now possible and I am going to post here my code for a simple web application.

But first a few lines about Shiny package and my dataset. Okay…if you are in a hurry and want to go straight to check my app, here is the link http://spark.rstudio.com/marqui/breastcancer/


Shiny package


Shiny is a new package created by RStudio (http://www.rstudio.com/shiny/) that makes it very easy to build interactive web applications with R. Yes, that means that anyone can use it, interact with your data and gain insights from your analysis results.



The dataset & my app


The web application I’ve built allows you to explore and visualize data about new cases of breast cancer across countries. The app is quite simple: you first select the continent you are interest in, and you get:
  1. a table of countries sorted from the highest breast cancer rate to the lowest
  2. a world map with data plotted over it

My dataset includes breast cancer data for 174 countries and it was originally collected by ARC (International Agency for Research on Cancer) in 2002. Actually my dataset includes also several other socio-economic variables (which I am not going to explore in this occasion) and went through a previous “cleaning stage” where I added further variables. Check my previous post for more info on that how to plot data over a map with R.   


My code


To build a Shiny app you need to create two separate components (scripts): a user interface definition and a server script. The first is named ui.R and the latter server.R ; bot must be located in the same folder (your shiny app directory). Finally, to run it, you need to first make sure you are pointing to your app directory and execute these commands:

> library (shiny) # to load the shiny library

> runApp() # or runApp(“location of your app folder”) if that is not set as your working directory

Here below are my scripts for the user interface and server components. Oh yes, if you have not clicked yet on the link above, again this is the link where you can access my app http://spark.rstudio.com/marqui/breastcancer/

## server.R
library(shiny)
library(maps)
# Source my breast cancer file with data
breastdata<- read.csv(“~/Rworkdir/data/mergedCleaned.csv”)
# Create a smaller data frame including only my variables of #interest:country, breast cancer rate, continent
x<- breastdata[,c(1,5,17)]
# Define server logic to plot breast cancer data for various #continents/countries
shinyServer(function(input,output){
# Sentence presenting the table in a reactive expression
tableText<- reactive({
paste(“This table ranks countries by number of breast cancer new cases per 100,000 women. You have selected all countries in”, input$continent)
})
# now I return the tabletText for printing as a caption
output$caption<- renderText({
tableText()
})
# Plot a table that ranks countries (from most affected to the least) by breast cancer rate
output$rank<- renderTable({
if (input$continent==”the World”){
head(x[order(x$breastcancerper100th, decreasing=T),], n= input$obs)
} else {
xcont<- subset(x, continent==input$continent)
head(xcont[order(xcont$breastcancerper100th, decreasing=T),], n= input$obs)
}
})
# Plot a world map visualizing breast cancer incidence. The radius of the circle correspond
# to the number of breast cancer new cases (larger radius = more breast cancer new cases)
output$map<- renderPlot({
map(“world”,col=”gray90”, fill=TRUE)
if (input$continent==”the World”){
radius <- 3^sqrt(breastdata$breastcancerper100th)
symbols(breastdata$lon, breastdata$lat, bg = “blue”, fg = “red”,
lwd = 1, circles = radius, inches = 0.175, add = TRUE,
main= “New cases of breast cancer in the world, 2002”)
} else {
radius <- 3^sqrt(breastdata[breastdata$continent==input$continent,"breastcancerper100th"])
symbols(breastdata[breastdata$continent==input$continent,”lon”],
breastdata[breastdata$continent==input$continent,”lat”],
bg = “blue”, fg = “red”,
lwd = 1, circles = radius, inches = 0.175, add = TRUE,
main= “New cases of breast cancer in the world, 2002”)
}
})
})
view raw server.R hosted with ❤ by GitHub
##ui.R
library(shiny)
# Define UI for my first breast cancer application
shinyUI(pageWithSidebar(
# my Application title
headerPanel(“Breast Cancer Rate across Continents”),
# a sidebar with controls to select the continent for wich I want to see the distribution
sidebarPanel(
selectInput(“continent”,”Continent:”,
list(“All World”=”the World”,
”Asia”=”AS”,
”West Europe”= “WE”,
”East Europe”= “EE”,
”North America”=”NORAM”,
”Latin America”=”LATAM”,
”Africa”=”AF”,
”Oceania”=”OC”
)),
numericInput(“obs”, “Choose how many countries to view in the table”, 3)
),
mainPanel(
h4(textOutput(“caption”)),
tableOutput(“rank”),
helpText(“Below we report the same data of the table, on a world map. The size of the circle corresponds to the number of breast cancer new cases on that country.”),
plotOutput(“map”),
helpText(“Check out my blog if you want more info about the code and dataset I used: http://www.analyticsforfun.com/”)
)
))
view raw ui.R hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: analytics for fun.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)