进行下拉选择时,R闪亮图形不会改变

时间:2017-03-29 11:11:03

标签: r input ggplot2 shiny reactive

一旦从selectInput下拉菜单中进行选择,图表中没有任何变化 - 从使用中的下拉列表看起来不像输入,或者不确定activeSwitch(activeOnSwitch)是否正常工作,TIA:

global.R

today = format(Sys.time(), "%m%d%Y")
MovingAverageNumber = 12
StartNumber = 1

as.Date_origin <- function(x){
  as.Date(x, origin = min(index))
}

# Load Datahead()
ValuationData <- read.csv("N:/DATA/GFI/GLOBAL RQA/MAPS/Global All/Working Folders/2017/March/Bbg World Sector Valuation v2.csv", header=T,row.names=1,
                      stringsAsFactors = FALSE, 
                      na.strings = c("N/A", "#N/A", "NA", "Null", ""))

index <- as.Date(rownames(ValuationData), format = "%m / %d / %Y")
ValuationData['dt'] = index
origin <- min(index)
labels <- index

legendlabels = c(labels[MovingAverageNumber+1],labels[seq.int(1L, length(index), 12L)],labels[length(index)])


    ma <- function(arr, n=MovingAverageNumber){
res = arr
for(i in n:length(arr)){
res[i] = mean(arr[(i-n+1):i])
}
res
}

server.R

library("ggplot2", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("scales", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("shiny", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("packrat", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("zoo", lib.loc="H:/My Documents/R/R-3.2.1/library")


shinyServer(function(input, output) {

output$distPlot <- renderPlot({ 

activeSwitch <- function(x) {switch(x,Active=1,Absolute=0)}
activeOnSwitch <- input$ActiveOrAbsolute
Active <- ma(ValuationData[,StartNumber]) - ma(ValuationData[,1])*(activeSwitch(activeOnSwitch))
#Active <- ma(ValuationData[,StartNumber]) - ma(ValuationData[,1])*(0)
ActiveChange <- Active[1:length(Active)-1] - Active[2:length(Active)]


ggplot(ValuationData[MovingAverageNumber+1:length(index),],aes(
  Active[MovingAverageNumber+1:length(index)], 
  ActiveChange[MovingAverageNumber+1:length(index)-1],
  colour=as.integer(dt))) +
  geom_point(alpha = 0.6) +
  scale_color_gradientn(name = "Dates",
                        colours = rainbow(7), 
                        breaks = as.integer(legendlabels), 
                        labels = format(legendlabels, "%b-%y")) +
  geom_path(size=3) +
  xlab("12 Month Average P/E Fwd 12 Month") +ylab("12 Month Average Monthly Change in P/E Fwd 12 Month") +
  ggtitle( paste(colnames(ValuationData)[StartNumber], Title)) +
  theme(panel.background = element_rect(fill = "grey80"))

  })
})

ui.R

library("ggplot2", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("scales", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("shiny", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("packrat", lib.loc="H:/My Documents/R/R-3.2.1/library")
library("zoo", lib.loc="H:/My Documents/R/R-3.2.1/library")




shinyUI(fluidPage(


  # Application title
  titlePanel("Valuation Cycles"),

  sidebarLayout(

    selectInput("ActiveOrAbsolute", "Active Or Absolute:",
            c("Active" = 1,
              "Absolute" = 0 )),

# Show a plot of the generated distribution
mainPanel(
  plotOutput("distPlot")
    )
  )
))

0 个答案:

没有答案