2017-10-02 16 views
0

私は基本データをダウンロードして株データをダウンロードし、3つの技術指標を実行しています。ShinyとRが3つのグラフを1つに結合する

これはコードです:

library(shiny) 
library(quantmod) 
library(dygraphs) 
library(TTR) 

ui <- shinyUI(fluidPage(
    titlePanel("Simple Stock Charting App"), 

    sidebarLayout(
    sidebarPanel(
     textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE") 
    ), 
    selectInput("var", label = "bals", choices=list("RSI","Price","ADX")), 


    ### uncomment for dygraphs chart 
    mainPanel(dygraphOutput("plot"),dygraphOutput("plot2"),dygraphOutput("plot3")) 
) 
)) 


server <- shinyServer(function(input, output) { 

    dataInput <- reactive({ 
    prices <- getSymbols(input$symb, auto.assign = FALSE) 
    }) 

    output$plot <- renderDygraph({renderPlot 
    dygraph(Ad(dataInput())) %>%dyRangeSelector() 
    }) 

    output$plot2 <- renderDygraph({renderPlot 
    dygraph((RSI(Ad(dataInput()), n = 14))) %>%dyRangeSelector() 
    }) 
    output$plot3 <- renderDygraph({renderPlot 
    dygraph((ADX(HLC(dataInput()),n = 14))) %>%dyRangeSelector() 

    }) 
}) 


shinyApp(ui,server) 

私はそれが唯一の3つの指標のそれぞれの時間を選択するユーザー可能であるかどうかを知りたいと思います。現在、3つすべてが表示されていますが、グラフを変更するRSI、Value、およびADXの選択に基づいて、1つのグラフを作成することが可能です。

答えて

2

あなたが望むもののためにswitchを使用することができます。

library(shiny) 
library(quantmod) 
library(dygraphs) 
library(TTR) 

ui <- shinyUI(fluidPage(
    titlePanel("Simple Stock Charting App"), 

    sidebarLayout(
    sidebarPanel(
     textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE") 
    ), 
    selectInput("var", label = "bals", choices=list("RSI","Price","ADX")) 
), 
    ### uncomment for dygraphs chart 
    mainPanel(dygraphOutput("plot")) 
)) 

server <- shinyServer(function(input, output) { 

    dataInput <- reactive({ 
    getSymbols(input$symb, auto.assign = FALSE) 
    }) 
    output$plot <- renderDygraph({ 
    data <- switch(input$var,"RSI" = RSI(Ad(dataInput()), n = 14), 
        "Price" = Ad(dataInput()), 
        "ADX" = ADX(HLC(dataInput()),n = 14)) 
    dygraph(data) %>%dyRangeSelector() 
    }) 
}) 

shinyApp(ui,server) 

enter image description here

関連する問題