R闪亮代码执行后生成空白pdf
Ui代码:
library("shiny")
shinyUI(fluidPage(
titlePanel(h4("Monthly Universe change Chart", align = "center")),
sidebarLayout(
sidebarPanel(
selectInput("Universeselected","Universe Options", choices = c("ALLFO_ALL","ALLFO","ALLFOPRICETHRESH",
"ACTIVEOPTIONS","ACTIVEANALYST","ACTIVEOPTIONS_2",
"ACTIVEANALYST_FACTSET_180","CUSTOM_NIFTY50_SECTORS",
"NIFTY50","MIDCAP50","TOP50ANALYSTNIFTY50",
"ALLFOMINUSNIFTY","ACTIVEFO"),selected = "ALLFO_ALL"),
radioButtons("RB","Select Format",choices = list("pdf","png"))),
mainPanel(
plotOutput("bar"),
downloadButton("downloadData","Download")
)
)
)
)
服务器代码:
library("shiny")
shinyServer(function(input,output){
output$bar <- renderPlot({
if (input$Universeselected == "ALLFO_ALL"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ALLFO_ALL.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ALLFO"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ALLFO.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ALLFOPRICETHRESH"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ALLFOPRICETHRESH.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ACTIVEOPTIONS"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ACTIVEOPTIONS.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ACTIVEANALYST"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ACTIVEANALYST.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ACTIVEOPTIONS_2"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ACTIVEOPTIONS_2.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
} else if (input$Universeselected == "ACTIVEANALYST_FACTSET_180"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ACTIVEANALYST_FACTSET_180.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
} else if (input$Universeselected == "CUSTOM_NIFTY50_SECTORS"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/CUSTOM_NIFTY50_SECTORS.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
} else if (input$Universeselected == "NIFTY50"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/NIFTY50.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
} else if (input$Universeselected == "MIDCAP50"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/MIDCAP50.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "TOP50ANALYSTNIFTY50"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/TOP50ANALYSTNIFTY50.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ALLFOMINUSNIFTY"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ALLFOMINUSNIFTY.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}else if (input$Universeselected == "ACTIVEFO"){
bar2 <- read.csv("D:/NIRAJ/Mydata/Task/code/files/ACTIVEFO.csv", check.names = FALSE, stringsAsFactors = FALSE)
bar2 <- bar2[(nrow(bar2)-1),-1]-bar2[(nrow(bar2)),-1]
test <- which(bar2 == -1 | bar2 == 1)
result <- bar2[,test]
if(!is.null(result[1,1])){
barplot(as.matrix(result), xlab = "Universe Change",las = 2, ylab = "0 to 1 or 1 to 0 Changes",col = "red", space =0.05,cex.names = 0.7)
}else NULL
}
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Universeselected, input$RB , sep = ".")
},
content = function(file) {
if(input$RB == "pdf"){
pdf(file)
}else {
png(file)
}
dev.off()
}
)
})
})
可能存在问题的实际下载代码:
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Universeselected, input$RB , sep = ".")
},
content = function(file) {
if(input$RB == "pdf"){
pdf(file)
}else {
png(file)
}
dev.off()
}
)
导出的PDF数据。