1

I am trying to build a file processor, where I can upload the raw file, click the button, and get the sorted report. So in this instance, I m wanting to see domain performance for different lines, so it splits the data set and writes to a .xlsx file, where the data splits for each tab based on the line.

It runs fine on RStudio, but when I publish it, I get the following errors. Either of the 2 :

error in [[ subscript out of bounds no-stack-trace-available

warning-error-in-if-argument-is-of-length-zero-no-stack-trace-available

library(openxlsx) 
library(readxl)
library(writexl) 
library(dplyr) 
library(magrittr) 
library(lubridate)
library(shiny) 

ui <- fluidPage(
  titlePanel("Domain Performance"),
  sidebarLayout( 
    sidebarPanel( 
      
      fileInput('file1', 'Upload file',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv','.xlsx'))
      
    ),
    
    mainPanel(
      #actionButton(inputId = "run",label = "Run the Code"),
      downloadButton('sortspend',"Spend"),
      tableOutput("outdata1")
      
    )
    
  ))



server <- function(input, output) {
  
  options(shiny.maxRequestSize=90*1024^2)  
  
  output$outdata1 <- renderTable({
    
    req(input$file1)
    if(is.null(input$file1)) {
      
      return()
      
    }
    
    else {
      
      input$file1
      
    }
    
    
  })
  
  
  spenddom <-reactive({
    
    req(input$file1)
    dspec <- input$file1
  
  domain_raw <- read.csv(dspec$datapath,header = TRUE)

  
  
  linenames = unique(domain_raw['Line'])
  lineids = unique(domain_raw$`Line Id`)
  
  for (i in seq_along(lineids)){
    
    
    ddf <-domain_raw%>% select(`Line Id`,Domain,`Advertiser Spending`,Impressions,Clicks,Conversion,Line)%>%
      filter(`Line Id` == lineids[i])%>%
      group_by(Line,Domain)%>%
      summarise(Spend = sum(`Advertiser Spending`), Imp = sum(Impressions), Clicks = sum(Clicks), Conversions = sum(Conversion))%>%
      mutate(CPA = Spend/Conversions)%>%arrange(desc(Spend))
    
  }
  


  })
  
  tabcreator <- reactive({
    
    req(input$file1)
    dspec <- input$file1 
    domain_raw <- read.csv(dspec$datapath,header = TRUE)
    
    linenames = unique(domain_raw['Line'])
    lineids = unique(domain_raw$`Line Id`)
    
    
    for (i in seq_along(lineids)){
      
  
      tabname <- lineids[i]
      
    }
    
    
  })
  
  
  
  output$sortspend<-downloadHandler(
    
    filename = function(){paste0("domainperfdl",".xlsx")},
    
    content = function(fname){
      
      write.xlsx(spenddom(),fname,sheetName = tabcreator())
      
    },
    
    contentType = "application/xlsx"
    
  )
  
}

shinyApp(ui=ui, server=server)

I m trying to understand what am I doing wrong here. It's the first time I m using for loops with R.

vinidapooh
  • 11
  • 2
  • Since there is not a literal `[[` in your code, it's a little difficult to troubleshoot. Do you know in which line of your script that error is generated? – r2evans Aug 24 '21 at 16:57
  • FYI, `output$outdata1` is largely moot: since you lead with `req(input$file1)`, if it truly is `NULL` already, it will not get to the `if` statement; instead, it will not attempt to render the table until `input$file1` is "truthy" (see `?isTruthy` and `?req`). Instead, you can just do `output$outdata1 <- renderTable({ req(input$file1); })` (since `req` returns its first argument). – r2evans Aug 24 '21 at 17:01
  • Thanks for that. I added the req() to try and see if it gives me the error statement. I was going for if itself. But how do I get to know the exact line where the error is coming? – vinidapooh Aug 24 '21 at 17:26
  • No easy way other than adding `message(..)` lines periodically to narrow down where it is happening. Unfortunately, when running on RSConnect it's a bit more difficult, so inserting some form of logging is all we can do. – r2evans Aug 24 '21 at 17:36
  • Okay cool, I run the same thing again, even after removing the if above in your earlier comment. This is the error I am getting in the server log now. Warning: Error in if: argument is of length zero – vinidapooh Aug 24 '21 at 17:53
  • The only `if` statement you show now is `if (is.null(input$file1))` which should not be producing that problem, so it's likely inside some other code. I suggest you update your code above to reflect what you're since changed it to, then narrow down in which code it's happening. – r2evans Aug 24 '21 at 18:06

0 Answers0