I can't get ggvis scales to remain fixed with reactive input

会有一股神秘感。 提交于 2019-12-10 09:53:41

问题


I'm trying to create a shiny app that allows the user to select certain groups to plot on a ggvis graph. The problem I'm having is that if I map reactive data to properties of the points (like the point fill, shape, etc.), the scale resets every time the user updates the groups. So the mapping of group identity to fill color does not remain constant. I tried to fix this by hard coding group id to fill color in a reactive element, but then I start getting difficult to interpret errors when the app starts to load.

Here's the code of my first attempt:

ui.R

#ui.R
library(shiny)
library(ggvis)

shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("gear", label = "Gears", choices = c("3","4","5")) 
    ),

    # Show a plot of the generated distribution
    mainPanel(
      uiOutput("ggvis_ui"),
      ggvisOutput("ggvis"),
      textOutput("jawn"))
  )
))

server.R

#server.R
library(shiny)
library(ggvis)
library(dplyr)

shinyServer(function(input, output) {

    selected <- reactive(input$gear)

    selectedData <- reactive({
      mtcars %>%
        filter(gear %in% selected())%>%
        mutate(gear = as.character(gear))
    })

    colorRange <- reactive({
      c(`3` = "red", `4` = "blue", `5` = "green")[sort(selected())]
    })

    output$jawn <- renderText(colorRange())


    mtcars%>%
      ggvis(~wt, ~mpg)%>%
      layer_points()%>%
      layer_points(data = selectedData, fill = ~gear)%>%
      scale_ordinal("fill", range = colorRange) %>% 
      bind_shiny("ggvis", "ggvis_ui")
})

When I run this I get the error:

Error : x is not a numeric or integer vector

I've also got a github repository with one of my other attempts at a solution, which gets a different error, and the code that works, but has the remapping problem: https://github.com/JoFrhwld/ggvis_scales

Edit: I should say this is with ggvis v0.3, dplyr v0.3, and shiny v0.10


回答1:


The answer, thanks to the ggvis google group

  • hardcode the range and domain of the scale, but not reactively.
  • group_by() the categorizing data, to inhibit meaningless animations.

The new server.R code is thus

# server.R
library(shiny)
library(ggvis)
library(dplyr)

shinyServer(function(input, output) {

    selected <- reactive(input$gear)

    selectedData <- reactive({
      mtcars %>%
        filter(gear %in% selected())%>%
        mutate(gear = as.character(gear))%>%
        group_by(gear)
    })

    fill_domain = c("3","4","5")
    fill_range = c("red","blue","green")

    mtcars%>%
      ggvis(~wt, ~mpg)%>%
      layer_points()%>%
      layer_points(data = selectedData, fill = ~gear)%>%
      scale_ordinal("fill", range = fill_range, domain = fill_domain)%>%
      bind_shiny("ggvis", "ggvis_ui")
})


来源:https://stackoverflow.com/questions/26085104/i-cant-get-ggvis-scales-to-remain-fixed-with-reactive-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!