MNU
MNU

Reputation: 764

How to keep R code inside page in R markdown output?

I am trying to run the following code in R markdown only to print the code. I do not want to print the results from R code. Some of the lines in code are really large. When I run, it worked perfectly, but some of the lines go outside of the page? Here is the R markdown code


title: R code
output: 
  pdf_document:
    toc: true
    number_sections: true
documentclass: article
classoption: 
  - portrait
  - a4paper
---



```{r,eval=F,echo=TRUE}
hrs_data_GS$rSLn==i,\]%*%beta\[k,\])^2/2*sigma2)+
                          sum(z_ij_str\[i\]*xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\])-
                          0.5*sum(w_ij\[hrs_data_GS$rSLn==i\]*(xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\])^2)+
                          ( (lamda/sigma2)*sum(hrs_data_GS$y_ij\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i\]-
                                                 xdata\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i,\]%*%beta\[k,\])
                            +sum_z_ij_str\[i\]- sum(w_ij\[hrs_data_GS$rSLn==i\]*
                                                    xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\]))^2*
                          2*((n_i\[i\]*lamda^2/sigma2)+sum_w_ij_j\[i\]+1/sig2.theta)^-1)+
          (log(pi_cls\[i,k\]))  #(log(pi_k\[k\]))  
      }
    }
    nume_prod<-nume_prod-apply(nume_prod,1,max) #it works 
    nume_prod <- exp(nume_prod)
    prob_cls_rsum=apply(nume_prod, 1, sum) 
    prob_cls=nume_prod/ prob_cls_rsum 
    #cls=miscF::rMultinom(p=cbind(prob_cls)) #miscF is not available at CRC
    cls=Hmisc::rMultinom(p=cbind(prob_cls),1)  
    #Update sum_y.ij_j and sum_wxa_ij_j before updating the theta
    #these values involve 'cls'
    #******
    for (i in 1:m){ 
      #1**********
      sum_y.ij_j\[i\]<-(lamda/sigma2)*sum((hrs_data_GS$y_ij\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i\]-
                                           xdata\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i,\]%*%beta\[cls\[i\],\])
      ) #conditioning on z_ij==1
      #2******
      sum_wxa_ij_j\[i\]<-sum((w_ij\[hrs_data_GS$rSLn==i\]*
                              xdata\[hrs_data_GS$rSLn==i,\]%*%
                              alpha\[cls\[i\],\])
      )
    }
    #Update theta_i
    theta_i=rnorm(m, #number of thetas 
                  (sum_z_ij_str-sum_wxa_ij_j+sum_y.ij_j)*
                    (((1/sig2.theta)+sum_w_ij_j+((lamda^2/sigma2)*n_i_z1))^-1), #mean  
                  sqrt((((1/sig2.theta)+sum_w_ij_j+((lamda^2/sigma2)*n_i_z1))^-1)) #std 


```

Here is the output R markdown output

I would like to keep all the codes inside the page. Is there any way I do it for all chunk?

Upvotes: 5

Views: 1108

Answers (2)

Daniel_j_iii
Daniel_j_iii

Reputation: 3242

add ``` to the end of the document, for some reason I couldn't get them to display. Different document format, but you only need to print to PDF from a browser

---

title: R code

output: html_document

---




```{r,,eval=F,echo=TRUE, fig.width=40}
hrs_data_GS$rSLn==i,\]%*%beta\[k,\])^2/2*sigma2)+
                          </br> sum(z_ij_str\[i\]*xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\])-
                          0.5*sum(w_ij\[hrs_data_GS$rSLn==i\]*(xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\])^2)+
                       ( (lamda/sigma2)*sum(hrs_data_GS$y_ij\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i\]-
                                              xdata\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i,\]%*%beta\[k,\])
                            +sum_z_ij_str\[i\]- sum(w_ij\[hrs_data_GS$rSLn==i\]*
                                                    xdata\[hrs_data_GS$rSLn==i,\]%*%alpha\[k,\]))^2*
                          2*((n_i\[i\]*lamda^2/sigma2)+sum_w_ij_j\[i\]+1/sig2.theta)^-1)+
          (log(pi_cls\[i,k\]))  #(log(pi_k\[k\]))  
      }
    }
    nume_prod<-nume_prod-apply(nume_prod,1,max) #it works 
    nume_prod <- exp(nume_prod)
    prob_cls_rsum=apply(nume_prod, 1, sum) 
    prob_cls=nume_prod/ prob_cls_rsum 
    #cls=miscF::rMultinom(p=cbind(prob_cls)) #miscF is not available at CRC
    cls=Hmisc::rMultinom(p=cbind(prob_cls),1)  
    #Update sum_y.ij_j and sum_wxa_ij_j before updating the theta
    #these values involve 'cls'
    #******
    for (i in 1:m){ 
      #1**********
      sum_y.ij_j\[i\]<-(lamda/sigma2)*sum((hrs_data_GS$y_ij\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i\]-
    xdata\[hrs_data_GS$z_ij==1 & hrs_data_GS$rSLn==i,\]%*%beta\[cls\[i\],\])
      ) #conditioning on z_ij==1
      #2******
      sum_wxa_ij_j\[i\]<-sum((w_ij\[hrs_data_GS$rSLn==i\]*
                              xdata\[hrs_data_GS$rSLn==i,\]%*%
                              alpha\[cls\[i\],\])
      )
    }
    #Update theta_i
    theta_i=rnorm(m, #number of thetas 
                  (sum_z_ij_str-sum_wxa_ij_j+sum_y.ij_j)*
                    (((1/sig2.theta)+sum_w_ij_j+((lamda^2/sigma2)*n_i_z1))^-1), #mean  
                  sqrt((((1/sig2.theta)+sum_w_ij_j+((lamda^2/sigma2)*n_i_z1))^-1)) #std

Upvotes: 0

Matt
Matt

Reputation: 7385

I think you're looking for width.cutoff:

You can change your chunk options at the top:

library(formatR)
```{r, tidy=TRUE, tidy.opts=list(width.cutoff=60), eval = FALSE, echo = TRUE}

Upvotes: 2

Related Questions