Sebastiano
Sebastiano

Reputation: 59

R-studio server returns error after simple if-else code execution

I am currently facing this problem. Analyzing a big data-set (roughly 3 million observations), I need to convert a variable from a format to another. Specifically, I had the date of incorporation of several firms, but coming in two formats: YYYY or MM-DD-YYYY, or other possibilities of which the last 4 characters were always relative to the year.

What I need is just the year so I developed this code:

library(stringi)

for (i in 1:length(amadeus$Dateofincorporation) {
    if(nchar(amadeus$Dateofincorporation[i]) == 4 & 
       !is.na(amadeus$Dateofincorporation[i])) {
        amadeus$Dateofincorporation[i] <- amadeus$Dateofincorporation[i]
    } 
    else if (nchar(amadeus$Dateofincorporation[i]) != 4 & 
             !is.na(amadeus$Dateofincorporation[i])) {
        amadeus$Dateofincorporation[i] <- stri_sub(amadeus$Dateofincorporation[i],-4,-1)
    } 
    else { 
        amadeus$Dateofincorporation[i] <- amadeus$Dateofincorporation[i] 
    }
}

The code executes for a long time, and then returns the output:

Warning messages: 1: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete 2: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 3: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 4: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete 5: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 6: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state

Does anyone have an idea on how to deal with this?

P.S. the vector is currently a character vector, do you think this has an impact?

Upvotes: 4

Views: 21931

Answers (4)

Lazarus Thurston
Lazarus Thurston

Reputation: 1287

Just increase the size of the graphics device (bottom right pane in rstudio) and the warning will vanish. Alernatively clear all the plots already in the graphics pane and you won't see the warning again.

Upvotes: 6

Dragos Bandur
Dragos Bandur

Reputation: 301

It may have nothing to do with the code that you have posted. It happens to me every time I have a plot left in the "Plots" tab from previous sessions, even after I have deleted the plot; the warning messages appear after I enter the first line of code

require(package)

1: In doTryCatch(return(expr), name, parentenv, handler) :
  display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state

The second message repeats as many times as the number of plots left in the "Plots" tab.

I usually get rid of these warnings by entering

dev.off()

See https://community.rstudio.com/t/strange-warning-on-initial-rstudio-statement/57003

Upvotes: 3

notebook
notebook

Reputation: 1

I think the problem is using the package plyr. I rarely use non base R packages, and as soon as I started using plyr I started getting these bizarre warning messages for seemingly innocent statements, like creating a new vector. I removed plyr from the script and re-ran my code, and all the warning messages disappeared!

Upvotes: 0

Sebastiano
Sebastiano

Reputation: 59

It may look weird, but I re-ran the code, and now it works. I mean, still gives the above warning, but the output is the desired one. I don't think it's relevant to understand the origins of the warnings, so thank you all!

Upvotes: 1

Related Questions