首页 > 解决方案 > 具有用户指定条件的均值和标准差

问题描述

我已转换为以下格式:

Date         price Industry  stock
29/10/2018   3      Airline   A
28/10/2018   4      Airline   A
27/10/2018   2      Airline   A
29/10/2018   5      Bank      B
29/10/2018   3      Food      C
28/10/2018   4      Bank      B
27/10/2018   2      Bank      B
27/10/2018   6      Food      C

我还输入了 Start Date 、 end date 、行业和库存。我使用以下代码根据用户输入创建了一个子集:

desc_filtered <- reactive({
c<-  dailyprice_gather %>%
  group_by(stocks) %>%
  mutate(
price_at_date = price[Date == selected_date2],
new_price = price - price_at_date)
c <- subset(c, Date>=input$dateRange[1] )
c <- subset(c, Date<=input$dateRange[2] )
c <- subset(c, Industry == input$industry2)
c <- subset(c, stocks == input$equities)
 })

我想显示用户在指定时间段内选择的行业和股票的平均值和标准差。行业和股票是多项选择下拉列表我可能需要使用 rowMeans 但不确定如何将 rowMean 用于反应功能。

标签: rshiny

解决方案


这是一个基于您在问题中描述的工作示例。我认为你的方向是正确的。关键是为数据框的子集创建一个反应对象。在我的示例中,它被称为sub_dat. 然后我们可以计算meanandsd基于sub_dat并用 打印它textOutput

由于您使用的是dplyr,我认为没有必要使用基本 R 子集函数。我们可以使用filter. 另一件事是我认为您不需要任何group_by操作。但如果你这样做,很容易修改我的示例以包含group_by操作。

# Load packages
library(tidyverse)
library(lubridate)
library(shiny)

# Create example data frame
dailyprice_gather <- tribble(
  ~Date,   ~price, ~Industry,  ~stock,
'29/10/2018',   3,      'Airline',   'A',
'28/10/2018',   4,      'Airline',   'A',
'27/10/2018',   2,      'Airline',   'A',
'29/10/2018',   5,      'Bank',      'B',
'29/10/2018',   3,      'Food',      'C',
'28/10/2018',   4,      'Bank',      'B',
'27/10/2018',   2,      'Bank',      'B',
'27/10/2018',   6,      'Food',      'C')

# Convert to date class
dailyprice_gather <- dailyprice_gather %>% mutate(Date = dmy(Date))

# A vector to show the choices for industry
ind_choices <- sort(unique(dailyprice_gather$Industry))

# A vector to show the choices for the stock
stock_choices <- sort(unique(dailyprice_gather$stock))

# Create the UI
ui <- fluidPage(
  # Select the date range
  dateRangeInput(inputId = "DateRange", label = "Select Date Range", 
                 start = min(dailyprice_gather$Date), 
                 end = max(dailyprice_gather$Date),
                 min = min(dailyprice_gather$Date),
                 max = max(dailyprice_gather$Date)),
  # Select the Industry
  selectInput(inputId = "Industry", label = "Select the Industry",
              choices = ind_choices, selected = ind_choices[1]),
  # Select the stock
  selectInput(inputId = "Stock", label = "Select the Stock",
              choices = stock_choices, selected = stock_choices[1]),
  # Show the mean
  h3("The Mean of Price"),
  textOutput(outputId = "MEAN"),
  # Show the standard deviation
  h3("The SD of Price"),
  textOutput(outputId = "SD")
)

# Create SERVER
server <- function(input, output) {
  # # Create a reactive object for subset data frame
  sub_dat <- reactive({
    dailyprice_gather %>%
      filter(Date >= input$DateRange[1], 
             Date <= input$DateRange[2],
             Industry %in% input$Industry,
             stock %in% input$Stock)
  })
  # Calculate the mean and sd based on sub_dat
  output$MEAN <- renderText({
    as.character(mean(sub_dat()$price))
  })
  output$SD <- renderText({
    as.character(sd(sub_dat()$price))
  })
}

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

推荐阅读