当然,我们只需要将下拉列表中选择的内容转换为图表可以使用的内容,例如
if(input$myselectinput == 'Layout MDS') {layoutselected <- layout.mds}
if(input$myselectinput == 'Layout with FR') {layoutselected <- layout_with_fr}
这是一个显示它的最小工作应用程序:
library(shiny)
library(igraph)
#Example data for graph
nodes=cbind('id'=c('Fermenters','Methanogens','carbs','CO2','H2','other','CH4','H2O'),
'type'=c(rep('Microbe',2),rep('nonBio',6)))
links=cbind('from'=c('carbs',rep('Fermenters',3),rep('Methanogens',2),'CO2','H2'),
'to'=c('Fermenters','other','CO2','H2','CH4','H2O',rep('Methanogens',2)),
'type'=c('uptake',rep('output',5),rep('uptake',2)),
'weight'=rep(1,8))
#UI
ui <- fluidPage(
#Select input / dropdown box
selectInput('myselectinput', 'Select Layout', choices = c('Layout MDS', 'Layout with FR')),
#Graph
plotOutput('myplot')
)
#Server
server <- function(input, output, session) {
output$myplot = renderPlot({
#Prepare net
net = graph_from_data_frame(links,vertices = nodes, directed = T)
#Turn what was seleced in our dropdown into something that our graph can use
if(input$myselectinput == 'Layout MDS') {layoutselected <- layout.mds}
if(input$myselectinput == 'Layout with FR') {layoutselected <- layout_with_fr}
#Plot our graph with the selected layout
plot.igraph(net, layout = layoutselected)
})
}
shinyApp(ui, server)