Below I have a working app with theiris
dataset. My actual dataset comes from a csv file into reactive context and is namedrt()
.The user also can choose different variables to correlate instead of only Sepal.Length
andSepal.Width
every time with input$lx1
and input$lx2
. I would like to know how to replace iris with rt()
, Sepal.Length
with input$lx1
and Sepal.Width
with input$lx2
in my code in order to make it work properly.
#ui.r
library(shiny)
library(ggplot2)
library(plotly)
fluidPage(
# App title ----
titlePanel(div("CROSS CORRELATION",style = "color:blue")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Input CSV-File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table",
shiny::dataTableOutput("contents")),
tabPanel("Correlation Plot",
tags$style(type="text/css", "
#loadmessage {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
padding: 5px 0px 5px 0px;
text-align: center;
font-weight: bold;
font-size: 100%;
color: #000000;
background-color: #CCFF66;
z-index: 105;
}
"),conditionalPanel(condition="$('html').hasClass('shiny-busy')",
tags$div("Loading...",id="loadmessage")
),
fluidRow(
column(3, uiOutput("lx1")),
column(3,uiOutput("lx2"))),
hr(),
fluidRow(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
column(3,uiOutput("td")),
column(3,uiOutput("an"))),
fluidRow(
plotlyOutput("sc"))
))
)))
#server.r
function(input, output) {
rt<-reactive({
req(input$file1)
csvdata <- read.csv(input$file1$datapath,
header = input$header
)
if(input$disp == "head"){
head(csvdata)
} else{
csvdata
}
csvdata$Lex1=as.numeric(levels(csvdata$Lex1))[csvdata$Lex1]
csvdata$Lex2=as.numeric(levels(csvdata$Lex2))[csvdata$Lex2]
csvdata$Lex3=as.numeric(levels(csvdata$Lex3))[csvdata$Lex3]
csvdata$Lex4=as.numeric(levels(csvdata$Lex4))[csvdata$Lex4]
csvdata$Lex5=as.numeric(levels(csvdata$Lex5))[csvdata$Lex5]
csvdata$Lex6=as.numeric(levels(csvdata$Lex6))[csvdata$Lex6]
csvdata$Lex7=as.numeric(levels(csvdata$Lex7))[csvdata$Lex7]
csvdata$Lex8=as.numeric(levels(csvdata$Lex8))[csvdata$Lex8]
csvdata$Lex9=as.numeric(levels(csvdata$Lex9))[csvdata$Lex9]
csvdata$Lex10=as.numeric(levels(csvdata$Lex10))[csvdata$Lex10]
csvdata$Lex11=as.numeric(levels(csvdata$Lex11))[csvdata$Lex11]
csvdata$Lex12=as.numeric(levels(csvdata$Lex12))[csvdata$Lex12]
capture.output(csvdata[rowSums(is.na(csvdata)) > 0,],file = "Missing_genes.csv")
row.has.na <- apply(csvdata, 1, function(x){any(is.na(x))})
csvdata2 <- csvdata[!row.has.na,]
csvdata2
})
output$contents <- shiny::renderDataTable({
iris })
output$lx1<-renderUI({
selectInput("lx1", label = h4("Select 1st Expression Profile"),
choices = colnames(iris[,1:4]),
selected = "Lex1")
})
output$lx2<-renderUI({
selectInput("lx2", label = h4("Select 2nd Expression Profile"),
choices = colnames(iris[,1:4]),
selected = "Lex2")
})
output$td<-renderUI({
radioButtons("td", label = h4("Trendline"),
choices = list("Add Trendline" = "lm", "Remove Trendline" = ""),
selected = "")
})
output$an<-renderUI({
radioButtons("an", label = h4("Correlation Coefficient"),
choices = list("Add Cor.Coef" = cor(subset(iris, select=c(input$lx1)),subset(iris, select=c(input$lx2))), "Remove Cor.Coef" = ""),
selected = "")
})
output$sc<-renderPlotly({
p1 <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2,text = "Species"))+
# Change the point options in geom_point
geom_point(color = "darkblue") +
# Change the title of the plot (can change axis titles
# in this option as well and add subtitle)
labs(title = "Cross Correlation") +
# Change where the tick marks are
scale_x_continuous(breaks = seq(0, 2.5, 30)) +
scale_y_continuous(breaks = seq(0, 2.5, 30)) +
# Change how the text looks for each element
theme(title = element_text(family = "Calibri",
size = 10,
face = "bold"),
axis.title = element_text(family = "Calibri Light",
size = 16,
face = "bold",
color = "darkgrey"),
axis.text = element_text(family = "Calibri",
size = 11))+
theme_bw()+
geom_smooth(method = input$td)+
annotate("text", x = 5, y = 5, label = as.character(input$an))+
geom_text(data=subset(iris, Sepal.Length > 6),
aes(Sepal.Length,Sepal.Width,label=Species))
# get clicked point
click_data <- event_data("plotly_click", source = "select")
# if a point has been clicked, add a label to the plot
if(!is.null(click_data)) {
pos <- click_data$pointNumber+1
label_data <- data.frame(x = iris$Sepal.Length[pos],
y = iris$Sepal.Width[pos],
label = iris$Species[pos],
stringsAsFactors = FALSE)
p1 <<- p1 +
geom_text(data = label_data,
aes(x = x, y = y, label = label),
inherit.aes = FALSE, nudge_y=.1)
}
ggplotly(p1,source = "select", tooltip = c("text")) %>%
layout(hoverlabel = list(bgcolor = "white",
font = list(family = "Calibri",
size = 9,
color = "black")))
})
}