2017-12-03 105 views
0

我想從服務器反應性數據做一個情節。不幸的是,我無法得到工作的陰謀。我收到如下錯誤:「錯誤:EXPR必須是長度爲1的矢量」。我嘗試了不同樣式的圖表和不同的庫:Quantmod,ggplot等等。有什麼建議麼?與來自服務器的反應性數據呼叫的閃亮應用程序

Server: 
library(shiny) 

Dat<-read.csv("A:\\home\\Documents\\Franchise_Failureby_Brand2011.csv", sep=';') 
names(Dat)[1]<-paste("Brand") 
names(Dat)[2]<-paste("Failure") 
names(Dat)[3]<-paste("Disbursement") 
names(Dat)[4]<-paste("Disb$X$1000") 
names(Dat)[5]<-paste("Chgoff") 
Dat1<-Dat[is.na(Dat)==FALSE,] 
Dat<-Dat1[1:578,] 

# Define server logic required to draw a histogram 
shinyServer(function(input, output) { 
    DatSv <- reactive({ 
    Value <- switch(input$Value, 
        "Failure"= Dat$Failure[1:10], 
        "Disbursement"=Dat$Disbursement[1:10], 
        "Disb$X$1000"=Dat$`Disb$X$1000`[1:10], 
        "Chgoff"=Dat$Chgoff[1:10]) 
    Brand<-Dat$Brand[1:10] 
    Brand(input$Value) 
    }) 

# Generate plot 
    output$plot1 <- renderPlot({ 
    library("quantmod") 
    hist(DatSv(), 
     main=paste('r', Value, '(', Brand, ')', sep='')) 
    }) 

    # Generate summary of data 

    output$summary<-renderPrint({ 
    summary(Dat) 
    }) 

}) 


UI: 
library(shiny) 
shinyUI(fluidPage(
    titlePanel("Plot Franchise Failure"), 
    sidebarLayout(
    sidebarPanel(
     radioButtons("n", "Chose output Y Axis:", 
        c("Failure" , 
        "Disbursement", 
        "Disb$X$1000" , 
        "Chgoff")), 

     checkboxInput("show_xlab", "Show/Hide X Axis Label", value=TRUE), 
     checkboxInput("show_ylab", "Show/Hide Y Axis Label", value=TRUE), 
     checkboxInput("show_title", "Show/Hide Title") 
    ), 
    mainPanel(
    tabsetPanel(
     type = "tabs", 
     tabPanel("Plot", plotOutput("plot1")), 
     tabPanel("Summary", verbatimTextOutput("summary")) 
) 
) 
) 
) 
) 
+1

你的問題可能來自交換機的聲明。嘗試在切換之前添加'req(input $ Value)'。 –

回答

0

嗨問題來自於將用戶界面中的輸入連接到服務器。在UI中,你已經爲radioButton提供了inputid = "n"。這意味着我們可以通過input$n而不是input$Value獲得Radibuttons的值。由於inputid = "Value"沒有輸入,因此後者始終爲NULL。我的代碼還有一些小問題,但這裏是服務器代碼的工作版本。我沒有修改用戶界面

library(shiny) 

Dat<-read.csv("A:\\home\\Documents\\Franchise_Failureby_Brand2011.csv", sep=';') 
names(Dat)[1]<-paste("Brand") 
names(Dat)[2]<-paste("Failure") 
names(Dat)[3]<-paste("Disbursement") 
names(Dat)[4]<-paste("Disb$X$1000") 
names(Dat)[5]<-paste("Chgoff") 
Dat1<-Dat[is.na(Dat)==FALSE,] 
Dat<-Dat1[1:578,] 

# Define server logic required to draw a histogram 
shinyServer(function(input, output) { 
    DatSv <- reactive({ 
    switch(input$n, 
      "Failure"= gsub("%","",as.character( Dat$Failure)), 
      "Disbursement"=Dat$Disbursement, 
      "Disb$X$1000"=gsub("\\$","",as.character( Dat$`Disb$X$1000`)), 
      "Chgoff"=gsub("%","",as.character(Dat$Chgoff))) 

    }) 

    # Generate plot 
    output$plot1 <- renderPlot({ 
    library("quantmod") 
    hist(as.numeric(DatSv()), 
     main=paste('Histogram of ',input$n, sep=''), 
     xlab = input$n) 
    }) 

    # Generate summary of data 

    output$summary<-renderPrint({ 
    summary(Dat) 
    }) 

}) 
相關問題