Tim Wilcox
Tim Wilcox

Reputation: 1331

Having issues with R markdown printing background data

Below is the code that is presenting the problem.

I can't seem to find a way to have it not print the source data before the table, house. Is this a code chunk option issue such as not having include = False in the code? To produce, you would create a new R markdown document and then put the code below in the first two code chunks (usually gray background color).

  ```{r setup, include=FALSE}
  knitr::opts_chunk$set(echo = TRUE)
  pkgs <- c("tidyverse","tidyquant","RODBC", "RODBCDBI",
      "spdplyr","rgdal","readxl","Rcpp",
      "RcppRoll", "ggforce","scales", "DBI","ggthemes", 
 "cowplot","gt","data.table","gridExtra","ggrepel","htmltab","tigris","tidycensus")


  loader <- function(x){
  for( i in x ){
  if(!require(i, character.only = TRUE) ){
  install.packages(i, dependencies = TRUE)
  require(i, character.only = TRUE)
  }
  }
  }

 loader(pkgs)
 #lapply(pkgs,library,character.only = TRUE)

 ```

## Housing Price

 ```{r, echo=FALSE,comment=FALSE,message=FALSE}
 # load data
 dt <- fread("http://www.freddiemac.com/fmac-resources/research/docs/fmhpi_master_file.csv")

 dt[,hpa_yoy:=Index_SA/shift(Index_SA,12)-1,.(GEO_Type,GEO_Code,GEO_Name)]

dt_nv<- dt[GEO_Name=="NV" & Year>2010,]
head(dt_nv)

dcast(dt_nv,Year~Month,value.var="hpa_yoy")
dt_nv[,mname:=factor(month.abb[Month],levels=month.abb)]
dt_tab <- dcast(dt_nv,Year~mname,value.var="hpa_yoy")

house<- gt(dt_tab) %>%
opt_row_striping()%>%
fmt_percent(
columns = c(month.abb),
decimals = 1,
use_seps = FALSE
) %>%
 tab_header(title="Nevada House Price Growth",
         subtitle="12-month % change in Freddie Mac House Price Index" )%>%
 tab_options(
 data_row.padding = px(9),
 row_group.padding = px(9),
 table.font.size = px(11),
 source_notes.font.size = px(11),
 source_notes.padding = px(1))

 ```


 ## Including Plots

Housing Prices

```{r, echo=FALSE, message=FALSE}
house
```

Upvotes: 0

Views: 75

Answers (1)

bash1000
bash1000

Reputation: 341

Really unclear what the question is and I am not installing all of these packages, but from what I can tell, you need to put include=FALSE into the header of the second chunk.

The lines: dt[,hpa_yoy:=Index_SA/shift(Index_SA,12)-1,.(GEO_Type,GEO_Code,GEO_Name)] and head(dt_nv) that are causing problems because they are not assigned to any object, R is just printing the table described by these line, I think.

If you are desperate, make this code eval=FALSE and then copy the code into another chunk with include=FALSE so the first block is there for show, while the actual block is evaluated without being shown in the document.

Upvotes: 1

Related Questions