1

I have to turn in a stats assignment in R Markdown pdf format. I was able to successfully turn my answers into an R Markdown file but some lines of code didn't fit onto the page.

The type of line I have to break looks like the following:

    ```{r}
    gayssmw1 <- subset(gay, study == 1 & wave == 1 & treatment == "Same-Sex Marriage  Script by Gay Canvasser")
    mean(gayssmw1$ssm)
    gayssmstraightw1 <- subset(gay, study == 1 & wave == 1 & treatment == "Same-Sex  Marriage Script by Straight Canvasser")
    mean(gayssmstraightw1$ssm)
    gayssmcontrolw1 <- subset(gay, study == 1 & wave == 1 & treatment == "No Contact")
    mean(gayssmcontrolw1$ssm)
    mean(gayssmcontrolw1$ssm) - mean(gayssmw1$ssm)
    mean(gayssmcontrolw1$ssm) - mean(gayssmstraightw1$ssm)
    ```

I turned back to R Studio and started inserting line breaks where needed first with shift + enter and then tried something I found on the R Markdown Basics website, the double space.

Now the lines fit into the page, but all results that are concerned by this line break in code now are displayed as:

    > gayssmw1 <- subset(gay, study == 1 & wave == 1 & treatment == "Same-Sex 
    Marriage Script by Gay Canvasser")
    > mean(gayssmw1$ssm)

    [1] NaN

etc.

What should I do ?

egonmasama
  • 11
  • 1
  • Put line breaks in places where they are syntactically valid. (I.e., *not* in the middle of strings). – Gregor Thomas Sep 26 '16 at 23:50
  • RStudio will probably do this well for you if you highlight the code in a code chunk and then choose `Code > Reformat Code` from the menu. – Gregor Thomas Sep 26 '16 at 23:51
  • 6
    Alternatively, you can take advantage of the `tidy` feature in the code chunks. More on this here: http://stackoverflow.com/questions/26210656/in-r-markdown-in-rstudio-how-can-i-prevent-the-source-code-from-running-off-a-p – Chrisss Sep 26 '16 at 23:52

0 Answers0