Search code examples
rshinystockshinyapps

Making an app that shows different stock values | Beginner


so im pretty new in R and programming for that matter.

I have to make an app that shows me the graph and the table of the data of the action that I choose within the input, as well as that I can choose between the variable that I want to see in the graph. I'm a bit lost and the code no longer works for me.

Here's the code:

#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)

Hopefully you'll help me in what to do. Thanks


Solution

  • We can use get function and avoid the if statements. Another approach can be to download the stock prices upon request from the user.

    # 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)