When the rmarkdown file is knitted, there are something after the title area, as it is shown in the picture.
How to remove those?
I have set "warning=FALSE, comment=NA" in the r code chunk, but it does not work.
Some of the codes are as follows:
---
title: 'Validness Report by All Facilities'
subtitle: '2017-01-03 to 2017-01-09'
output: pdf_document
geometry: margin=0.5in
classoption: landscape
documentclass: article
---
```{r input, echo = FALSE, results = 'hide', cache = TRUE,
warning=FALSE, comment=FALSE, error=FALSE}
setwd("F:/")
dfDataIn_valid2 <- read.csv("full_valid.csv", stringsAsFactors = FALSE)
```
```{r validness, echo = FALSE, results = 'hide',
cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE}
# Check if required packages are installed. If not, install them.
packages <- c("rJava", "xlsxjars", "xlsx", "lubridate", "dplyr", "lazyeval")
lapply(packages, library, character.only = TRUE)
```
You need chunk option message set to FALSE
```{r, echo = FALSE, results = 'hide', cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE, message=FALSE)}
Also, you don't need to repeat the options for each chunk; you could also just set them in the first one:
```{r}
library(knitr)
opts_chunk$set(echo = FALSE, results = 'hide', cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE, message=FALSE)
```
Related
I am trying to change the font in a table I made in stargazer() to Times New Roman and can't figure out how to do that. I made 4 multiple regression models and put them all into a single table.
stargazer(reg3FModel1, reg3FModel2, reg3FModel3, reg3FModel4,
single.row = TRUE,
digits = 2,
font.size = "large",
type = "text",
model.numbers = FALSE,
dep.var.caption = "Response variables",
omit.stat = c("rsq", "adj.rsq", "ser", "f", "n"),
title = "Regression Analysis with 3-Factor Model")
I'm using GridSearchCV to perform feature selection (SelectKBest) for a linear regression. The results show that 10 features are selected (using .best_params_), but I'm unsure how to display which features this are.
The code is pasted below. I'm using a pipeline because the next models will also need hyperparameter selection. x_train is a dataframe with 12 columns that I cannot share due to data restrictions.
cv_folds = KFold(n_splits=5, shuffle=False)
steps = [('feature_selection', SelectKBest(mutual_info_regression, k=3)), ('regr',
LinearRegression())]
pipe = Pipeline(steps)
search_space = [{'feature_selection__k': [1,2,3,4,5,6,7,8,9,10,11,12]}]
clf = GridSearchCV(pipe, search_space, scoring='neg_mean_squared_error', cv=5, verbose=0)
clf = clf.fit(x_train, y_train)
print(clf.best_params_)
You can access the information about feature_selection step like this:
<GridSearch_model_variable>.best_estimater_.named_steps[<feature_selection_step>]
So, in your case, it would be like this:
print(clf.best_estimator_.named_steps['feature_selection'])
#Output: SelectKBest(k=8, score_func=<function mutual_info_regression at 0x13d37b430>)
Next you can use the get_support function to get the boolean map of the selected features:
print(clf.best_estimator_.named_steps['feature_selection'].get_support())
# Output: array([ True, False, True, False, True, True, True, False, False,
True, True, False, True])
Now provide this map over the original columns:
data_columns = X.columns # List of columns in your dataset
# This is the original list of columns
print(data_columns)
# Output: ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX',
'PTRATIO', 'B', 'LSTAT']
# Now print the select columns
print(data_columns[clf.best_estimator_.named_steps['feature_selection'].get_support()])
# Output: ['CRIM', 'INDUS', 'NOX', 'RM', 'AGE', 'TAX', 'PTRATIO', 'LSTAT']
So you can see out of 13 features only 8 were selected ( as in my data k=4 was the best case)
Here is the full code with boston dataset:
import pandas as pd
from sklearn.datasets import load_boston
from sklearn.model_selection import KFold
from sklearn.feature_selection import SelectKBest, mutual_info_regression
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
boston_dataset = load_boston()
X = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
y = boston_dataset.target
cv_folds = KFold(n_splits=5, shuffle=False)
steps = [('feature_selection', SelectKBest(mutual_info_regression, k=3)),
('regr', LinearRegression())]
pipe = Pipeline(steps)
search_space = [{'feature_selection__k': [1,2,3,4,5,6,7,8,9,10,11,12]}]
clf = GridSearchCV(pipe, search_space, scoring='neg_mean_squared_error', cv=5, verbose=0)
clf = clf.fit(X, y)
print(clf.best_params_)
data_columns = X.columns
selected_features = data_columns[clf.best_estimator_.named_steps['feature_selection'].get_support()]
print(selected_features)
# Output : Index(['CRIM', 'INDUS', 'NOX', 'RM', 'AGE', 'TAX', 'PTRATIO', 'LSTAT'], dtype='object')
References:
https://stackoverflow.com/a/33378303/8160718
https://stackoverflow.com/a/38788087/8160718
I have R markdown document and I want to dynamically create tabs with ggplotly graphics inside them
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig=ggplot(cars)+geom_point(aes(speed, dist))
```
# level 1
## level 2{.tabset .tabset-pills}
```{r echo=FALSE, results='asis'}
for (h in 1:3){
cat("###", h,'{-}', '\n\n')
ggplotly(fig)
cat( '\n\n')
}
```
I understand that it is different from normal ggplot graph and I looked at the solutions here: enter link description here but It did not work for me
Following this post this can be achieved like so:
Edit: Following this post I added two functions to pass the fig.width and fig.height to ggplotly.
Edit 2: Added the code to additionally use plotly::subplots.
---
title: test
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r, echo=FALSE}
# Get the current figure size in pixels:
get_w <- function() {
with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
fig.width*dpi/fig.retina)
}
get_h <- function() {
with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
fig.height*dpi/fig.retina)
}
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
# level 1
## level 2 {.tabset .tabset-pills}
```{r, include=FALSE}
htmltools::tagList(ggplotly(fig))
```
```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
fig <- ggplotly(fig, width = get_w(), height = get_h())
for (h in 1:3) {
cat("###", h, '{-}', '\n\n')
print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
cat( '\n\n')
}
```
found a solution from this link too, this doesn't call for HTML, just markdown.
---
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
## Results {.tabset}
### 1
We show a scatter plot in this section.
```{r}
ggplotly(fig)
```
### 2
We show the data in this tab.
```{r}
ggplotly(fig)
```
Hey I am wondering if there is a way to collapse this bit of code, so its not 12 lines of setting variables.
Current Code:
set isVideo to false
set isTV to false
set isMovie to false
set isRawVideo to false
set isDocumentary to false
set isAudio to false
set isSports to false
set isUnknown to false
set toPrompt to false
set BTNChoice to ""
set keyword to ""
set keywordHit to ""
What I'm hoping for:
set (isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt) to false
set (BTNChoice, keyword, keywordHit) to ""
Or:
isVideo = isTV = isMovie = isRawVideo = isDocumentary = isAudio = isSports = isUnknown = toPrompt = false
BTNChoice = keyword = keywordHit = ""
Let me know if theres a way to reduce this or if I'm stuck with one variable per line.
Ryan
The only one-liner way is
set {isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt} to {false, false, false, false, false, false, false, false, false}
set {BTNChoice, keyword, keywordHit} to {"", "", ""}
Or even a real one-liner
set {isVideo, isTV, isMovie, isRawVideo, isDocumentary, isAudio, isSports, isUnknown, toPrompt, BTNChoice, keyword, keywordHit} to {false, false, false, false, false, false, false, false, false, "", "", ""}
The number of items on the right side must not be less than the number of items on the left side.
I don't quite understand placement of chunks in an Rstudio notebook. I need to know how to make the chunks and results in the markdown appear in the same order as the original document and be able to control which are included using the include= chunk option.
---
title: "Test markdown"
output:
html_notebook: default
---
```{r, setup, include=FALSE}
# set default chunk options:w
knitr::opts_chunk$set(echo = FALSE, include = FALSE, fig.width = 8, collapse = TRUE, warning = FALSE)
```
Note 0
```{r}
# Comment
c <- 92461110
m <- 7056556
```
```{r, include=TRUE}
c
```
Note 1
```{r}
paste("Nothing")
```
Note 2
```{r,include=TRUE}
paste("Something")
```
```{r, include=TRUE}
paste("something else")
```
I should get
Test Markdown
Note 0
92461110
Note 1
Note 2
[1] "Something"
[1] "Something else"
Instead I get
Test markdown
Note 0
Note 1
Note 2
[1] "Nothing"
[1] "Something"