0

一旦从 selectInput 下拉菜单中进行选择,图表中的任何内容都不会发生变化 - 看起来不像输入形式的下拉菜单正在使用或不确定 activeSwitch(activeOnSwitch) 是否正常工作,TIA:

全局.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
}

服务器.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"))

  })
})

用户界面

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")
    )
  )
))
4

0 回答 0