【问题标题】:I can't get ggvis scales to remain fixed with reactive input我无法让 ggvis 秤在反应性输入下保持固定
【发布时间】:2014-11-22 23:20:38
【问题描述】:

我正在尝试创建一个闪亮的应用程序,允许用户选择某些组以在 ggvis 图表上进行绘制。我遇到的问题是,如果我将反应数据映射到点的属性(如点填充、形状等),则每次用户更新组时比例都会重置。因此,组标识到填充颜色的映射不会保持不变。我试图通过硬编码组 ID 来在反应性元素中填充颜色来解决这个问题,但是当应用程序开始加载时,我开始难以解释错误。

这是我第一次尝试的代码:

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"))
  )
))

服务器.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")
})

当我运行它时,我得到了错误:

Error : x is not a numeric or integer vector

我还有一个 github 存储库,其中包含我在解决方案中的其他尝试之一,它得到一个不同的错误,并且代码有效,但存在重新映射问题:https://github.com/JoFrhwld/ggvis_scales

编辑:我应该说这是 ggvis v0.3、dplyr v0.3 和 shiny v0.10

【问题讨论】:

    标签: r shiny ggvis


    【解决方案1】:

    答案,感谢the ggvis google group

    • 硬编码刻度的范围和域,但不是被动编码。
    • group_by() 分类数据,以抑制无意义的动画。

    新的 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))%>%
            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")
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2016-04-25
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多