R代写-T7
时间:2021-04-07
FGT_T7: Interactive Applications with Shiny
Simon Tomlinson
Introduction
Shiny is a framework that allows the easy development of interactive web applications. These
applications can be developed and used within RStudio and then hosted on a custom web server
(called a Shiny server) see https://rstudio.com/products/shiny/download-server/ or else be hosted
on the cloud https://www.shinyapps.io/. Shiny is typically used as an easy way to share R analysis
results, for example as supplementary data in publications or with collaborators who are not
comfortable with the R command line interface.
Shiny has many features and there is an extensive tutorial available at
https://shiny.rstudio.com/tutorial/. Here we are interested to use a subset of the available features
to build our first Shiny applications using RStudio. In this tutorial we create and application using
RStudio and then go on to create an interactive application to create an interactive heatmap. This is
a similar plot and using the same data as for FGT_T6: Exploiting Configuration in R & RStudio.
Introduction
• Login to RStudio on bioinfmsc3.mvm.ed.ac.uk or bioinfmsc4.med.ed.ac.uk.
• Create a working directory for today and setwd to that directory. It a good idea to clear the
workspace and restart R from within RStudio (see Session menu). Make sure you have saved
any previous work you want to keep. Note you can access the terminal within RStudio using
the menu items Tools->Terminal ->New Terminal. This will open up a Terminal Tab next to
the Console Window. From there you can issue Unix commands to create all the folders you
need and issue various Unix commands.
• First create a Shiny Application from RStudio
using File ->New File -> Shiny Web App... Make sure this is a single file
Shiny application.

• Give your application a descriptive name eg (not) myShinyApp and select single file web
app

• Click the Run App green arrow on the top of the app.R window. This will execute the
Shiny application


The executing web application displays a histogram that can be resized as required.



Close the web application by closing the browser window or tab containing the running application.
Do not close the main RStudio window.
The code for this application is displayed in RStudio. Notice how the application has two parts, the
first part that defines the User Interface (ui) and the second part that defines the server data
(server). These are defined by two functions are then called by shinyApp(ui = ui,
server = server). So when this page loads during execution it creates these two functions
and then executes the application. Note that the GUI creates a slider using sliderInput which
defines the interactive slider. The position of this slider is needed by the server part that builds
the plot using the position of the slider. This position is passed to the server function by Shiny and is
available as input$bins that comes from the ui. All inputs from the GUI are part of the input
object bins is the name of sliderInput that generated the value stored in input. If the slider
position changes the new position of the slider is passed to the server part as a new value of
input$bins and the plot is redrawn. The server part of the application could contain any R
code and any settings could be passed from the ui part- so it should be clear that using this simple
approach almost any R application could be represented in the GUI.
Of course in practice Shiny can be used to send data to send data back to the ui from the server and
create all sorts of interesting dynamic interactions. However, one important limitation is that the
GUI needs to run in (almost) real time- so we need to take care not to require the application to
perform large complex computations on the fly. Shiny does support some performance
optimisations (eg by caching results). However, in the next example we create a subset of the data
to be displayed and then save this data. We then load this data, process the settings and build the
plot.
The archive "FGT_T7_shiny_example_full.tar" contains the code for the second Shiny
example. Create a folder to store the application such as “myFirstApp2”. Copy this code from
the shared_files folder or from Learn and unpack this archive. Open the app.R file in Rstudio
and execute the application.

This basically creates an application that mimics the heatmap from FGT_T6. Notice from the code
how the different elements are used to create settings that are then used in pheatmap. Note how
it is sometimes necessary to process the parameters from the GUI before using these parameters
into pheatmap. The GUI can be easily modified to set other parameters taken by the pheatmap
programme.
Creating the Input files
This mostly utilises code from FGT_T6 and so it only briefly outlined here. We only need to generate
the experimental and saved annotation data once and then use this data from the Shiny scripts to
build the GUI that displays the results.
library(SummarizedExperiment)
library(org.Hs.eg.db)
#grab the example data set
data(airway, package="airway")
#process the expression data
expression <- assays(airway)[[1]]
variances <- apply(expression, 1, var)
expression <- expression[order(variances, decreasing=TRUE)[1:50],]
#match the annnotation
annotation <- merge(toTable(org.Hs.egSYMBOL), toTable(org.Hs.e
gENSEMBL2EG))
gene_names <- annotation$symbol[match(rownames(expression), an
notation$ensembl_id)]
rownames(expression)[! is.na(gene_names)] <- paste(gene_names[
! is.na(gene_names)], rownames(expression)[! is.na(gene_names)
], sep = ' / ')
#create the experiment data
experiment <- data.frame(colData(airway))
#save the data for use in the application
save(expression,file="expression.Rdata")
save(experiment,file="experiment.Rdata")


































































































学霸联盟


essay、essay代写