闪亮的应用程序与服务器的响应数据调用

时间:2017-12-03 09:32:58

标签: r shiny reactive

我正在尝试使用来自服务器的反应数据绘制图表。不幸的是我无法让情节发挥作用。我收到如下错误:“错误: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 个答案:

答案 0 :(得分:0)

问题来自于将UI中的输入与服务器连接。在UI中,您为radioButtons提供了<HTML> <HEAD> <TITLE>Scripting the HTA:APPLICATION Tag</TITLE> <HTA:APPLICATION ID="oHTA" > <SCRIPT LANGUAGE="VBScript"> Option Explicit Dim cmdLineArray Dim strHello ' This code assumes that you have no spaces in ' the path to Hello.hta. (In other words, this code ' splits the command line by spaces and assumes ' that your name is the second word.) ' cmdLineArray = Split(oHTA.commandLine) strHello = "Hello " & cmdLineArray(1) & ", " _ & "How are you?" MsgBox strHello </SCRIPT> </HEAD> <BODY> </BODY> </HTML> 。这意味着我们可以使用inputid = "n"而非input$n获取Radiobuttons的值。后者始终为input$Value,因为NULL没有输入。我的代码还有其他一些小问题,但这是服务器代码的工作版本。我没有修改UI

inputid = "Value"