Shiny ggvis dynamic object

I am trying to add a dynamic ggvis graph to a Shiny app. First, the user selects a dimension, and then adds elements from this dimension.

For global .R and sample data, see 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")) ) ) ) )) 

This is normal

  • at the beginning when there are no elements and all data is displayed. This is a hack because the ggvis object throws an error if there is no data being served.
  • when all selected items are deleted (which matches 1.), and another dimension is selected

But when I try to switch to another dimension without first deleting the elements, I get the following:

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

I understand that ggvis is very new and constantly evolving, but I suspect there is something in the Shiny reactions that are not synchronized. If anyone can point out what I'm doing wrong, thank you very much!

+3
source share
1 answer

The error is caused by the fact that you have data.frame with zero rows and has a resulting 1:0 . You can change your selected function to:

  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 }) 
+4
source

Source: https://habr.com/ru/post/975722/


All Articles