r-制作一个显示不同股票价值的应用程序|初学者



所以我在R和编程方面很新。

我必须制作一个应用程序,向我显示我在输入中选择的动作的图形和数据表,以及我可以在图形中看到的变量之间进行选择。我有点迷路了,代码不再适用于我。

这是代码:

#Importamos librerías:
library(shiny)
library(tidyverse)
library(tidyquant)
library(plotly)
#Cargamos los datos de las acciones de Amazon:
datos_yahoo_AMZN <- tq_get(
x = "AMZN",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
datos_yahoo_AAPL <- tq_get(
x = "AAPL",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
datos_yahoo_TSLA <- tq_get(
x = "TSLA",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
library(shiny)
ui <- fluidPage(
titlePanel("Datos Financieros"),
verticalLayout(
radioButtons(
inputId = "accion",
label = "Elige la acción que quieres ver",
choices = c(
"Amazon" = "datos_yahoo_AMZN",
"Apple" = "datos_yahoo_AAPL",
"Tesla" = "datos_yahoo_TSLA"
),
selected = "Amazon",
inline = TRUE
),
radioButtons(
inputId = "var_y",
label = "Elige el dato que quieras conocer",
choices = c(
"Close" = "close",
"Open" = "open",
"Volume" = "volume"
),
selected = "close",
inline = TRUE
),
colourInput(
inputId = "col",
label = "Elige el color",
value = "black"

),
plotlyOutput(outputId = "grafica")
)
)
server <- function(input, output, session) {
if(input$accion == "Amazon") {
output$grafica <- renderPlotly(
datos_yahoo_AMZN |> 
ggplot(aes(x = date)) +
geom_line(aes_string(y = input$var_y),
color = input$col)
)
}
}
shinyApp(ui, server)

希望你能帮我做什么。谢谢

我们可以使用get函数并避免if语句。另一种方法可以是根据用户的请求下载股票价格。

# Importamos librerías:
library(shiny)
library(dplyr)
library(tidyquant)
library(plotly)
library(colourpicker)
library(ggplot2)
# Cargamos los datos de las acciones de Amazon:
datos_yahoo_AMZN <- tq_get(
x = "AMZN",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
datos_yahoo_AAPL <- tq_get(
x = "AAPL",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
datos_yahoo_TSLA <- tq_get(
x = "TSLA",
get = "stock.prices",
from = "2022-01-01",
to = "2022-08-30"
)
library(shiny)
ui <- fluidPage(
titlePanel("Datos Financieros"),
verticalLayout(
radioButtons(
inputId = "accion",
label = "Elige la acción que quieres ver",
choices = c(
"Amazon" = "datos_yahoo_AMZN",
"Apple" = "datos_yahoo_AAPL",
"Tesla" = "datos_yahoo_TSLA"
),
selected = "Amazon",
inline = TRUE
),
radioButtons(
inputId = "var_y",
label = "Elige el dato que quieras conocer",
choices = c(
"Close" = "close",
"Open" = "open",
"Volume" = "volume"
),
selected = "close",
inline = TRUE
),
colourInput(
inputId = "col",
label = "Elige el color",
value = "black"
),
plotlyOutput(outputId = "grafica")
)
)
server <- function(input, output, session) {
output$grafica <- renderPlotly({
req(input$accion)
plt <- get(input$accion) |>
ggplot(aes(x = date)) +
geom_line(aes_string(y = input$var_y),
color = input$col
)
ggplotly(plt)
})
}
shinyApp(ui, server)

相关内容

最新更新