-
Notifications
You must be signed in to change notification settings - Fork 1
/
IPA.R
190 lines (166 loc) · 5.2 KB
/
IPA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
###FIRST TIME ONLY:
{
#install.packages("DT")
#install.packages("shiny")
#install.packages("shinydashboard")
#install.packages("shinydashboardPlus")
#install.packages("ggvis")
#install.packages("ggplot2")
#install.packages("dplyr")
}
###FIRST TIME ONLY ^^^
{
library(DT)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(ggvis)
library(ggplot2)
library(dplyr)
}
### Define UI for application that draws a scatter plot with file upload and col select
{shinyUI <- dashboardPage(
dashboardHeader(title = "Isotopic Peak Allocation"),
dashboardSidebar(
width = 350,
radioButtons(
"fileType_Input",
label = h4("Choose File type"),
choices = list(".csv/txt" = 1),
selected = 1,
inline = TRUE
),
fileInput(
'file1',
h4('Upload Your Data'),
accept = c(
'text/csv',
'text/comma-separated-values,text/plain',
'.csv'
)),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis Variable:",
choices = NULL),
selectInput(inputId = "y", label = "Select y-axis Variable:",
choices = NULL),
menuItem("Raw Data", tabName = "RawData",
icon = icon("table")),
menuItem("Data Summary", tabName = "DataSummary",
icon = icon("calculator")),
menuItem("Charts", tabName = "Charts",
icon = icon("chart-bar"))
)),
dashboardBody(
tabItems(
tabItem( tabName = "RawData",
fluidRow(
box(
title = "View Data",
width = NULL,
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
DT::dataTableOutput('contents'))
)),
tabItem(tabName = "DataSummary",
box(verbatimTextOutput("summary"))
),
tabItem(tabName = "Charts",
box(
plotOutput('plot',dblclick = "plot_click",width = '700', height = "700", click = "plot1_dblclick",
brush = brushOpts(
id = "plot1_brush",
resetOnNew = TRUE)),
verbatimTextOutput("info"),
tableOutput("tibbl"),
actionButton('save_to_global', "Save Table"))
)
))
)
}
### Run the Server with Reactive Functions
{
shinyServer <- function(input, output, session) {
vals <- reactiveVal(NULL)
ranges <- reactiveValues(x = NULL, y = NULL)
# Get the upload file
myData <- reactive({
req(input$file1)
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE)
data
})
output$contents <- DT::renderDataTable({
DT::datatable(myData())
})
observe({
data <- myData()
updateSelectInput(session, 'x', choices = names(data))
})
#gets the x variable name, will be used to change the plot legends
xVarName <- reactive({
input$x
})
observe({
data <- myData()
updateSelectInput(session, 'y', choices = names(data))
})
#gets the y variable name, will be used to change the plot legends
yVarName <- reactive({
input$y
})
output$summary <- renderPrint({
summary(myData())
})
#create the table or update the values.
observeEvent(c(input$plot_click$x, input$plot_click$y), {
if (is.null(vals())) {
vals(tibble(x = input$plot_click$x, y = input$plot_click$y))
} else {
vals(vals() %>%
add_row(x = input$plot_click$x, y = input$plot_click$y))
}
})
output$tibbl <- renderTable({
vals()
})
observeEvent(input$save_to_global, {
assign(print(input$x), vals(), envir = .GlobalEnv)
})
output$plot <- renderPlot({
df <- myData()
df2 <- vals()
if (is.null(vals())) {
ggplot(df, aes_string(x = input$x, y = input$y))+
geom_line()+
coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)
} else {
segment_data = data.frame(
x = c(vals()$x),
xend = c(vals()$x),
y = numeric(length(vals()$y)),
yend = c(vals()$y))
ggplot(df, aes_string(x = input$x, y = input$y))+
geom_line()+
coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)+
geom_point(data=df2, aes(x= c(vals()$x), y= c(vals()$y)),color="blue", shape="square")+
geom_segment(data=segment_data, aes(x=x, y=y, xend=xend, yend=yend), col="blue", lwd=1)
}
})
# When a double-click happens, check if there's a brush on the plot.
# If so, zoom to the brush bounds; if not, reset the zoom.
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
} else {
ranges$x <- NULL
ranges$y <- NULL
}
})
}
}
### Run the App
shinyApp(shinyUI, shinyServer)