1

I have an Excel file and a cell which contains a newline.

1. , : Yes, number of years: ______,\n    : Year at which exposure stopped: _______

After I used the rio package to import the Excel file, I have a double backslash:

 df <- rio::import("excel_file.xlsx")

 print(df[1,])

"1. , : Yes, number of years: ______,\\n    : Year at which exposure stopped: _______" 

I would like to import the string as \n without have to use the gsub/sub function, I tried to change the line into:

1. , : Yes, number of years: ______,'\n'    : Year at which exposure stopped: _______
1. , : Yes, number of years: ______,/n    : Year at which exposure stopped: _______
1. , : Yes, number of years: ______,\\n    : Year at which exposure stopped: _______

but it didn't work.

My desired output is to have \n after running rio::import:

"1. , : Yes, number of years: ______,\n    : Year at which exposure stopped: _______"

Do you have any advice?

Peter Chung
  • 1,010
  • 1
  • 13
  • 31
  • 1
    Try `cat(myString)`, do you still see the extra backslash? – zx8754 Jun 14 '23 at 07:00
  • 1
    `"I used rio package"` please share the code., – zx8754 Jun 14 '23 at 07:02
  • does the initial file actually have a newline, or just the characters "\n"? Because the double backslashes seems to indicate that it is an escaped backslash. I created [a spreadsheet with a newline in a cell](https://docs.google.com/spreadsheets/d/1DDiBQwP2DzHi3rC51sBhWIl7E71s58ZK5iwsgymWVmA/edit?usp=sharing), and after importing with ```rio``` I got ```[1] Hello\nWorld! <0 rows> (or 0-length row.names)``` – Mark Jun 14 '23 at 07:55
  • @MarkHill it only marks \n, I tried to rio::export it into excel to check, it becomes a newline in excel, not a character, but in my initial excel file, it is the character \n. – Peter Chung Jun 15 '23 at 01:40
  • How odd! So, from what I gather, the gist of the problem is that the spreadsheet you have, when imported, gives you something with a ```\\n```, when you would prefer ```\n``` (feel free to correct me) Right now, it's unclear where the issue lies. I'm assuming because I was able to make a spreadsheet with only one backslash, the issue is not with R or rio in general. Could you upload the file? If the issue can be replicated on someone else's computer, the issue is with rio, or the file. If not, it's something with your install of rio – Mark Jun 15 '23 at 10:42

0 Answers0