0

When a render this script with the RMarkdown, the path of a file exceeds the limit of the PDF.

df_reserva = read_excel('E:/OneDrive/FACULDADE/Semestre 6/Métodos Quantitativos Aplicados a Políticas Públicas e Sociais/Trabalho Final/Bases de Dados/forms.xlsx')

I want to add some line breaks, so the code looks like this

df_reserva = read_excel('E:/OneDrive/FACULDADE/Semestre 6/    
Métodos Quantitativos Aplicados a Políticas Públicas e Sociais/    
Trabalho Final/Bases de Dados/forms.xlsx')

Any tips here?

Phil
  • 7,287
  • 3
  • 36
  • 66

2 Answers2

1

Does this work?

p1 <- 'E:/OneDrive/FACULDADE/Semestre 6/'
p2 <- 'Métodos Quantitativos Aplicados a Políticas Públicas e Sociais/'
p3 <- 'Trabalho Final/Bases de Dados/forms.xlsx'
df_reserva <- read_excel(paste0(p1, p2, p3))
dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • It works! But is there any possibility to do it like in Python, where we can use "\" to continue on the next line? Thanks a lot! – Felipe Queiroz Jan 07 '22 at 17:41
  • I don't think so since the the backslash and new line will be included in the string. See [Split code over multiple lines in an R script](https://stackoverflow.com/questions/6329962/split-code-over-multiple-lines-in-an-r-script) for some other approaches. – dcarlson Jan 08 '22 at 02:19
1

You should use file.path(). For instance:

df_reserva = read_excel(file.path('E:/OneDrive/FACULDADE/Semestre 6',
                                  'Métodos Quantitativos Aplicados a Políticas Públicas e Sociais',
                                  'Trabalho Final/Bases de Dados/forms.xlsx')

If you use R > 4.1.0 you can also use basepipe (|>) to make it more readable by avoiding nested parenthesis:

file.path('E:/OneDrive/FACULDADE/Semestre 6',
          'Métodos Quantitativos Aplicados a Políticas Públicas e Sociais',
          'Trabalho Final/Bases de Dados/forms.xlsx') |>
    read_excel() -> df_reserva
Mehrad Mahmoudian
  • 3,466
  • 32
  • 36