Skip to content

Commit

Permalink
Merge pull request #40 from cct-datascience/text
Browse files Browse the repository at this point in the history
Add text placeholders and year filter
  • Loading branch information
Aariq committed Aug 24, 2022
2 parents 4de39a1 + eec0201 commit cf6f46b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 22 deletions.
38 changes: 28 additions & 10 deletions app/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ articles <- read_csv("articles_clean.csv")
# UI ----------------------------------------------------------------------

ui <- fluidPage(
h1("Title"),
p("A short description could go here, but probably shouldn't be too long or you'll have to scroll down quite a bit to get to the rest of the app."),
fluidRow(

## Input panel -------------------------------------------------------------
panel(
sliderInput(
inputId = "year_range",value = c(min(articles$year), max(articles$year)),
label = "Year Range",
min = min(articles$year),
max = max(articles$year),
sep = "",
dragRange = TRUE
),
selectizeGroupUI(
id = "my-filters",
params = list(
Expand Down Expand Up @@ -46,27 +58,32 @@ ui <- fluidPage(
# Server ------------------------------------------------------------------

server <- function(input, output, session) {

sankey_data <- callModule(

# Filter data by selectize input ------------------------------------------
sankey_filtered <- callModule(
module = selectizeGroupServer,
id = "my-filters",
data = articles,
vars = c("domain", "biomarker", "collection",
"frequency", "communication", "behavior", "outcome")
)

)

#render the plot

# Render the plot --------------------------------------------------------
output$sankey <- renderPlot({
#Take a dependency on the refresh button
input$refresh


## Filter data ------------------------------------------------------------
#use isolate() so the plot only updates when the button is clicked, not when
#sankey_data is updated
#could still update highlighting with every change by using sankey_data() in
#a scale_color* call possibly. Worry about this later in case we don't end
#up sticking with ggplot
plotdf <- isolate(sankey_data()) %>%
sankey_data <- isolate(sankey_filtered() %>%
filter(year >= input$year_range[1] & year <= input$year_range[2]))

plotdf <- sankey_data %>%
ggsankey::make_long(
domain,
biomarker,
Expand All @@ -76,7 +93,8 @@ server <- function(input, output, session) {
behavior,
outcome
)


# Build the Plot --------------------------------------------------------
ggplot(plotdf,
aes(x = x,
next_x = next_x,
Expand Down Expand Up @@ -110,14 +128,14 @@ server <- function(input, output, session) {
xlab(NULL)
})

# download button function
# Download button function ----------------------
output$download <- downloadHandler(
filename = function() {
#constructs file name based on today's date
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(sankey_data(), file)
write.csv(sankey_data, file)
}
)
}
Expand Down
90 changes: 90 additions & 0 deletions notes/notes on new plotly code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
library(tidyverse)
library(ggsankey)
library(networkD3)

df <- tribble(
~doi, ~domain, ~freq, ~outcome,
1 , "diabetes", "once", "blood sugar reduction",
1 , "diabetes", "once", "weight loss",
2 , "diabetes", "more than once", "blood sugar reduction",
3 , "obesity", "once", "weight loss",
3 , "obesity", "once", "activity level", #TODO tooltip from obsity to once should be 1 paper.
4 , "obesity", "more than once", "weight loss",
5 , "obesity", "more than once", "weight loss"
)


#ggsankey version:

df_made_long <- ggsankey::make_long(df, domain, freq, outcome, value = doi)

ggplot(df_made_long,
aes(x = x,
next_x = next_x,
node = node,
next_node = next_node,
fill = factor(node),
label = node)
) +
geom_sankey() +
geom_sankey_label() +
theme(legend.position = "none")


# Networkd3 version:

# Count number of observations for each link
results_n <-
df_made_long %>%
group_by(node, next_node) %>%
summarize(n = n(),
n_refs = length(unique(value)))

# nodes
nodes <-
df_made_long %>%
group_by(node) %>%
summarize(N_REFS = length(unique(value))) %>%
rename(name = node) %>%
mutate(node = 0:(n()-1)) %>%
as.data.frame()


# edges
# Create links dataframe
links <-
left_join(results_n, nodes, by = c("node" = "name")) %>%
left_join(nodes, by = c("next_node" = "name")) %>%
ungroup() %>%
rename(source = node.y,
target = node.y.y,
value = n) %>%
select(source, target, value, n_refs) %>%
na.omit() %>%
as.data.frame()

sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
units = 'observations')

# try a plotly version
library(plotly)
plot_ly(
type = "sankey",
orientation = "h",

node = list(
label = nodes$name,
pad = 15,
thickness = 20,
line = list(
color = "black",
width = 0.5
)
),

#TODO: put both numbers of observations and number of # refs/papers
link = links,
hoverinfo = "text",
# hoverlabel = ,
)
41 changes: 29 additions & 12 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
]
},
"Packages": {
"Cairo": {
"Package": "Cairo",
"Version": "1.6-0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "60aada7beac23aa3e9b219485d6b2da8",
"Requirements": []
},
"DBI": {
"Package": "DBI",
"Version": "1.1.3",
Expand Down Expand Up @@ -631,10 +623,10 @@
},
"httr": {
"Package": "httr",
"Version": "1.4.3",
"Version": "1.4.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "88d1b310583777edf01ccd1216fb0b2b",
"Hash": "57557fac46471f0dbbf44705cc6a5c8c",
"Requirements": [
"R6",
"curl",
Expand All @@ -654,6 +646,19 @@
"uuid"
]
},
"igraph": {
"Package": "igraph",
"Version": "1.3.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "40e38ff98f90967805831b6234afd05f",
"Requirements": [
"Matrix",
"magrittr",
"pkgconfig",
"rlang"
]
},
"isoband": {
"Package": "isoband",
"Version": "0.2.5",
Expand Down Expand Up @@ -825,6 +830,18 @@
"colorspace"
]
},
"networkD3": {
"Package": "networkD3",
"Version": "0.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "38310ec4ddb1398359abdd603c151067",
"Requirements": [
"htmlwidgets",
"igraph",
"magrittr"
]
},
"nlme": {
"Package": "nlme",
"Version": "3.1-157",
Expand Down Expand Up @@ -1079,10 +1096,10 @@
},
"rstudioapi": {
"Package": "rstudioapi",
"Version": "0.13",
"Version": "0.14",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "06c85365a03fdaf699966cc1d3cf53ea",
"Hash": "690bd2acc42a9166ce34845884459320",
"Requirements": []
},
"rvest": {
Expand Down

0 comments on commit cf6f46b

Please sign in to comment.