Objeto ggvis dinâmico no Shiny
Estou tentando adicionar um gráfico ggvis dinâmico a um aplicativo brilhante. Primeiro, o usuário escolhe uma dimensão e, em seguida, adiciona itens dessa dimensão.
Para global.R e dados de amostra, consultehttps://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"))
)
)
)
))
Isso é bom
no início, quando não há itens selecionados e todos os dados são plotados. Isso é um hack, porque o objeto ggvis gera um erro se não houver dados fornecidos.quando todos os itens selecionados são excluídos (igual a 1.) e outra dimensão é escolhidaMas quando tento mudar para outra dimensão sem excluir os itens primeiro, recebo o seguinte:
Error in `Error in `$<-.data.frame`(`*tmp*`, "keys", value = c(1L, 0L)) :
replacement has 2 rows, data has 0
lt;-.data.frame`(`*tmp*`, "keys", value = c(1L, 0L)) :
replacement has 2 rows, data has 0
Entendo que o ggvis é muito novo e está em constante desenvolvimento, mas suspeito que há apenas algo nos valores reativos do Shiny que está fora de sincronia. Se alguém puder apontar o que estou fazendo de errado, muito obrigado!