Solution for ShinyR Input to determine layout
is Given Below:
Looking to make a shinyR app where the user can select from a dropdown the different possible layouts of a network graph (using igraph). However, I cannot find a way to convert what is a text input into something that translates into the layout of the graph.
For example, from the dropdown SelectPicker, if the user selects “layout_with_fr”, ideally I would then plot a network with the desired layout. However the only ways I know to set the layout plot(g, layout = layout.with.fr) or layout_with_fr(g) wouldn’t be able to accept this text input. How do I convert this input into something useable to affect the layout?
Thanks!
Edit: Great answer by Ash below, thanks.
Sure, we just need a step to turn what was selected in our dropdown to something that the graph can use, e.g.
if(input$myselectinput == 'Layout MDS') {layoutselected <- layout.mds}
if(input$myselectinput == 'Layout with FR') {layoutselected <- layout_with_fr}
Here is a minimal working app showing it in action:
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)