Knitr: opts_chunk$set() not working in Rscript command
I think you need to add
library("knitr")
to the chunk (you might want to set message=FALSE
in the chunk options for that chunk).
The problem is that when you do
Rscript -e 'knitr::knit("myfile.Rmd")'
you're not actually attaching the knitr
package, which means it isn't in the search path for functions, which means that R can't find the opts_chunk
object.
- Using
knitr::opts_chunk
might work too ... - as you suggested, so does
Rscript -e 'library("knitr"); knit("myfile.Rmd")'
When you click the button in RStudio, RStudio automatically loads knitr
in the environment in which it runs knit()
.
knitr::opts_chunk$set(message=FALSE) does not work
The changes made with knitr::opts_chunk$set
will be activated starting th next chunk. See my example above. So it is recommended to setup your setting in the first chunk of your RMD file and then calculate/manipulate ... your data. See the official documentation here under Details
.
```{r}
library(tidyverse)
knitr::opts_chunk$set(message=FALSE)
strMessage <- if_else(knitr::opts_chunk$get("message"),"TRUE","FALSE")
message(c("message is set to ",strMessage))
```
```{r}
strMessage <- if_else(knitr::opts_chunk$get("message"),"TRUE","FALSE")
message(c("message is set to ",strMessage))
```
Setting work directory in knitr using opts_chunk$set(root.dir = ...) doesn't work
It is knitr::opts_knit
instead of knitr::opts_chunk
.
Is there a global command line knit option as eval=FALSE for all chunks?
You do not need to edit the .Rmd
file. You can just run
knitr::opts_chunk$set(eval = FALSE)
before running knitr::knit()
; knitr will respect the global chunk options you set before calling knitr::knit()
.
Rmarkdown does not follow same paths as R script and console commands
Try something that looks like this as I am not sure of the nature of your `R Markdown.
test <- readRDS(here::here("data/test_data.rds"))
The bottom line is to use the here
function from the here
package.
Related Topics
R Shiny Dt - Edit Values in Table with Reactive
Renaming and Hiding an Exported Rcpp Function in an R Package
How to Print a Variable Inside a for Loop to the Console in Real Time as the Loop Is Running
Replacing Negative Values in a Model (System of Odes) with Zero
Overlapping the Predicted Time Series on the Original Series in R
R: Calculate Means for Subset of a Group
Draw Histograms Per Row Over Multiple Columns in R
Dplyr - Mutate Dynamically Named Variables Using Other Dynamically Named Variables
Control Font Thickness Without Changing Font Size
How to Add Legend to Geom_Smooth in Ggplot in R
The Rolling Regression in R Using Roll Apply
Extent of Boundary of Text in R Plot
Ggplot2: Creating Themed Title, Subtitle with Cowplot
How to Calculate a Table of Pairwise Counts from Long-Form Data Frame
Group Rows in Data Frame Based on Time Difference Between Consecutive Rows