+ Reply to Thread
Results 1 to 10 of 10

Thread: knitr

  1. #1
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    knitr




    From what I can deduce, latex code below should print R code in a PDF. Note I'm trying to embed code in a .tex file. However its not working - do you know what I'm doing wrong?

    Code: 
    \documentclass{article}
    \usepackage{graphicx}
    \newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
    \begin{document}
    
    this is my text \rinline{1+1}
    
    this is more text \rinline{x} % created object in R: x <- 1:10
    
    \end{document}

  2. #2
    ggplot2orBust
    Points: 34,697, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,932
    Thanks
    1,339
    Thanked 743 Times in 667 Posts

    Re: knitr

    Are you working with a .Rnw file or some extension compatible with knitr? I'd start with looking at knitr's great demos: http://yihui.name/knitr/demo/minimal/
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  3. #3
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: knitr

    I've figured out how to embed R code in a .Rnw file. However, I'm trying to embed code in a .tex file. This link suggests this is possible: https://github.com/yihui/knitr-examp...005-latex.Rtex

    My understanding is that .Rnw files are a 'middle man' between R and latex, and to produce a PDF document, at some point the R code needs to come out of the .Rnw file and into a .tex file. To keep things as simple as possible, I'm trying to embed the R code straight into a .tex file.
    Last edited by Layo909; 12-19-2012 at 02:53 PM.

  4. #4
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: knitr

    Well you could start with an .Rnw file, use knit on it, and then examine the resulting .tex file.
    Morte a tutti i raptors
    001100010010011110100001101101110011

  5. #5
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: knitr

    Problem is, any changes in the .tex file do not show up in the .Rnw file. So if you re-run knit, any changes in the .tex file get over written. I would be happy to work with purely .Rnw files, but .Rnw files do not open in latex editors such as texstudio.

  6. #6
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: knitr

    My suggestion was mainly so that you could see how to actually embed the R code. You mentioned you knew how to do it directly in the Rnw and wanted to know how to embed R directly in tex so I figured you could just see what the resulting tex file looks like and copy that if you want to write directly in tex.

    Are you sure you can't get texstudio to open a Rnw file? I can (on Linux) - you just need to when you choose "Open Files" the box from "Tex files" to "Any files". If that doesn't work you can always just rename the file.Rnw to file.tex

    I'm assuming you're just talking about embedding R code itself - not the output. If you want the output then you really should just stick with .Rnw and use knitr.
    Morte a tutti i raptors
    001100010010011110100001101101110011

  7. #7
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: knitr

    Yes you're right, texstudio can open .Rnw files by changing options in 'Open Files' box - I missed that. That removes quite a few problems.

    So for combining R data analysis with writing reports and articles, would you recommend working purely with .Rnw files? Does working with .Rnw files for producing PDFs introduce any other restrictions at all?

  8. #8
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: knitr

    Not that I can think of. Really a file extension is relatively meaningless and all you're doing is taking a tex file and adding some R code to it with slightly special syntax that gets modified to pure latex after you run knit on it. It's a lot more convenient if you're actually trying to add R code and/or the output of R commands.
    Morte a tutti i raptors
    001100010010011110100001101101110011

  9. #9
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: knitr

    Thanks I've learnt a couple of things from this thread.

    One thing I have just noticed is syntax highlighting is removed when opening an .Rnw file in an ios app called textastic. Might email app owner regarding that, as I often prefer writing on iPad as opposed to Mac.

  10. #10
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: knitr


    You could always just rename it to have a .tex extension. I think knitr will knit a file ending in .tex as if it were a .Rnw file.
    Morte a tutti i raptors
    001100010010011110100001101101110011

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats