2017-02-16 82 views
0

我有一個問題,關於shiny和使用滑塊,當顯示chart_Series和彩色線條。從右側使用滑塊時,適當選擇顏色(主要是紅點)。當我使用左側滑塊(例如查看最新數據)時,顏色選擇不當(應爲綠色)。我尋求幫助,我很感謝您的任何建議!R Shiny quantmod zoomChart和固定着色的點

require(quantmod) 
require(shiny) 
getSymbols("YHOO") 
data <- YHOO 
data <- data[1:100,] 

col_function <- rep("red",50) 
col_function <- c(col_function, rep("green",50)) 


plot <- { 

    date_range <- index(data) 


    if (interactive()) { 
    options(device.ask.default = FALSE) 
    # Define UI 
    ui <- fluidPage(
     titlePanel("Time:"), 
     sidebarLayout(
     # Sidebar with a slider input 
     wellPanel(
      tags$style(type="text/css", '#leftSlide { width:200px; float:left;}'), 
      id = "leftSlide", 
      sliderInput("Range", "Choose Date Range:", 
         min = first(date_range), max = last(date_range), value = c(first(date_range),last(date_range))) 
     ), 

     mainPanel(
      plotOutput("Plot", width = "150%", height = "500px") 
     ) 
    ) 
    ) 

    # Server logic 
    server <- function(input, output) { 
     output$range <- renderPrint({ input$slider2 }) 
     output$Plot <- renderPlot({ 

     x_ti2 <- xts(rep(1, NROW(data)), order.by = index(data)) 
     x_ti2[1, ] <- 0.5 

     chart_Series(data) 
     add_TA(x_ti2, col = col_function, pch = 9, type = 'p', cex = .7) 
# Another way below, but the color function is still not working 
     #chart_Series(data["2017"], TA = 'add_TA(x_ti, col = col_function, pch = 15, type = "p", cex = .7); add_TA(x_ti2, col = "orange", pch = 9, type = "p", cex = .7)') 

     zoom_Chart(paste(input$Range, collapse = "::")) 
     }) 

     observe({ 
     print(input$Range) 
     }) 
    } 

    shinyApp(ui, server) 
    } 
} 

plot 
+0

我已經看過chartSeries以及chart_series。兩者似乎都有相同的問題。從我的角度來看,情節的縮放並不能控制尖銳的線條。看起來好像它們獨立於chartSeries。但是,這仍然不能解釋從另一側正確放大的原因。此外,我測試了dateRangeInput:發生了同樣的問題。 – Simon

回答

0

所以,我發現我自己的問題解決方法:

據我所知,shiny將編輯的數據,但不着色向量(在各自漂亮的邏輯......) 。因此,你必須告訴閃亮更新着色的範圍。

首先,我將着色結果轉換爲xps對象。然後我將x_ti2變量添加到原始數據矩陣中,並使用輸入$範圍爲col_function定義了一個變量。該變量需要全局環境的一部分(例如閃亮可以使用它)。我希望這會解決一些問題,如果有人有類似的問題。