【问题标题】:ShinyR Input to determine layoutShinyR 输入确定布局
【发布时间】:2021-08-03 04:34:57
【问题描述】:

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

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

谢谢!

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

【问题讨论】:

  • 请提供一个可重现的最小示例。

标签: r layout shiny igraph shiny-reactivity


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2020-10-09
    • 1970-01-01
    • 2021-04-22
    • 2015-09-21
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多