如何改变用于实现闪亮的不同类型分析的功能

时间:2016-07-09 11:08:47

标签: r shiny

我试图让我的用户选择在应用程序中改变分析类型,因此我必须改变正在使用的功能......这不像听起来那么直接...... igraph r-package中有不同的布局算法,每个算法都在命令中实现,例如:" layout.fruchterman.reingold"," layout.drl"," layout_with_kk"等

我想以某种方式让我的用户在以下代码中选择命令来实现他们喜欢的算法:

shinyUI(plotOutput("graph"))


serveractual <- function(input, output) {
output$graph<-renderPlot({
    adj_mat<-adjacency(adj)
    g <- simplify(graph.adjacency(adj_mat, mode='directed', weighted=TRUE,  add.colnames=NA))
    coords_fr = layout.fruchterman.reingold(g, weights=E(g)$weight)
    plot.igraph(g, layout=coords_fr, vertex.label=NA)
  })
}
shinyApp(uiactual, server actual)

起初我试过了:

selectInput(inputId = "adj", "choose graph type", choices=c("layout.fruchterman.reingold", "layout.drl", "layout_with_kk")),

coords_fr = input$adj(g, weights=E(g)$weight)

但当然参数变化并不是那样的。

那我该如何实现&#34; selectInput&#34;改变用于分析的功能?? 有什么提示吗?

PS。你可以定义&#34; adj&#34;在本地R环境中使用对象(邻接矩阵),以便使用以下代码运行闪亮的应用程序:

set.seed(1)

# generate a couple clusters
nodes_per_cluster <- 30
n <- 10

nvals <- nodes_per_cluster * n

# cluster 1 (increasing) 
cluster1 <- matrix(rep((1:n)/4, nodes_per_cluster) + 
                   rnorm(nvals, sd=1),
                   nrow=nodes_per_cluster, byrow=TRUE)

# cluster 2 (decreasing)
cluster2 <- matrix(rep((n:1)/4, nodes_per_cluster) + 
                   rnorm(nvals, sd=1),
                   nrow=nodes_per_cluster, byrow=TRUE)

# noise cluster
noise <- matrix(sample(1:2, nvals, replace=TRUE) +
                rnorm(nvals, sd=1.5),
                nrow=nodes_per_cluster, byrow=TRUE)

dat <- rbind(cluster1, cluster2, noise)
colnames(dat) <- paste0('n', 1:n)
rownames(dat) <- c(paste0('cluster1_', 1:nodes_per_cluster), 
                   paste0('cluster2_', 1:nodes_per_cluster),
                   paste0('noise_',    1:nodes_per_cluster))

adj<-dat

1 个答案:

答案 0 :(得分:0)

使用以下内容对其进行排序:

ui.R
radioButtons("type", "Distribution type:",
                 c("Normal" = "norm",
                   "Reingold" = "r",
                   "DRL" = "drl",
                   "Lay out with KK" = "layout"), selected = "Reingold")
server.R
data <- reactive({
    layout_style <- switch(input$type,
                   norm = layout.norm,
                   r = layout.fruchterman.reingold,
                   drl = layout.drl,
                   layout=lay_with_kk)
  })
coords_fr = data()(g, weights=E(g)$weight)