从导入为 csv 的数据文件中在闪亮的应用程序中显示 Sankey 图
Display Sankey graph in shiny application from a data file imported as csv
我无法通过使用来自 networkd3. Well, what I wanted to do is to enter a table as a squared matrix with all nodes and it's cases contain the weights! Simply I couldn't be able to generate Sankey graph this way described in the first part here : enter link description here 的 sankeyNetwork()
作为 CSV 上传的数据在闪亮的应用程序上显示 Sankey 图
目标是方便应用程序的用户添加数据,尽管他们没有义务输入 "source"、"target" 和“权重”,它只会生成 link如果与矩阵案例相关联的两个节点之间的权重重新组合了一个非零的权重! Link 我给出了邻接矩阵的命令,它在控制台上运行良好,但无法将它变成一个闪亮的应用程序
server.R
library(shiny)
require(networkD3)
require(igraph)
shinyServer(function(input, output) {
data <- reactive({
file1 <- input$myData
if (is.null(file1)) {
return()
}
read.csv(file = file1$datapath,
sep = input$sep,
header = FALSE)
})
label <- reactive({
file1 <- input$myLabels
if (is.null(file1)) {
return()
}
read.csv(file = file1$datapath,
sep = input$sep,
header = FALSE)
})
matrix <- function(data) {
m = as.matrix(data)
n = nrow(m) - 1
colnames(m) <- c(0:n)
return(m)
}
Nodes <- function(label) {
p = as.data.frame(label$Label)
colnames(p) <- as.factor(colnames(p))
return(p)
}
Links1 <- function(matrix) {
p = graph_from_adjacency_matrix(matrix,
mode = "directed",
weighted = T,
diag = T)
L = get.data.frame(p)
return(L)
}
Links1$from <- function(Links1) {
p = Links1$from
return(p)
}
Links1$to <- function(Links1) {
j = Links1$to
return(j)
}
Links1$weight <- function(Links1) {
o = Links1$weight
return(o)
}
output$plot <- renderSankeyNetwork({
sankeyNetwork(
Links = Links1,
Nodes = Nodes,
Source = ' Links1$from',
Target = 'Links1$to',
Value = 'Links1$weight',
NodeID = "label$Label",
fontSize = 30,
nodeWidth = 30
)
})
output$filedf <- renderTable({
if (is.null(data())) {
return ()
}
input$file
})
output$sum <- renderTable({
if (is.null(data())) {
return ()
}
summary(data())
})
output$table <- renderTable({
if (is.null(data())) {
return ()
}
data()
})
output$tb <- renderUI({
if (is.null(data()))
h5("Powered by",
tags$img(
src = 'RStudio-Ball.png',
heigth = 200,
width = 200
))
else
tabsetPanel(
tabPanel("About file", tableOutput("filedf")),
tabPanel("Data",
tableOutput("table")),
tabPanel("Summary", tableOutput("sum"))
)
})
})
ui.R
require(networkD3)
library(shiny)
require(igraph)
shinyUI(fluidPage(
titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("myData", "Upload your data"),
fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
helpText("Default max. file size is 5MB"),
radioButtons(
inputId = 'sep',
label = 'Separator',
choices = c(
Comma = ',',
Semicolon = ';',
Tab = '\t',
Space = ''
),
selected = ';'
)
),
mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
)
))
不确定您要对您声明的所有那些不正确且最终未使用的函数执行什么操作,但也许这个最小化的示例将帮助您入门...
(全部在一个 R 文件中,不管文件名是什么)
library(shiny)
ui <- fluidPage(titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("myData", "Upload your data"),
fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
helpText("Default max. file size is 5MB"),
radioButtons(
inputId = 'sep',
label = 'Separator',
choices = c(
Comma = ',',
Semicolon = ';',
Tab = '\t',
Space = ' '
),
selected = ';'
)
),
mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
))
server <- function(input, output) {
data <- reactive({
file1 <- input$myData
if (is.null(file1)) {
return(NULL)
}
read.csv(file = file1$datapath,
sep = input$sep,
header = TRUE)
})
label <- reactive({
file1 <- input$myLabels
if (is.null(file1)) {
return(NULL)
}
read.csv(file = file1$datapath,
sep = input$sep,
header = TRUE)
})
output$plot <- renderSankeyNetwork({
print(names(data()))
sankeyNetwork(
Links = data(),
Nodes = label(),
Source = 'source',
Target = 'target',
Value = 'value',
NodeID = "name",
fontSize = 30,
nodeWidth = 30
)
})
}
shinyApp(ui = ui, server = server)
并使用以下 CSV 文件作为示例...
myData.csv
source;target;value
0;1;124.729
1;2;0.597
1;3;26.862
1;4;280.322
1;5;81.144
6;2;35
7;4;35
8;9;11.606
10;9;63.965
9;4;75.571
11;12;10.639
11;13;22.505
11;14;46.184
15;16;104.453
15;14;113.726
15;17;27.14
15;12;342.165
15;18;37.797
15;19;4.412
15;13;40.858
15;3;56.691
15;20;7.863
15;21;90.008
15;22;93.494
23;24;40.719
25;24;82.233
5;13;0.129
5;3;1.401
5;26;151.891
5;19;2.096
5;12;48.58
27;15;7.013
17;28;20.897
17;3;6.242
28;18;20.897
29;15;6.995
2;12;121.066
2;30;128.69
2;18;135.835
2;31;14.458
2;32;206.267
2;19;3.64
2;33;33.218
2;20;4.413
34;1;4.375
24;5;122.952
35;26;839.978
36;37;504.287
38;37;107.703
37;2;611.99
39;4;56.587
39;1;77.81
40;14;193.026
40;13;70.672
41;15;59.901
42;14;19.263
43;42;19.263
43;41;59.901
4;19;0.882
4;26;400.12
4;12;46.477
26;15;525.531
26;3;787.129
26;11;79.329
44;15;9.452
45;1;182.01
46;15;19.013
47;15;289.366
myLabels.csv
name
Agricultural 'waste'
Bio-conversion
Liquid
Losses
Solid
Gas
Biofuel imports
Biomass imports
Coal imports
Coal
Coal reserves
District heating
Industry
Heating and cooling - commercial
Heating and cooling - homes
Electricity grid
Over generation / exports
H2 conversion
Road transport
Agriculture
Rail transport
Lighting & appliances - commercial
Lighting & appliances - homes
Gas imports
Ngas
Gas reserves
Thermal generation
Geothermal
H2
Hydro
International shipping
Domestic aviation
International aviation
National navigation
Marine algae
Nuclear
Oil imports
Oil
Oil reserves
Other waste
Pumped heat
Solar PV
Solar Thermal
Solar
Tidal
UK land based bioenergy
Wave
Wind
我无法通过使用来自 networkd3. Well, what I wanted to do is to enter a table as a squared matrix with all nodes and it's cases contain the weights! Simply I couldn't be able to generate Sankey graph this way described in the first part here : enter link description here 的 sankeyNetwork()
作为 CSV 上传的数据在闪亮的应用程序上显示 Sankey 图
目标是方便应用程序的用户添加数据,尽管他们没有义务输入 "source"、"target" 和“权重”,它只会生成 link如果与矩阵案例相关联的两个节点之间的权重重新组合了一个非零的权重! Link 我给出了邻接矩阵的命令,它在控制台上运行良好,但无法将它变成一个闪亮的应用程序
server.R
library(shiny)
require(networkD3)
require(igraph)
shinyServer(function(input, output) {
data <- reactive({
file1 <- input$myData
if (is.null(file1)) {
return()
}
read.csv(file = file1$datapath,
sep = input$sep,
header = FALSE)
})
label <- reactive({
file1 <- input$myLabels
if (is.null(file1)) {
return()
}
read.csv(file = file1$datapath,
sep = input$sep,
header = FALSE)
})
matrix <- function(data) {
m = as.matrix(data)
n = nrow(m) - 1
colnames(m) <- c(0:n)
return(m)
}
Nodes <- function(label) {
p = as.data.frame(label$Label)
colnames(p) <- as.factor(colnames(p))
return(p)
}
Links1 <- function(matrix) {
p = graph_from_adjacency_matrix(matrix,
mode = "directed",
weighted = T,
diag = T)
L = get.data.frame(p)
return(L)
}
Links1$from <- function(Links1) {
p = Links1$from
return(p)
}
Links1$to <- function(Links1) {
j = Links1$to
return(j)
}
Links1$weight <- function(Links1) {
o = Links1$weight
return(o)
}
output$plot <- renderSankeyNetwork({
sankeyNetwork(
Links = Links1,
Nodes = Nodes,
Source = ' Links1$from',
Target = 'Links1$to',
Value = 'Links1$weight',
NodeID = "label$Label",
fontSize = 30,
nodeWidth = 30
)
})
output$filedf <- renderTable({
if (is.null(data())) {
return ()
}
input$file
})
output$sum <- renderTable({
if (is.null(data())) {
return ()
}
summary(data())
})
output$table <- renderTable({
if (is.null(data())) {
return ()
}
data()
})
output$tb <- renderUI({
if (is.null(data()))
h5("Powered by",
tags$img(
src = 'RStudio-Ball.png',
heigth = 200,
width = 200
))
else
tabsetPanel(
tabPanel("About file", tableOutput("filedf")),
tabPanel("Data",
tableOutput("table")),
tabPanel("Summary", tableOutput("sum"))
)
})
})
ui.R
require(networkD3)
library(shiny)
require(igraph)
shinyUI(fluidPage(
titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("myData", "Upload your data"),
fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
helpText("Default max. file size is 5MB"),
radioButtons(
inputId = 'sep',
label = 'Separator',
choices = c(
Comma = ',',
Semicolon = ';',
Tab = '\t',
Space = ''
),
selected = ';'
)
),
mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
)
))
不确定您要对您声明的所有那些不正确且最终未使用的函数执行什么操作,但也许这个最小化的示例将帮助您入门...
(全部在一个 R 文件中,不管文件名是什么)
library(shiny)
ui <- fluidPage(titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("myData", "Upload your data"),
fileInput("myLabels", "Upload its label as ID/Label/Nodes"),
helpText("Default max. file size is 5MB"),
radioButtons(
inputId = 'sep',
label = 'Separator',
choices = c(
Comma = ',',
Semicolon = ';',
Tab = '\t',
Space = ' '
),
selected = ';'
)
),
mainPanel(sankeyNetworkOutput("plot"), uiOutput("tb"))
))
server <- function(input, output) {
data <- reactive({
file1 <- input$myData
if (is.null(file1)) {
return(NULL)
}
read.csv(file = file1$datapath,
sep = input$sep,
header = TRUE)
})
label <- reactive({
file1 <- input$myLabels
if (is.null(file1)) {
return(NULL)
}
read.csv(file = file1$datapath,
sep = input$sep,
header = TRUE)
})
output$plot <- renderSankeyNetwork({
print(names(data()))
sankeyNetwork(
Links = data(),
Nodes = label(),
Source = 'source',
Target = 'target',
Value = 'value',
NodeID = "name",
fontSize = 30,
nodeWidth = 30
)
})
}
shinyApp(ui = ui, server = server)
并使用以下 CSV 文件作为示例...
myData.csv
source;target;value
0;1;124.729
1;2;0.597
1;3;26.862
1;4;280.322
1;5;81.144
6;2;35
7;4;35
8;9;11.606
10;9;63.965
9;4;75.571
11;12;10.639
11;13;22.505
11;14;46.184
15;16;104.453
15;14;113.726
15;17;27.14
15;12;342.165
15;18;37.797
15;19;4.412
15;13;40.858
15;3;56.691
15;20;7.863
15;21;90.008
15;22;93.494
23;24;40.719
25;24;82.233
5;13;0.129
5;3;1.401
5;26;151.891
5;19;2.096
5;12;48.58
27;15;7.013
17;28;20.897
17;3;6.242
28;18;20.897
29;15;6.995
2;12;121.066
2;30;128.69
2;18;135.835
2;31;14.458
2;32;206.267
2;19;3.64
2;33;33.218
2;20;4.413
34;1;4.375
24;5;122.952
35;26;839.978
36;37;504.287
38;37;107.703
37;2;611.99
39;4;56.587
39;1;77.81
40;14;193.026
40;13;70.672
41;15;59.901
42;14;19.263
43;42;19.263
43;41;59.901
4;19;0.882
4;26;400.12
4;12;46.477
26;15;525.531
26;3;787.129
26;11;79.329
44;15;9.452
45;1;182.01
46;15;19.013
47;15;289.366
myLabels.csv
name
Agricultural 'waste'
Bio-conversion
Liquid
Losses
Solid
Gas
Biofuel imports
Biomass imports
Coal imports
Coal
Coal reserves
District heating
Industry
Heating and cooling - commercial
Heating and cooling - homes
Electricity grid
Over generation / exports
H2 conversion
Road transport
Agriculture
Rail transport
Lighting & appliances - commercial
Lighting & appliances - homes
Gas imports
Ngas
Gas reserves
Thermal generation
Geothermal
H2
Hydro
International shipping
Domestic aviation
International aviation
National navigation
Marine algae
Nuclear
Oil imports
Oil
Oil reserves
Other waste
Pumped heat
Solar PV
Solar Thermal
Solar
Tidal
UK land based bioenergy
Wave
Wind