Skip to content

preview option to see changes #267

@yonicd

Description

@yonicd

worthwhile for PR?

style_preview <- function(obj){
  
  on.exit({unlink(mydir,recursive = TRUE,force = TRUE)},add = TRUE)
  
  mydir <- file.path(tempdir(),'styler')
  
  dir.create(mydir,showWarnings = FALSE)
  
  if( !file.exists(obj) ){
    cat(obj,file = file.path(mydir,'original.R'),sep='\n')
  }else{
    file.copy(obj,file.path(mydir,'original.R'),overwrite = TRUE)
  }
    
  this_text <- readLines(file.path(mydir,'original.R'))
  
  styled <- styler::style_text(this_text)
  
  cat(styled,file = file.path(mydir,'styled.R'),sep='\n')
  
  diffr::diffr(file.path(mydir,'original.R'),file.path(mydir,'styled.R'))  
  
}

ugly_code <- "a<-function( x){1+1}           "

style_preview(ugly_code)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions