确定布局的 ShinyR 输入

ShinyR Input to determine layout

希望制作一个 shinyR 应用程序,用户可以在其中 select 从下拉列表中选择网络图的不同可能布局(使用 igraph)。但是,我找不到将文本输入转换为可转换为图形布局的内容的方法。

例如,从下拉 SelectPicker 中,如果用户 selects“layout_with_fr”,理想情况下我会绘制一个具有所需布局的网络。但是,我知道设置布局 plot(g, layout = layout.with.fr) 或 layout_with_fr(g) 的唯一方法将无法接受此文本输入。如何将此输入转换为可用于影响布局的内容?

谢谢!

编辑:下面 Ash 的回答很棒,谢谢。

当然,我们只需要一步将下拉列表中选择的内容转换为图表可以使用的内容,例如

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)