[英]Reactive input not working with ggvis and Shiny
Simple example of a Shiny app using ggvis. 使用ggvis的Shiny应用程序的简单示例。 Trying to use a pulldown to filter a variable.
尝试使用下拉来过滤变量。 So here I'm trying to filter by mtcars' gear (either 3, 4, or 5), then plotting x and y of mpg and hp for each of the unique values of gear.
所以在这里我试图通过mtcars的齿轮(3,4或5)进行过滤,然后为每个齿轮的唯一值绘制mp和hp的x和y。
I get the initial plot drawn with a default of '3' selected, but if I change the value via the pulldown nothing happens. 我得到的初始绘图选择了默认的'3',但如果我通过下拉更改值,则没有任何反应。 I think I know where things are going wrong (commented in the code), but I've tried just about everything I can think of and have no idea what the actual mistake I'm making is.
我想我知道哪里出了问题(在代码中评论),但我已经尝试了我能想到的一切,并且不知道我正在犯的实际错误是什么。
Thanks 谢谢
ui.R ui.R
# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Car Thing"),
sidebarLayout(
sidebarPanel(
uiOutput("choose_gear")
),
mainPanel(
ggvisOutput("ggvis")
)
)
))
server.R server.R
#server.R
library(shiny)
library(ggvis)
library(dplyr)
gear_nos <- sort(unique(mtcars$gear))
shinyServer(function(input, output, session) {
output$choose_gear <- renderUI({
selectInput("gears", "Choose Gear", gear_nos, selected="3")
})
# I'm pretty sure this is where I'm messing something up
pickedGear <- reactive({
mtcars %>% filter(gear == input$gears)
})
if(is.null(dim(pickedGear))){
pickedGear <- mtcars[mtcars$gear == 3,]
}
pickedGear %>% ggvis(~mpg, ~hp) %>% layer_points(fill := "green") %>% bind_shiny("ggvis")
})
I think this might be what you want. 我想这可能是你想要的。
Note that it took me quite awhile to figure out the validate
piece that eliminates an extraneous error message ( incorrect string: length(0) 32 expected
) on startup initialization of the shinyServer
code, but I will remember it for the future now I guess. 请注意,我花了很
shinyServer
在启动初始化shinyServer
代码时消除了一个无关的错误消息( incorrect string: length(0) 32 expected
)的validate
部分,但我现在将会记住它。
library(shiny)
library(ggvis)
library(dplyr)
# library(googleVis) # used observe instead now
u <- shinyUI(fluidPage(
titlePanel("Car Thing"),
sidebarLayout(
sidebarPanel(
uiOutput("choose_gear")
),
mainPanel(
ggvisOutput("ggvis")
)
)
))
gear_nos <- sort(unique(mtcars$gear))
s <- shinyServer(function(input, output, session) {
output$choose_gear <- renderUI({
selectInput("gears", "Choose Gear", gear_nos, selected="3")
})
pickedGear <- reactive({
shiny::validate(need(input$gears, message=FALSE))
mtcars %>% filter(gear == input$gears)
})
# could also replace "observe" with this from googlevis : "output$ggvis <- renderGvis({"
observe({
pickedGear() %>% ggvis(~mpg,~hp) %>% layer_points(fill:="green") %>% bind_shiny("ggvis")
})
})
shinyApp(u,s)
Yielding: 产量:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.