我已转换为以下格式:
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
我还输入了开始日期,结束日期,行业和库存。我已经根据用户输入使用以下代码创建了一个子集:
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用于响应函数。
答案 0 :(得分:1)
从软件包 dplyr 中,您可以使用函数filter()
和group_by()
来按股票计算价格均值(和sd)。
假设您的数据集为df
:
df %>%
filter(Date >= input$dateRange[1] & Date <= input$dateRange[2]) %>%
filter(Industry %in% input$industry_choices) %>%
group_by(Industry, stock) %>%
summarise(
price_mean = mean(price),
price_sd = mean(sd)
)
在Shiny中,输入被视为字符串,有时您需要使用group_by_()
。
例如,如果用户可以选择变量分组依据:group_by_(input$grouping_choice)
答案 1 :(得分:1)
这是一个根据您在问题中所描述内容的有效示例。我认为您的方向正确。关键是为数据帧的子集创建反应性对象。在我的示例中,它称为sub_dat
。然后,我们可以基于mean
计算sd
和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)