繁体   English   中英

Shiny中的动态ggvis对象

[英]Dynamic ggvis object in Shiny

我正在尝试将动态ggvis图添加到Shiny应用程序中。 首先,用户选择一个维度,然后添加该维度中的项目。

有关global.R和示例数据,请参阅https://gist.github.com/tts/a41c8581b9d77f131b31

server.R:

shinyServer(function(input, output, session) {


  # Render a selectize drop-down selection box 
  output$items <- renderUI({

    selectizeInput(
      inputId = 'items', 
      label = 'Select max 4. Click to delete',
      multiple = TRUE,
      choices = aalto_all[ ,names(aalto_all) %in% input$dim],
      options = list(maxItems = 4, placeholder = 'Start typing')
    )

  })


  selected <- reactive({

    if (is.null(input$items)) {
      return(aalto_all)
    }
    df <- aalto_all[aalto_all[[input$dim]] %in% input$items, ]
    df$keys <-seq(1, nrow(df))
    df

  })


  selected %>% 
    ggvis(~WoS, ~NrOfAuthors, fill = ~School, key := ~keys) %>%
    layer_points() %>%
    add_tooltip(show_title) %>%
    bind_shiny("gv")


  show_title <- function(x=NULL) {
    if(is.null(x)) return(NULL)
    key <- x["keys"][[1]]
    selected()$Title20[key]
  }  


})

ui.R:

shinyUI(fluidPage(

  titlePanel('Some (alt)metric data for articles published since 2010'),

  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "dim", 
        label = "Dimension", 
        choices = dimensions,
        selected = c("Title")),
      uiOutput("items")
      ),


    mainPanel(

      tabsetPanel(
        # I'll add more tabs
        tabPanel("Plot with ggvis", ggvisOutput("gv"))
      )
    )
  )
))

还行吧

  1. 在开始时,没有选择任何项目,并绘制所有数据。 这是一个hack,因为如果没有提供数据,ggvis对象会抛出错误。
  2. 当删除所有选定的项目(与1.相同)并选择另一个维度时

但是当我尝试切换到另一个维度而不先删除项目时,我得到了这个:

Error in `$<-.data.frame`(`*tmp*`, "keys", value = c(1L, 0L)) : 
replacement has 2 rows, data has 0

我知道ggvis是非常新的并且在不断发展,但我怀疑Shiny反应值中只有一些东西不同步。 如果有人能指出我做错了什么,非常感谢!

导致该错误是因为您有一个零行的data.frame并且得到1:0 您可以将selected功能更改为:

 selected <- reactive({
    if (is.null(input$items)) {
      return(aalto_all)
    }
    df <- aalto_all[aalto_all[[input$dim]] %in% input$items, ]
    df$keys <-seq_along(df[,1])
    if(nrow(df) == 0){
      return(aalto_all)
    }
    df
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM