Learn R Programming

XLConnect (version 1.0.7)

clearNamedRegion-methods: Clearing named regions in a workbook

Description

Clears named regions in a workbook.

Usage

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

Arguments

object

The workbook to use

name

The name of the named region to clear

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.

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