-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.R
135 lines (118 loc) · 4.47 KB
/
analysis.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
library(shiny)
library(shinyjs)
library(jsonlite)
library(ggplot2)
library(tidyverse)
THREADS <- 1
# Function to read the JSON files and combine them into a data frame
load_data <- function() {
data_list <- list()
# Loop through your 10 JSON files and read them into a list
for (i in 1:THREADS) {
file_name <- paste0("FTC_", i, ".json")
data <- fromJSON(file_name)
data_list[[i]] <- data
}
# Combine the data into a data frame
df <- do.call(rbind, data_list)
return(df)
}
load_data2 <- function() {
data_list <- list()
# Loop through your 10 JSON files and read them into a list
for (i in 1:THREADS) {
file_name <- paste0("TH_", i, ".json")
data <- fromJSON(file_name)
data_list[[i]] <- data
}
# Combine the data into a data frame
return(data_list)
}
# Shiny app
ui <- navbarPage("My Application",
tabPanel("Metabolite Distribution",
fluidPage(
useShinyjs(),
titlePanel("Metabolite Visualization"),
sidebarLayout(
sidebarPanel(
fluidPage(
selectInput("metabolite", "Select a Metabolite:", choices = colnames(load_data()), selected = "13dpg_c")),
),
mainPanel(
fluidPage(
plotOutput("barPlot")
)
)
)
)
),
tabPanel("Metabolite History",
useShinyjs(),
titlePanel("Metabolite Visualization"),
sidebarLayout(
sidebarPanel(
fluidPage(
selectInput("metabolite2", "Select a Metabolite:", choices = colnames(load_data()), selected = "13dpg_c"),
selectInput("item2", "Select a run:", choices = c(1:length(load_data2()), "all"), selected = "all"),
sliderInput("slider2", "Max x value:", min = 100, max = 1000, value = 1000)
),
),
mainPanel(
fluidPage(
plotOutput("scatterPlot")
)
)
)
),
tabPanel("Component 2")
)
server <- function(input, output) {
# Load data once when the app starts
data <- load_data()
d <- load_data2()
data2 <- data.frame(metabolite = character(), concentration = numeric(), item = factor())
for(i in 1:THREADS){
data_temp <- as.data.frame(d[[i]], check.names = FALSE)
df_plot <- pivot_longer(data_temp ,names_to = "metabolite", values_to = "concentration", cols = everything())
df_plot$item <- as.factor(i)
data2 <- rbind(data2, df_plot)
}
print(nrow(data2))
print(unique(data2$item))
output$barPlot <- renderPlot({
selected_metabolite <- input$metabolite
df_plot <- pivot_longer(as.data.frame(data), names_to = "metabolite", values_to = "concentration", cols = everything())
df_plot <- filter(df_plot, metabolite == selected_metabolite)
df_plot$concentration <- as.numeric(df_plot$concentration)
# Plot the selected metabolite across all 10 files
ggplot(df_plot, aes(x = metabolite, y = concentration)) +
geom_boxplot() +
labs(x = "File", y = "Concentration",
title = paste("Concentration of", selected_metabolite)) +
theme_minimal()
})
output$scatterPlot<- renderPlot({
selected_metabolite <- input$metabolite2
selected_run <- input$item2
df_plot <- filter(data2, metabolite == selected_metabolite)
df_plot <- df_plot %>% group_by(item) %>%
mutate(rank = row_number()) %>%
ungroup()
if(selected_run != "all"){
df_plot <- filter(df_plot, item == selected_run)
}
df_plot <- filter(df_plot, rank < input$slider2)
df_plot$concentration <- as.numeric(df_plot$concentration)
# Plot the selected metabolite across all 10 files
ggplot(df_plot, aes(y = concentration, x = rank, color = item)) +
geom_point() +
labs(x = "File", y = "Concentration",
title = paste("Concentration of", selected_metabolite, "of run", selected_run)) +
theme_minimal()
})
}
# Run the app
shinyApp(ui = ui, server = server)
#server()
Sys.sleep(200)