我正在尝试创建一个 Shiny 的应用程序,允许用户选择某些组以在 ggvis 图形上绘制。我遇到的问题是,如果我将 react 数据映射到点的属性(如点填充、形状等),则每次用户更新组时都会重置比例。因此,组标识到填充颜色的映射不会保持不变。我试图通过硬编码组 ID 来解决这个问题,以在 react 元素中填充颜色,但是当应用程序开始加载时,我开始难以解释错误。
这是我第一次尝试的代码:
用户界面
#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 和 Shiny v0.10
最佳答案
答案,感谢 the ggvis google group
新的 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")
})
关于r - 我无法通过无功输入使 ggvis 比例保持固定,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26085104/