我正在制作一個閃亮的應用程式來生成股價圖。我想在給定日期范圍的情況下生成繪圖,但每當我嘗試調整日期時,都會出現錯誤“引數 1 不是向量”。如果我從我的應用程式中洗掉“daterangeinput”部分,一切正常。我附上了我的代碼 資料。
運行以下部分以獲取資料。
library(tidyquant)
library(tidyverse)
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(plotly)
sp500_names <- tq_index("SP500") %>%
slice_head(n = 10) %>%
select(symbol, company)
tickers <- sp500_names[,1]
prices <- tq_get(tickers,
get = "stock.prices",
from = today() - months(24),
to = today(),
complete_cases = F) %>%
select(symbol, date, close)
閃亮的應用程式代碼:
ui <- fluidPage(
# Title
titlePanel("Stock Price Visualization and Forecasting"),
# Sidebar
sidebarLayout(
sidebarPanel(width = 3,
pickerInput(
inputId = "stocks",
label = h4("Pick a stock"),
choices = tickers$symbol,
selected = tickers,
options = list(`actions-box` = TRUE),
multiple = T),
# Date input
dateRangeInput("daterange", "Pick a Time Period",
# value = today(),
min = today() - months(23),
max = today())),
# Plot results
mainPanel(
plotlyOutput("plot",height=600)
)
)
)
server <- function(input, output, session) {
# Server logic based on user inputs
observeEvent(input$stocks,{
prices <- prices %>%
dplyr::filter(symbol %in% input$stocks) %>%
filter(date > input$daterange[1] & date <= input$daterange[2])
# Create plot
output$plot <- renderPlotly({
print(
ggplotly(prices %>%
ggplot(aes(date, close, color = symbol))
geom_line(size = 1, alpha = 0.9)
theme_minimal(base_size=16)
theme(axis.title=element_blank(),
plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill="grey"),
panel.grid = element_blank(),
legend.text = element_text(colour="black"))
)
)
})
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
問題是您沒有為日期范圍設定任何開始和結束日期,并且您的應用程式不會處理這些。因此,一個簡單的解決方法是簡單地設定一個默認的開始和結束日期。另外恕我直言,我看不出有任何理由observeEvent。相反,我建議使用 areactive根據用戶輸入過濾您的資料,然后可以將其用于繪圖:
library(tidyquant)
library(tidyverse)
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(plotly)
ui <- fluidPage(
# Title
titlePanel("Stock Price Visualization and Forecasting"),
# Sidebar
sidebarLayout(
sidebarPanel(
width = 3,
pickerInput(
inputId = "stocks",
label = h4("Pick a stock"),
choices = tickers$symbol,
selected = tickers,
options = list(`actions-box` = TRUE),
multiple = T
),
# Date input
dateRangeInput("daterange", "Pick a Time Period",
# value = today(),
start = min(prices$date),
end = today(),
min = min(prices$date),
max = today()
)
),
# Plot results
mainPanel(
plotlyOutput("plot", height = 600)
)
)
)
server <- function(input, output, session) {
prices_filtered <- reactive({
req(input$stocks)
prices %>%
dplyr::filter(symbol %in% input$stocks) %>%
filter(date > input$daterange[1] & date <= input$daterange[2])
})
output$plot <- renderPlotly({
req(input$stocks)
g <- ggplot(prices_filtered(), aes(date, close, color = symbol))
geom_line(size = 1, alpha = 0.9)
theme_minimal(base_size = 16)
theme(
axis.title = element_blank(),
plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "grey"),
panel.grid = element_blank(),
legend.text = element_text(colour = "black")
)
ggplotly(g)
})
}
shinyApp(ui, server)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462761.html
