简体   繁体   English

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

[英]Data Points Not Showing Up in R Shiny Scatterplot

I am working on building a Shiny app for a college baseball team, however, I am having trouble getting the data points to show up on a scatterplot after adding a date input into the filtering.我正在为大学棒球队构建一个 Shiny 应用程序,但是,在将日期输入添加到过滤后,我无法让数据点显示在散点图上。 I have changed the date column to a date datatype and added it to the reactive function but nothing is still showing up.我已将日期列更改为日期数据类型并将其添加到反应函数中,但仍然没有显示任何内容。

Here is the code I have written so far:这是我到目前为止编写的代码:

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)

I am just using some MLB data to build this, so here is the data that I am using, this is just one pitcher.我只是使用一些 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")

In the server the inputId is DateRangeInputserver中 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)
  })
}

based on the ui基于用户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")
    )
  )
)

-testing -测试

shinyApp(ui = ui, server = server)

output输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM