Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams \end{document}

\input{folder/file.tex} command in LaTeX fetches the text in the file from the folder. When the main file is run, all the text from file.tex is compiled and included.

Rmd - desired output

I am using R Markdown for my project. I desire to develop and document different topics in different files.

I want to have the following directory structure:

main.Rmd
main_directory
        file1.Rmd
        file2.Rmd
    figures
    read_me

Explored pathways

I have explored the following routes to achieve this.

As suggested in Link 1

sys.source(file = file.path(getwd(), "main_directory", 
                        "code", 
                        "file1.r"))

Here, the code text is not included in the output HTML knit from the Rmd.

  • As suggested in Link 2
  • ```{r, child=file.path(getwd(), "main_directory", "code", "file1.Rmd"}
    

    It led to the following error:

    Error: attempt to use zero-length variable name
    

    Here, the code text is not included in the output HTML knit from the Rmd.

  • As suggested in Link 3
  • ```{r}
    includeMarkdown(file.path(getwd(), "main_directory", "code", "file1.Rmd"))
    

    It also led to the following error:

    Error: attempt to use zero-length variable name
    

    Question: How to include the Rmd code into main file similar to LaTeX input

    The equivalent of the LaTeX \input{"/path/to/file"} is:

    ```{r include=TRUE, echo=FALSE, results="asis"}
    library(readr)
    cat(readr::read_file("/path/to/file"))
    

    For many input files you can consider loading readr at the beginning together with the definition of some input_myfile() function. I hope this helps!

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.