使用Shiny创建带有dplyr的散点图

时间:2015-02-12 21:36:53

标签: r shiny dplyr

我正在使用Shiny创建一个交互式应用程序。我想使用ui.R中的输入来过滤数据集,以便它将更改过滤数据集的输入的xyplot。这是我的代码。

ui.R是:

library(shiny) # load the shiny package
shinyUI(fluidPage(
titlePanel(h4('What makes a batter swing?', align = "center")),
sidebarPanel(
selectInput("PlateCount", label = "What is the count?",
            choices = c("0-0" = '0-0', "0-1" = '0-1', "0-2" = '0-2', "0-3"='0-3'),
            selected = '0-0'),
sliderInput("endspeed", "What is the Maximum end speed of the pitch?", min=38, max=95, value=50),
radioButtons("relief", label = "Relief Pitchers?",
             choices = c("Relief", "Non-Relief",
                         "All Pitchers"), selected = "All Pitchers")
),
mainPanel(
plotOutput("plot")
)
)
)

我正在尝试使用马赛克中的ggvis或xyplot显示情节,但我得到两者的错误消息。

library(shiny); library(dplyr); library(ggvis) # Load shiny package
Pitchers$count= as.character(Pitchers$count)
Pitchers$Swing <- as.character(Pitchers$Swing)
#here is trying mosaic
shinyServer(function(input, output){
plotData <- reactive({ 
df <- Pitchers %>%
  filter(count==input$PlateCount)
})
output$plot <- renderPlot(xyplot(y~x, group=Swing, pch='.', data=df, auto.key=TRUE))
})

#here is trying ggvis
shinyServer(function(input, output){
plotData <- reactive({ 
df <- Pitchers %>%
  filter(count==input$PlateCount)
  })
 output$plot <- renderUI(Pitchers %>%
                          ggvis(x = ~x, y = ~y) %>%
                          layer_points())
})

使用马赛克选项,会弹出错误消息,读取“封闭”类型的“无效'envir'参数”。 使用ggvis选项,该图显示在RStudio的绘图窗口中,但在应用程序中,我收到错误消息: 错误:参数长度为零

为了澄清我并不是想同时创建这两个情节,它们只是我试图制作情节的不同方式。我在运行应用程序时对其进行了评论。

1 个答案:

答案 0 :(得分:0)

如果没有可重现的数据集,我很难确定(如果提供的话,我会相应更新),但您需要更新您的ui.R和server.R以获取ggvis图。首先,您要对bind_shiny对象使用ggvis。其次,你的ui.R你应该用plotOutput替换ggvisOutput。以下假设您在x中有变量yPitchers

server.R

shinyServer(function(input, output){
  plotData <- reactive({ 
    df <- Pitchers %>%
      filter_(count==input$PlateCount)
  })

  plotData %>%
    ggvis(x = ~x, y = ~y) %>%
    layer_points() %>%
    bind_shiny("plot")
})

ui.R

shinyUI(fluidPage(
  titlePanel(h4('What makes a batter swing?', align = "center")),
  sidebarPanel(
    selectInput("PlateCount", label = "What is the count?",
                choices = c("0-0" = '0-0', "0-1" = '0-1', "0-2" = '0-2', "0-3"='0-3'),
                selected = '0-0'),
    sliderInput("endspeed", "What is the Maximum end speed of the pitch?", min=38, max=95, value=50),
    radioButtons("relief", label = "Relief Pitchers?",
                 choices = c("Relief", "Non-Relief",
                             "All Pitchers"), selected = "All Pitchers")
  ),
  mainPanel(
    ggvisOutput("plot")
  )
)
)