数据点未出现在 R 闪亮散点图中

时间:2021-06-18 00:53:57

标签: r shiny

我正在为大学棒球队构建 Shiny 应用程序,但是,在将日期输入添加到过滤后,我无法让数据点显示在散点图上。我已将日期列更改为日期数据类型并将其添加到反应函数中,但仍然没有显示任何内容。

这是我目前编写的代码:

library(shiny)
library(dplyr)
library(ggplot2)
library(ggalt)

s2020 = read.csv("C:/Users/kaifr/Downloads/Baseball Research/20 Data.csv")
s2020$game_date.x = as.Date(s2020$game_date.x, format = "%Y-%m-%d")    

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Baseball Dashboard"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput("name","Select Pitcher", choices = unique(s2020$player_name)),
            dateRangeInput(inputId = "DateRangeInput", label = "Select Date Range", start = min(s2020$game_date.x), end = max(s2020$game_date.x))
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("table")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    sc_reactive = reactive({
        s2020 %>% dplyr::filter(player_name == input$name,
                                between(game_date.x, input$game_date.x[1], input$game_date.x[2]))
    })

    output$table <- renderPlot({
        ggplot(sc_reactive(), aes(pfx_x, pfx_z, color = pitch_type)) +
            geom_point() +
            coord_fixed() +
            geom_encircle() +
            theme_bw() +
            geom_hline(yintercept = 0) +
            geom_vline(xintercept = 0) +
            ylim(-2,2) + xlim(-2,2)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

我只是使用一些 MLB 数据来构建它,所以这是我使用的数据,这只是一个投手。

dput(s2020)

structure(list(player_name = c("deGrom, Jacob", "deGrom, Jacob", 
"deGrom, Jacob", "deGrom, Jacob", "deGrom, Jacob"), pfx_x = c(0.38, 
0.38, -0.6, -0.35, -0.72), pfx_z = c(0.3, 0.39, 1.38, 1.32, 1.58
), game_date.x = structure(c(18472, 18516, 18493, 18472, 18526
), class = "Date"), pitch_type = c("SL", "SL", "FF", "FF", "FF"
)), row.names = c(NA, -5L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

server 中,inputId 是 DateRangeInput

server <- function(input, output) {
  
  sc_reactive = reactive({
    s2020 %>% 
       dplyr::filter(player_name == input$name,
              between(game_date.x, as.Date(input$DateRangeInput[1]), 
                    as.Date(input$DateRangeInput[2])))
  })
  
  output$table <- renderPlot({
    ggplot(sc_reactive(), aes(pfx_x, pfx_z, color = pitch_type)) +
      geom_point() +
      coord_fixed() +
      geom_encircle() +
      theme_bw() +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 0) +
      ylim(-2,2) + xlim(-2,2)
  })
}

基于ui

ui <- fluidPage(
  
  # Application title
  titlePanel("Baseball Dashboard"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("name","Select Pitcher", choices = unique(s2020$player_name)),
      dateRangeInput(inputId = "DateRangeInput",
        label = "Select Date Range", start =min(s2020$game_date.x), 
              end = max(s2020$game_date.x))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("table")
    )
  )
)

-测试

shinyApp(ui = ui, server = server)

输出

enter image description here

相关问题