【问题标题】:Data Points Not Showing Up in R Shiny Scatterplot数据点未显示在 R Shiny 散点图中
【发布时间】:2021-09-02 18:31:18
【问题描述】:

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

这是我目前写的代码:

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")

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    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)
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多