Warning: Error in if: argument is of length zero [No stack trace available]

Here lovely ones, I try to run this code on shiny but inserting my input elements leaves me with the error:

Warning: Error in if: argument is of length zero
[No stack trace available]

what could be the cause?

library(shiny)
library(shinythemes)
library(RODBC)
library(dplyr)
library(ggplot2)

#UI CODE

ui <- shinyUI(fluidPage(
headerPanel(title="STATISTICAL PROCESS CONTROL"),
sidebarLayout(
sidebarPanel(
tags$h2("Input:"),
textInput(inputId="txt1",label= "sql script:"),
textInput(inputId="txt2",label="oracle script:"),

), # sidebarPanel

mainPanel(
  tabsetPanel(type = "tab",
              tabPanel("SPC MODEL",plotOutput("spc")),
              tabPanel("plot_rowcount",plotOutput("plot_rowcount")),
              tabPanel("plot_cell",plotOutput("plot_cell"))
              
  ) # mainPanel
), # Navbar 1, tabPanel

) # navbarPage
) # fluidPage
)

SERVER CODE

server <- function(input, output) {

observeEvent(input$txt1, {
print(input$txt1)
})

observeEvent(input$txt2, {
print(input$txt2)
})
SPC <- reactive({
sql_script <- req(input$txt1) %>% as.character()

oracle_script <- req(input$txt2) %>% as.character()

con=odbcConnect("dsn",uid="", pwd="Analytics10$")
on.exit(odbcClose(con))
if(odbcClose(db.con)!=TRUE) 
sql_db=sqlQuery(con,"sql_script")

#ORACLE_DB_CONNECTION
con_2 <- odbcConnect("oracle_dsn",uid="", pwd="password10$")
on.exit(odbcClose(con_2))
odbcQuery(con_2,"oracle_script")
oracle_db = sqlGetResults(con_2, as.is=FALSE, errors=FALSE, max=1000000, buffsize=1,nullstring=NA, na.strings="NA", believeNRows=TRUE, dec=getOption("dec"))

#ROW_COUNT_STATISTICS
source_count= nrow(oracle_db)
Destination_count=nrow(sql_db)
spool_status = if (source_count>Destination_count) {print("incomplete")} else if (source_count<Destination_count) {print("duplicate error")} else {print("complete")}
ROW_COUNT_STATISTICS = data.frame(source_count,Destination_count,spool_status) 

#CELL_STATISTICS
missing_fields= length(which(is.null(sql_db)))
No_cell_fields=nrow(sql_db)*ncol(sql_db)
complete_fields= No_cell_fields - missing_fields
percentage_cell_completeness= complete_fields/No_cell_fields *100
CELL_STATISTICS= data.frame(missing_fields,complete_fields,percentage_cell_completeness)

spc= list(ROW_COUNT_STATISTICS=ROW_COUNT_STATISTICS,CELL_STATISTICS=CELL_STATISTICS)


#PLOTTING A GRAPH
options(scipen=999)
library(ggplot2)

#ROW COUNT PLOT
ROW_COUNT= c(source_count,Destination_count)
Table=c("Source","Destination")
ROW_COUNT_STAT_ggplot=data.frame(ROW_COUNT,Table)
plot_1 = ggplot(ROW_COUNT_STAT_ggplot,aes(x= Table,y=ROW_COUNT)) + geom_point(aes(col=spool_status), size=5) + ggtitle("ROW PLOT") 
p= if (source_count>Destination_count) {source_count} else {(Destination_count)}
measurement=coord_cartesian(ylim=c(0,p))
plot_rowcount=plot_1+measurement



#CELL_PLOT
fields= c("Missing_fields","Complete_fields")
Cell_count=c(missing_fields,complete_fields)
CELL_PLOT_ggplot=data.frame(fields,Cell_count)
plot_2= ggplot(CELL_PLOT_ggplot,aes(x= fields,y=Cell_count))+ geom_count(aes(fill=percentage_cell_completeness)) + ggtitle("CELL PLOT")
measurement_2=coord_cartesian(ylim=c(0,No_cell_fields))
plot_cell= plot_2 +measurement_2

return(list(spc=spc,plot_rowcount = plot_rowcount,plot_cell=plot_cell))

})

output$spc <- renderPlot({
req(SPC())
SPC()$spc
})

output$plot_rowcount <- renderPlot({
req(SPC())
SPC()$plot_rowcount
})

output$plot_cell <- renderPlot({
req(SPC())
SPC()$plot_cell
})

}

shinyApp(ui, server)

this is wrong, this is trying to execute the fixed string 'sql_script' as a sql query, rather it should be the contents of the variable sql_script (presumably)

sql_db<-sqlQuery(con,sql_script)

ditto the oracle_script

thanks, but waht do you mean by 'detto the oracle script'?

ditto means, the same thing discussed already applies in the other case also

ok, maybe I dont totally understand your point, but I made the queries the user would input as the input id and called that input = sql_script, so I do not see how sql_script is fixed as different users would input different scripts to verify if their spool is complete or not.

ok try this

#setup
mystring <-"this is my string"

#are these the same ?
print(mystring)
print("mystring")

no they arent, the first prints out what you equated it to be, the second can only print what is inside the bracket

I'm suggesting that the same phenomenon is at work here

sql_db=sqlQuery(con,"sql_script")
#rather than
sql_db=sqlQuery(con,sql_script)

Ok I understand, so I should use this?

sql_db=sqlQuery(con,sql_script)

Thank you very very much, your explanations and answers has really fostered the completion of my project

1 Like