Tutorial: An app in R shiny visualizing biopsy data — in a pharmaceutical company

We can therefore look at the description of the selectInput by?shiny::selectInputand seeWe now need to build the choices as the column names of the biopsy data set from 2-10.

the selected input will be the same.

We shall allow multiple inputs, so multiple will be set to TRUE.

Additionally we shall name the inputId "vars".

So we can replace the part output$variables inside the app.

R file with this:output$variables <- renderUI({ selectInput(inputId="vars", label = "Variables to use", choices = names(biopsy)[2:10], multiple = TRUE, selected = names(biopsy)[2:10] ) })And you’re done.

2) A Heatmap to see the outcome of clusteringThe basic heatmap function allows you to draw a heat map.

In this case we would like to change a few things.

We would like to change the clustering method inside the hclust function to a method defined by the user.

We can grab the user defined method by using input$method as we already defined this input field as a drop down menu.

We have to overwrite the default hclust method with our method by:my_hclust <- function(.

){ hclust(method=my_method,.

) } my_method <<- input$methodBe aware that you define a global variable my_method here, which suffices within the scope of this tutorial.

However, please keep in mind that global variables can be problematic in many other contexts and do your own research what best fits your application.

Now for the heatmap call we basically need to change a few inputs.

Please see the result:heatmap(x = t(as.

matrix(biopsy_numeric())), Rowv=NA, hclustfun=my_hclust, labCol =biopsy$"disease status", col=viridis(15) )We need to transform the biopsy_numeric matrix, as we would like to have the patients in columns.

As there is just a one dimensional clustering, we can switch of row labels by setting Rowv to NA.

The hclustfun is overwritten by our function my_hclust.

For coloring of the plot we use the viridis palette as it is a color blind friendly palette.

And the labels of our columns shall now not only the patient IDs but the disease status.

You can see the names of all columns we defined in the file R/utils.


There you see that the last column of biopsy is called "disease status".

This will be used to label each patient.

Now we got:output$plot1 <- renderPlot({ my_hclust <- function(.

){ hclust(method=my_method,.

) } my_method <<- input$method heatmap(x = t(as.

matrix(biopsy_numeric())), Rowv=NA, hclustfun=my_hclust, labCol =biopsy$"disease status", col=viridis(15) ) })Part 2 is done3) Plot a phylogenetic treeTo allow plotting a phylogenetic tree we provided you with a function called phyltree.

You can read the whole code of the function inside R/utils.


This function takes as inputsa numeric matrix > biopsy_numeric() CHECKThe clustering method > input$method CHECKThe number of clusters > input$nc CHECKA color function > viridis CHECKYou can read why to use () behind biopsy_numeric here.

The hard part are now the labels.

The biopsy_numeric data set is filtered by the # of patients.

Therefore we have to filter the labels, too.

Therefore we uselabels = biopsy %>% dplyr::select("disease status") %>% filter(row_number() <= input$patients) %>% mutate_all(as.

character) %>% pull("disease status")This is a workflow using functional programming with the R-package dplyr.

The function select allows us to just select the "disease status".

The filter function filters the number of rows.

The mutate_all function applies the as.

character function to all columns and finally we export the labels as a vector by using pull.

labels for the tree nodes > biopsy %>% .

CHECKThe final result looks like thisoutput$plot2 <- renderPlot({ phyltree( x = biopsy_numeric(), method = input$method, nc = input$nc, color_func = "viridis", labels = biopsy %>% dplyr::select("disease status") %>% filter(row_number() <= input$patients) %>% mutate_all(as.

character)%>% pull("disease status") ) })4) Create a table from clustering resultsNow we would also like to see for each patient in which cluster she was assigned.

Therefore we perform the clustering and tree cutting on our own:clust <- hclust(dist(biopsy_numeric()), method = input$method)cluster_assigment = cutree(clust, k = input$nc) #cluster assignementThe cluster_assignment is now a vector with numbers for the clusters for each patient such as c(1,2,1,1,1,2,2,1,.


This information can be helpful if we combine it with the patientID and the disease status that was named in the patients forms.

The task will be performed using the cbind function of R:out_table <- cbind( cluster_assigment, biopsy %>% filter(row_number() <= length(cluster_assigment)) %>% select(c(1,11)) )# cbindNow this table shall be sorted by the cluster_assigment to get a faster view on which patients landed in the wrong cluster.

out_table %>% arrange(cluster_assigment)The final code:output$cluster_table <- renderTable({ # ——— perform clustering —————- # Clustering via Hierarchical Clustering clust <- hclust(dist(biopsy_numeric()), method = input$method) cluster_assigment = cutree(clust, k = input$nc) #cluster assignement # Create a table with the clusters, Patient IDs and Disease status out_table <- cbind( cluster_assigment, biopsy %>% filter(row_number() <= length(cluster_assigment)) %>% select(c(1,11)) )# cbind # Order by cluster_assigment out_table %>% arrange(cluster_assigment) } )DoneWhat to do now?Now you can run the runApp() function.

If you choose 100 patients, 2 clusters, “ward.

D2” clustering and all variables you will see pretty fast, that the patients:1002945101627710180991096800could be identified as the patients that were clustered wrong.

Now you can go search for problems in clustering or look at the sheets of those patients.

By changing the labeling e.


using PatientIDs inside the phyltree function call, you can even check which other patients show close measurements to these patients.

Explore and play!Further readingGet to know other packages important for pharmaceutical applicationsVisual Elements of Data ScienceLearn how to build custom shiny inputsLearn how to start fast with a team running R.. More details

Leave a Reply