43 lines
		
	
	
		
			908 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			43 lines
		
	
	
		
			908 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
|   | # Evaluate and echo different lines | ||
|  |   | ||
|  | For demonstration purposes, we may want to show some source code in the output, | ||
|  | but really evaluate different code in the background. | ||
|  | 
 | ||
|  | ```{r} | ||
|  | hook_source <- knit_hooks$get('source') | ||
|  | knit_hooks$set(source = function(x, options) { | ||
|  |     res <- hook_source(x, options) | ||
|  |     gsub("(^|\n)#'#' ", '\\1', res) | ||
|  | }) | ||
|  | ``` | ||
|  | The trick is to mask the source code in special comments (e.g. `#'#'`), and | ||
|  | remove the comment markers later. Of course, you have to guarantee these markers | ||
|  | are unique. | ||
|  | 
 | ||
|  | ```{r test, echo=-3} | ||
|  | x <- 2 | ||
|  | ## 1/sqrt(2 * pi) * exp(-x^2/2) | ||
|  | dnorm(x) | ||
|  | ```     | ||
|  | 
 | ||
|  | ```{r sdf} | ||
|  | 
 | ||
|  | 
 | ||
|  | dfdfd | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | ``` | ||
|  | 
 | ||
|  | We used `echo=-3` to remove the 3rd expression from the source code, and | ||
|  | `gsub()` to strip `#'#'` off. | ||
|  | 
 | ||
|  | This is completely hack. Use with care. | ||
|  | 
 | ||
|  | Example code: | ||
|  | 
 | ||
|  | ```{r} | ||
|  | ## Example code: | ||
|  | paggregate(argent[, 2:5], by = list(Transect = argent$Transect, Season = argent$Season), FUN = sum) | ||
|  | ``` |