自定义函数 - > dataframe - > reactive - > plot - > ERROR(零长度输入不能与非零长度的输入混合)

问题描述 投票:0回答:1

我在R编码一个闪亮的应用程序时遇到问题。当我在不使用闪亮的单独脚本中运行该函数时,如果单独添加绘图和数据框输出,则获取绘图。对不起,首字母缩略词和长代码。目前正在开展一个项目。但是,一旦它闪亮,错误如下:

Warning: Error in mapply: zero-length inputs cannot be mixed with those of non-zero length
Stack trace (innermost first):
    99: mapply
    98: AddMonths
    97: SL [#31]
    96: <reactive:obs2> [#7]
    85: obs2
    84: is.data.frame
    83: plot_ly
    82: "plotly"::"ggplotly" [#24]
    81: func
    80: origRenderFunc
    79: output$plot2
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

有什么问题?

库(闪亮)

ui <- navbarPage("name",  tabPanel("name2",sidebarLayout(
  sidebarPanel(numericInput("AI", "name3", min = 0, max = 100,
                           value = c(25, 40)), sliderInput("AGFS", "name4", min = 0, max = 200000,
                                                            value = 57000, pre = "£"), selectInput("SPI", "SPIType", 
                                                           list("Plan 1", "Plan 2", "No"), selected = NULL, multiple = FALSE,selectize = TRUE, width = NULL, size = NULL), 
                                              numericInput("SLO", "SLOType", min = 0, max = 100000, value = 15000 ), 
                                              sliderInput("EC", "ECType:", min = 0, max = 100, value = 11, post = "%"),
                                             ), mainPanel("the results will go here")))

  ,tabPanel("NIT", sidebarLayout(
    sidebarPanel(sliderInput("ATFB", "ATFBtype:", min = -60000, max = 0, value = -1180.8, pre = "£"),
                 sliderInput("TB", "TBtype:", min = 0, max = 100000, value = 656, pre = "£")),
        mainPanel("the results will go here")))

  ,tabPanel("name5", sidebarLayout(
    sliderInput("BR", "BaseRateType:", min = 0, max = 10, value = 0.25,step = 0.05, post = "%"), sliderInput("RPI", "RPI:", min = 0, max = 10, value = 3.1, step = 0.1, post = "%")), mainPanel(plotlyOutput("plot2"))))



SL <- function(SPI, SLO, BR, RPI, AGFS,EC,ATFB){

  Deb = 17775
  DebMonthly = Deb/12
  Deb2 = 21000
  Deb2Monthly = RepaymentThresholdPlan2Annual/12  
  ECTotal= AGFS*EC
  AGFSBT = AGFS+ATFB-ECTotal

  obs = data.frame(timed=c(Sys.Date()), LO = c(SLO), iint = c(0), total = c(0), R1 = c(0))
  add.months= function(date,n){ seq(date, by = paste (n, "months"), length = 2)[2]}

    if (SPI == "Plan 1"){
    R= ((AGFSBT - Deb)*0.09)/12
    if (min(1+BR, RPI) == RPI){
      smallr <- RPI
    }else{
      smallr <- min(1+BR, RPI)}
    smallr




N = n.period(((1 + smallr) ^ (1 / 12) - 1), -SLO, 0, R, 0)+2
    obs[2,2]= SLO
    obs[2,1] = AddMonths((obs[1,1]),1)

    for (i in 2:N ) {
      obs[i,5]= -R}



    for (i in 3:N ) {
      obs[i,1] = AddMonths((obs[i-1,1]),1)
      obs[2,3]= SLO* ((1 + smallr) ^ (1 / 12) - 1)
      obs[2,4]= obs[2,2]+obs[2,3]


      obs[i,2] = obs[i-1,4]+obs[i,5]
      obs[i,3]= obs[i,2]* ((1 + smallr) ^ (1 / 12) - 1)
      obs[i,4]= obs[i,2]+obs[i,3]
    }
  } else if(SPI == "Plan 2"){
    R= ((AGFSBT - Deb2)*0.09)/12
    if(AGFS<=21000){
      smallr = RPI
    }else if(AGFS>=21000.01 && AGFS<=41000){
      smallr = RPI+(((AGFS-21000.01)/21000.01)*0.03)/((41000-21000)/21000)*100
    }else if (AGFS>=41000.01){
      smallr = (RPI+0.03)}

          for (i in 2:N ) {
      obs[i,5]= -R}



    for (i in 3:N ) {
      obs[i,1] = AddMonths((obs[i-1,1]),1)
      obs[2,3]= SLO* ((1 + smallr) ^ (1 / 12) - 1)
      obs[2,4]= obs[2,2]+obs[2,3]


      obs[i,2] = obs[i-1,4]+obs[i,5]
      obs[i,3]= obs[i,2]* ((1 + smallr) ^ (1 / 12) - 1)
      obs[i,4]= obs[i,2]+obs[i,3]
    }}
   return(obs)
  }


  server <- function(input, output) {

  obs2 <- reactive({SL(input$SPI, input$SLO, input$BR, input$RPI, input$AGFS,input$EC,input$ATFB)})

  output$plot2 <- renderPlotly({plot_ly(x = obs2()$timed, y = obs2()$LO, type = 'scatter')})
r function dataframe shiny reactive
1个回答
0
投票

遗憾的是,你的例子不可复制,please take a notice of it

没有任何方式运行你的代码我假设在plotly部分有一个问题,试试这个 - >我在~x变量之前添加了标志y

  output$plot2 <- renderPlotly({plot_ly(obs2(), x = ~timed, y = ~LO, type = 'scatter')})

如果这不起作用,请尝试在控制台obs2中打印,如果你真的从闪亮的函数SL获取数据:

  obs2 <- reactive({
data <- SL(input$SPI, input$SLO, input$BR, input$RPI, input$AGFS,input$EC,input$ATFB)
print(data)})
© www.soinside.com 2019 - 2024. All rights reserved.