我正在尝试创建一个闪亮的应用程序,允许用户选择某些组来绘制ggvis图。我遇到的问题是,如果我将反应数据映射到点的属性(如点填充,形状等),则每次用户更新组时,比例都会重置。因此,组标识到填充颜色的映射不会保持不变。我尝试通过硬编码组ID来修复此问题以填充反应元素中的颜色,但是当应用程序开始加载时,我开始难以解释错误。
这是我第一次尝试的代码:
#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
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和闪亮的v0.10
答案 0 :(得分:5)
因此新的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")
})