Write Excel specific cell

My Excel file looks like the following

screen-excel

> df 
# A tibble: 2 x 3
  year  item  q3   
  <chr> <chr> <chr>
1 2021  A     7576 
2 2021  B     2432 

How can I update the Excel file with df data? That is, how can I fill q3( row 11, column E) in the Excel file with q3 of df?

library(dplyr)
library(readxl)
library(writexl)

# Toy Excel data----
df0 <- structure(list(year = c("2019", "2019", NA, NA, "year", "2020", 
                               "2020", NA, NA, "year", "2021", "2021"),
                      item = c("A", "B", NA, NA, "item", "A", "B", NA, NA, "item", "A", "B"),
                        q1 = c("2321", "4654", NA, NA, "q1", "2321", "3654", NA, NA, "q1", "7978", "6787"),
                        q2 = c("3543", "4654", NA, NA, "q2", "3543", "7987", NA, NA, "q2", "4654", "6870"),
                        q3 = c("7576", "2432", NA, NA, "q3", "7576", "2432", NA, NA, "q3", NA, NA),
                        q4 = c("13213", "34543", NA, NA, "q4", "13213", "34543", NA, NA, "q4", NA, NA)),
                        row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
# create Excel file to be used  
writexl::write_xlsx(list(example1 = df0), "myfile.xlsx") 

# Data to be exported----
df <- tibble(year = c("2021", "2021"),
             item = c("A", "B"), 
             q3 = c("7576", "2432"))


  1. One way is : to do things in R (first read the file and process in R) and save or overwrite it as below.
df1<-readxl::read_excel("myfile.xlsx", sheet = "example1")
df1$q3[11:12]<-df$q3
writexl::write_xlsx(list(example1 = df1), "myfile.xlsx") 
  1. You can consult writeData fucntion in "openxlsx" package. One problem of this function, you can not append something in an existing worksheet. So, it is difficult to use it in your case. If you want to write something in a new worksheet, you can use this function easily.

@mhakanda many thanks for the suggestions!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.