【问题标题】:Using Shiny to Create a Scatterplot with dplyr使用 Shiny 使用 dplyr 创建散点图
【发布时间】:2015-02-12 21:36:53
【问题描述】:

我正在使用 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())
})

使用镶嵌选项时,会弹出错误消息,显示“'closure' 类型的'envir' 参数无效”。 使用 ggvis 选项,绘图显示在 RStudio 的绘图窗口中,但在应用程序中,我收到错误消息: 错误:参数长度为零

澄清一下,我并不是要同时创建两个情节,它们只是我试图制作情节的不同方式。我在运行应用程序时注释掉一个。

【问题讨论】:

  • output$plot &lt;- renderPlot(xyplot(y~x, group=Swing, pch='.', data=df, auto.key=TRUE)),你的意思是写data=plotData()而不是data=df
  • 也许你在ggvis版本中需要filter_(~count==input$PlateCount)
  • 第一条评论修复了xyplot命令,但是ggvis版本没有显示在应用程序中,它只显示在RStudio的绘图面板中。
  • @Megan,您能否提供一个可重现的数据集或指出您在哪里找到它(如果是公开的)?

标签: r shiny dplyr


【解决方案1】:

如果没有可重复的数据集,很难确定(如果提供,我会相应地更新),但您需要为ggvis 地块更新您的 ui.R 和 server.R。首先,您想将bind_shiny 用于ggvis 对象。其次,你的 ui.R 你应该用ggvisOutput 替换plotOutput。以下假设您在Pitchers 中有变量xy

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

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多