0

i'm having issues in R coding a Shiny App. When i run the function in a separate script not using shiny i get the plot if add the plot separately and the dataframe output. Sorry for the acronyms and long code. Currently working on a project. However, once it is in shiny, the error is the following:

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>

What is the issue?

library(shiny)

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')})

1 Answers1

0

Your example is unfortunately not reproducible, please take a notice of it.

Without any way to run Your code i assume there is a problem in plotly part, try this out -> i have added sign ~ before x and y variables

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

If this will not work try to print in console obs2 if You actually get the data from function SL in Shiny:

  obs2 <- reactive({
data <- SL(input$SPI, input$SLO, input$BR, input$RPI, input$AGFS,input$EC,input$ATFB)
print(data)})
Mal_a
  • 3,670
  • 1
  • 27
  • 60