使用对 selectinput 做出反应的 sankey 图创建闪亮的应用 [英] Create shiny app with sankey diagram that reacts to selectinput

查看:45
本文介绍了使用对 selectinput 做出反应的 sankey 图创建闪亮的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有 Sankey 图和 selectInput 的仪表板,让最终用户选择过滤源列.我在试图弄清楚如何使用反应式表达式来过滤数据时遇到了麻烦.这有点复杂,因为它不仅仅是读取数据的一个步骤,因为它必须经过预处理.我试过将反应过滤器放在最后,但它不起作用,如下所示.我也尝试让每一步都具有反应性,但那是一团糟,当然行不通.

I'm trying to create a dashboard with a Sankey diagram and a selectInput that lets the end user choose filter the source column. I'm having trouble trying to figure out how to use the reactive expressions to filter the data. It's sort of complex because it's not just one step of reading the data in as it has to be preprocessed. I've tried putting the reactive filter at the end but it isn't working, as you'll see below. I tried also making each step reactive but that was a mess that certainly didn't work.

它目前不起作用,因为 1) 仪表板加载但没有图表(应该是 schname 的默认值/第一个值)和 2)当我选择另一个 schname 时,它​​给出了一个闭包类型的对象"不可子集"错误.我认为这意味着我在处理反应式表达式的方式上做错了,但我还没有从我所有的搜索中弄清楚.

It's not working at the moment in the sense that 1) the dashboard loads but there's no diagram (should be the default/first value of schname) and 2) when I select the other schname it gives an "object of type closure is not subsettable" error. I think this means that I'm doing something wrong with the how I'm treating the reactive expression but I have yet to figure it out from all my searches.

正则表达式:

library(shiny)
ui <- fluidPage(
  selectInput(inputId = "school",
              label   = "School",
              choices =  c("alpha", "echo")),

  sankeyNetworkOutput("diagram")
)

server <- function(input, output) {

  dat <- data.frame(schname = c("alpha", "alpha", "alpha", "echo"),
                    next_schname = c("bravo", "charlie", "delta", "foxtrot"),
                    count = c(1, 5, 3, 4))

  links <- data.frame(source = dat$schname,
                      target = dat$next_schname,
                      value  = dat$count)
  nodes <- data.frame(name = c(as.character(links$source),
                               as.character(links$target)) %>%
                        unique)

  links$IDsource <- match(links$source, nodes$name) - 1
  links$IDtarget <- match(links$target, nodes$name) - 1

  links <-reactive({
    links %>%
      filter(source == input$school)
  })


  output$diagram <- renderSankeyNetwork({
    sankeyNetwork(
      Links = links,
      Nodes = nodes,
      Source = "IDsource",
      Target = "IDtarget",
      Value = "value",
      NodeID = "name",
      sinksRight = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

推荐答案

我认为在反应式和非反应式数据框之间为 links 分离对象名称很重要.其次,对于渲染函数,您希望像函数一样调用响应式对象:links().第三,确保为应用程序加载了所有依赖项.

I think separating object names for links between the reactive and not reactive data frame is important. Second, for the render function, you want to call reactive objects like a function: links(). Third, make sure all dependencies are loaded for the app.

例如:

library(shiny)
library(networkD3)
library(dplyr)
ui <- fluidPage(
  selectInput(inputId = "school",
              label   = "School",
              choices =  c("alpha", "echo")),

  sankeyNetworkOutput("diagram")
)

server <- function(input, output) {

  dat <- data.frame(schname = c("alpha", "alpha", "alpha", "echo"),
                    next_schname = c("bravo", "charlie", "delta", "foxtrot"),
                    count = c(1, 5, 3, 4))

  links <- data.frame(source = dat$schname,
                      target = dat$next_schname,
                      value  = dat$count)
  nodes <- data.frame(name = c(as.character(links$source),
                               as.character(links$target)) %>%
                        unique)

  links$IDsource <- match(links$source, nodes$name) - 1
  links$IDtarget <- match(links$target, nodes$name) - 1

  links2 <-reactive({
    links %>%
      filter(source == input$school)
  })


  output$diagram <- renderSankeyNetwork({
    sankeyNetwork(
      Links = links2(),
      Nodes = nodes,
      Source = "IDsource",
      Target = "IDtarget",
      Value = "value",
      NodeID = "name",
      sinksRight = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

这篇关于使用对 selectinput 做出反应的 sankey 图创建闪亮的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆