Learn R Programming

XLConnect (version 1.1.0)

clearNamedRegion-methods: Clearing named regions in a workbook

Description

Clears named regions in a workbook.

Usage

# S4 method for workbook,character
clearNamedRegion(object, name, worksheetScope)

Arguments

object

The workbook to use

name

The name of the named region to clear

worksheetScope

Optional - the name of the worksheet in which the region is scoped; useful if different sheets have scoped regions with the same name.

Author

Nicola Lambiase
Mirai Solutions GmbH https://mirai-solutions.ch

Details

Clearing a named region/range means to clear all the cells associated with that named region. Clearing named regions can be useful if (named) data sets in a worksheet need to be replaced, i.e. data is first read, modified in R and finally written back to the the same named region. Without clearing the named region first, (parts of) the original data may still be visible if they occupied a larger range in the worksheet.

If worksheetScope is unspecified, the first matching name found anywhere in the workbook will be cleared. Otherwise, only a name specifically scoped to the worksheet may be cleared. To only clear a name in global scope, pass "" as the value.

See Also

workbook, clearSheet, clearRange, clearRangeFromReference, clearSheet

Examples

Run this code
  if (FALSE) {
# mtcars xlsx file from demoFiles subfolder of 
# package XLConnect
demoExcelFile <- system.file("demoFiles/mtcars.xlsx", 
                             package = "XLConnect")

# Load workbook
wb <- loadWorkbook(demoExcelFile)

# Read named region 'mtcars'
data <- readNamedRegion(wb, name = "mtcars", header = TRUE)

# Only consider cars with a weight >= 5
data <- data[data$wt >= 5, ]

# Clear original named region
clearNamedRegion(wb, name = "mtcars")

# Write subsetted data back
# Note: this is covering a smaller area now -
# writeNamedRegion automatically redefines the named region
# to the size/area of the data
writeNamedRegion(wb, data = data, name = "mtcars",
                 header = TRUE) 
} 

Run the code above in your browser using DataLab