【问题标题】:Dynamic ggvis object in ShinyShiny中的动态ggvis对象
【发布时间】:2014-08-24 15:19:11
【问题描述】:

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

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

服务器.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 反应式值中只有一些不同步的东西。如果有人能指出我做错了什么,非常感谢!

【问题讨论】:

    标签: r shiny ggvis


    【解决方案1】:

    这个错误是因为你有一个零行的 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
      })
    

    【讨论】:

    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2014-04-14
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多