У меня есть набор данных, который я преобразовал в следующий формат и строю график на его основе.
structure(list(Date = structure(c(17833, 17830, 17829, 17828,
NA), class = "Date"), stocks = structure(c(1L, 1L, 1L, 1L, 1L
), .Label = c("DBS SP Equity", "OCBC SP Equity", "ST SP Equity"
), class = "factor"), cumulative = c(22.99, 23.1, 23.71, 24.1,
NA), Industry = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Banks",
"Telecommunications"), class = "factor")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
У меня есть 2 поля ввода: Industry и DateRange.
Мой ввод
selectInput(inputId = "industry2",
label = "Industry",
choices = input_selection[input_selection !='MarketIndex'],
selected = NULL,
multiple = TRUE),
dateRangeInput('dateRange',
label = 'Date range input: yyyy-mm-dd',
start = min(sharesdata_gather$Date), end = max(sharesdata_gather$Date))
Я могу построить график 2 - Отрасль против даты и Запас против даты для всех данных в моей исходной структуре данных.
Но не может построить график только для дат, указанных пользователем. Я попытался использовать функцию подмножества для фильтрации графика, но получил ошибку «Операция не разрешена без активного реактивного контекста. (Вы пытались сделать что-то, что может быть сделано только внутри реактивного выражения или наблюдателя.)»
Моя функция сервера:
#filtering the data for input start and end date
dailyprice_gather <- subset(dailyprice_gather, Date>=input$dateRange[1] )
dailyprice_gather <- subset(dailyprice_gather, Date<=input$dateRange[2] )
#grap for Date vs Cumulative for each industry
output$ind=renderPlot({
ggplot(data = dailyprice_gather[dailyprice_gather$Industry == input$industry2,]) +
geom_line(aes(x= Date , y= cumulative, color=Industry) , size=0.25) +
ggtitle(paste0("Simple Cumulative Return over Years - Industry Wise"))
})
#graph for Date vs Stock
output$stk =renderPlot({
ggplot(data = dailyprice_gather[dailyprice_gather$Industry == input$industry2 & dailyprice_gather$stocks == input$equities,])+
geom_line(aes(x= Date , y= cumulative, color=stocks) , size=0.25) +
ggtitle(paste0("Simple Cumulative Return over Years - Stock Wise"))
})