Open james-t-bila opened 3 years ago
Hi @james-t-bila,
Sorry this has taken me a while to get to. The openxlsx R package is probably the best one to use in this case because it allows reading and writing to excel formatted files. There are others out there but openxlsx has worked the best for me. Take a look at the introduction to openxlsx materials for a bit more information.
With openxlsx, you're first step is to identify where in the excel file you want to edit, for this you need:
You can extract data from an excel file using something like this code:
quarterly_data <- openxlsx::read.xlsx(
xlsxFile = file.path("insert", "path", "to", "xlsx", "file", "here", "filename.xlsx"),
sheet = ["name" or index of sheet - defaults to 1],
startRow = [index of row to start on]
)
Note that you can use the rows
and cols
parameters for the read.xlsx()
to more specifically target which rows and columns to read by their index.
Once extracted, you'll want to identify the index of the column to edit - try something like:
current_quarter <- "Dec08"
index_of_column_to_edit <- which(colnames(quarterly_data) == current_quarter)
You'll need to identify the row to edit in a similar way - by finding the matching commodity name in the commodity column. Something like:
# Find row index for specific commodity of interest
# Using grep to allow for not having the exact commodity name - use which() if you have exact name
commodity <- "bananas"
index_of_row_to_edit <- grep(
pattern = commodity,
x = quarterly_data$[insert name of column with commodity names here]
)
# Check only single row was identified
if(length(index_of_row_to_edit) > 1){
index_of_row_to_edit <- index_of_row_to_edit[1]
warn(paste0("Multiple row indices were identified based on the commodity pattern provided (", commodity, "). First index selected to update."))
}
With the row and column indices of where you would like to edit in the xlsx file. You can update using similar code to the following:
# Load the excel file
workbook <- openxlsx::loadWorkbook(file.path("insert", "path", "to", "xlsx", "file", "here", "filename.xlsx"))
# Define data to update in table <- note this could be a matrix/dataframe of values instead of single value
my_updated_price_per_kilo <- 55
# Edit specific cells within a specific sheet
openxlsx::writeData(
workbook,
sheet = [insert sheet "name" or index here],
startCol = index_of_column_to_edit,
startRow = index_of_row_to_edit,
x = updated_data_to_insert
)
# Save the changes to the excel files
openxlsx::saveWorkbook(
workbook,
file = file.path("insert", "path", "to", "xlsx", "file", "here", "filename.xlsx"),
overwrite = TRUE
)
Let me know if the above is useful, and feel free add more comments here with any issues or questions.
Thanks and apologies again for not getting back to you sooner.
Joe
Want to append the Quarterly Data of the Price per Kilo for Different Quarters of Different Years into the Master spreadsheet of all the Items in the CPI. Inserting the Price per Kilo into specific rows and columns, for instance matching price per kilo of bananas to Dec08 column in master spreadsheet row of bananas