Search code examples
rxlsxgnupgraw

is there an R function for converting a raw input into an .xlsx file?


So for context I'm trying to decrypt and xlsx file using the gpg package. If I decrypt as_text = TRUE I get an error message with and embedded nul in string.

writexl::write_xlsx(x = data.frame(x = 1:2, y = 1:2),
                    path = "testtemp/test_1/test2.xlsx")

encrypted_xlsx <- gpg::gpg_encrypt(
  data = list.files("testtemp/test_1/",
                    full.names = TRUE,
                    pattern = "xlsx"
  ),
  receiver = "mypublickey"
)

writeLines(
  text = encrypted_xlsx,
  con = "testtemp/test_1/test2.xlsx.gpg"
)

gpg::gpg_import("myprivatekey")
decrypted_file <- gpg::gpg_decrypt(
  data = "testtemp/test_1/test2.xlsx.gpg",
  verify = FALSE,
  as_text = FALSE
)

xl <- readxl::read_xlsx(path = rawConnection(decrypted_file))

What I've tried in the last line doesn't work but hopefully conveys what I'm trying to achieve.

platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 4
minor 0.2
year 2020
month 06
day 22
svn rev 78730
language R
version.string R version 4.0.2 (2020-06-22) nickname Taking Off Again


Solution

  • The solution to this was to write the decrypted output from gpg:gpg_decrypt to file.

    filename = tempfile(fileext = '.xlsx') #create temp file with xlsx suffix
    writeBin(decrypted_file, filename)     #write data to file
    
    xl <- readxl::read_xlsx(path = filename)
    
    print(xl)
    
    # A tibble: 2 x 2
          x     y
      <dbl> <dbl>
    1     1     1
    2     2     2
    

    thanks to https://stackoverflow.com/users/1968/konrad-rudolph for the answer.