I have been working with R for a few months now and am finally ready to start publishing some of my work. The only issue is that much of my analysis centers around presenting visualizations I generated with ggplot2 and mapview.
Using Rstudio, I choose the "knit to html" button, but notice that none of my visualizations actually appear in the html document.
I have a feeling this has to do with my YAML header, but I am having a difficult time finding a straight answer on what I need to add in order to automatically generate my plots.
My header is as follows:
title: "###"
author: "###"
date: "###"
output:
html_document:
toc: yes
toc_depth: 2
toc_float: yes
pdf_document:
toc: yes
toc_depth: '2'
knitr::opts_chunk$set(echo = TRUE)
Here's an example of the code chunk in markdown where I would like the visualization to appear directly below the code:
viz_trips_by_weekday <- ggplot(data = trips_by_weekday)+
geom_col(mapping = aes(x = weekday,
y = ride_count,
fill = customer_type),
position = 'dodge')+
labs(title = "Annual Ride Count By Weekday",
x = "Day of Week",
y = "Number of Rides",
)
print(viz_trips_by_weekday)
Any help would be greatly appreciated!
check these options in the code chunk
library(ggplot2)
Create a chunk in the beginning of your document that holds all the libraries you are using in the code. Like here, where you use ggplot2 package.
Please help regarding one issue that I have with the output I am getting with R Markdown-flexdashboard. I suppose I am doing something wrong.
I am using the following code in Rmarkdown:
title: "Economy and Population in Croatia 2000-2017"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
library(flexdashboard)
library(tidyverse)
library(plotly)
library(ggrepel)
library(reshape2)
library(viridis)
library(DT)
load("my_work_space.RData")
Page 1 - National level
Column {data-width=600, .tabset}
Croatia 2000-2017: Population and Economy
g1
Croatia by County: Population 2018
ggplotly(g4)
Croatia by County: Economy 2017
ggplotly(g5)
Column {data-width=400}
Croatia: Economy and Demography by County
datatable(data = Datatable_Demo_Econs,
rownames=FALSE,
options=list(pageLength=3))
GDP of Regions 2000-2017
ggplotly(g2, tooltip = c("Region", "Year"))
Here are the pics of the code in the Rmarkdown code-pic1, code-pic2
However, I only manage to create the first plotly plot (g1) in the flexdashboard. Other graphs are produced via ggplotly (maybe that is the problem?). It looks like some kind of bug for ggplotly graphs in flexdash Rmarkdown script, or I am doing something wrong.
I am attaching the picture to see the almost empty flexdashboard.
Empty flexdashboard
It is confusing for me that exactly the same code produced the correct and full flexdashboard if I am using RStudio Cloud. I am attaching two pictures of correct and full dashboard produced in RStudio Cloud:
1. pic: correct flexdash
2. pic: correct flexdash
Thanks
I am working on the following code with some latex (making a reproducible report document in R notebook, but for some reason my table is put above my text and equation. How do I fix this? enter image description here
title: "Empirical Research in Finance"
subtitle: "**Hypothesis tests and simulation**"
author: Faes E.
fontsize: 12pt
date: '*January 25, 2021*'
output:
pdf_document: default
html_document:
df_print: paged
header-includes:
- \usepackage{amsmath} \usepackage{color}
- \usepackage{titling}
- \pretitle{\begin{center}
\includegraphics[width=4in,height=4in]{AMSLOGO.jpg}\LARGE\\}
- \posttitle{\end{center}}
\thispagestyle{empty}
# copy the R logo to the current directory
file.copy(file.path(R.home("doc"), "html", "AMSLOGO.jpg"), '.')
\newpage
\newpage
An examination of the first and second moments of the sample skewness is been done in table 1. There can be seen that the mean goes to zero for larger samples. For the variance, the asymptotic variance is being reached. We know that it should render the following values (equation 2) as we know that skewness is asympotitcally distributed with mean 0 and variance 6/n (where n is in this case the sample size).
\begin{equation}
\tag{2}
skewness \stackrel{a}{\sim}N(0,\dfrac{6}{n})
\end{equation}
```{r echo=FALSE}
kable(des(mom=S), digits= 3, caption = "Statistics for skewness") %>%
kable_classic(full_width = F) %>%
footnote(number = c("n is the sample size","std is the standard deviation"))
```
Like user2554330 answered correctly and worked for me:
You can use kable(..., format = "latex", position = "h!") to give a very strong suggestion to leave it in place. If LaTeX thinks that's a bad idea, it still won't do it.
I’d like to synchronize the interactivity of two R plots: a plotly plot and a leaflet map. The plotly plot graphs a time series for a specific location in the leaflet map. In other words, I’d like to select a location (or group of locations) in a map and see the corresponding selection in the ploty plot and vice versa select a time series in the plotly plot and see the corresponding marker in the map highlighted. So, in both directions.
The attached file (dataset1) is a R list that contains all data. Each element of the list is a dataset (time series) for each location. The linked variable for both plots is “Codi.Estació”.
I’ve tried the crosstalk package but the authors warn that “Crosstalk currently only works for linked brushing and filtering of views that show individual data points, not aggregate or summary views”. I’m not interested in an individual data point but a whole time series.
Could anyone help me how to handle that? Tips, examples, other packages (instead of leaflet) are welcome?
Thank you very much and have a nice summer,
Download the dataset: https://drive.google.com/file/d/1PkPm1ObcEer8Lne5vJMZR6MdFTONRSvY/view?usp=sharing
Download HTML output: https://drive.google.com/open?id=1YHko4V-iAUZqZr3wNC7zGEdrMhjmykSA
The code in R Markdown (*.Rmd) (to run in Rstudio):
---
title: "Piezometers La Bisbal del Penedès "
author: "J.M. Campanera"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
---
```{r setup, include = FALSE}
library(plotly)
library(leaflet)
library(flexdashboard)
load("dataset1.RData")
```
Column {data-width=700}
-----------------------------------------------------------------------
### Water depth
```{r echo=FALSE}
# Plot 1
p<-plot_ly()
for (i in 1:length(dataset1)) {
p<-add_trace(p,name=dataset1[[i]]$Codi.Estació[1],x=dataset1[[i]]$Data,y=dataset1[[i]]$Valor,mode = 'scatter',type="scatter")
}
p
```
Column {data-width=300}
-----------------------------------------------------------------------
### well locations
```{r echo=FALSE}
m <- leaflet()
m<-addTiles(m)
for (i in 1:length(dataset1)) {
m<-addCircleMarkers(m,lng=dataset1[[i]]$Longitud[1], lat=dataset1[[i]]$Latitud[1],label=dataset1[[i]]$Codi.Estació[1],labelOptions = labelOptions(noHide = T, textOnly = TRUE),popup=as.character(dataset1[[i]]$Fondària.Pou..m.[1]))
}
m
```
Finally I develop a solution:
1) Interactivity in Plotly plot and highlighting in the leaflet map:
I used the funtion "event_data(event=c("plotly_click"))" and I get the inspiration from
https://plot.ly/r/shinyapp-plotly-events/
2) Interactivity in Leaflet map and highlighting in the plotly plot:
I used the funtion "input$map_marker_click" and I get the inspiration from here: https://rstudio.github.io/leaflet/shiny.html
Thank you,
I wasn't able to find much pertinent to this on Stack-Overflow, or the web.
I'm getting this error:
> library(knitr)
> knit2html("pa1_template.rmd")
Error in knit2html("pa1_template.rmd") :
It seems you should call rmarkdown::render() instead of knitr::knit2html() because pa1_template.rmd appears to be an R Markdown v2 document.
I just ran it with rmarkdown::render(), and it created the HTML file. However, my assignment wants me to run it through knit2html() and create an md file.
When I run the Rmd file through the RStudio "Knit HTML" menu option, it creates the HTML file fine.
Any pointers appreciated.
Here is the content of the rmd file:
## Loading and preprocessing the data
Read the data file in.
```{r readfile}
steps<-read.csv("activity.csv",header=TRUE, sep=",")
steps_good<-subset(steps, !is.na(steps))
```
Sum the number of steps per day
```{r summarize/day}
steps_day<-aggregate(steps~date, data=steps_good, sum)
```
Create a histogram of the results
```{r histogram}
hist(steps_day$steps, main="Frequency of Steps/day", xlab="Steps/Day", border="blue", col="orange")
```
# What is the mean total number of steps taken per day?
Calculate the mean of the steps per day
```{r means_steps/day}
mean_steps<-mean(steps_day$steps)
mean_steps
```
Calculate the median of the steps per day
```{r median_steps/day}
med_steps<-median(steps_day$steps)
med_steps
```
#What is the average daily activity pattern?
Get the average steps per 5 minute interval
```{r avg_5_min}
step_5min<-aggregate(steps~interval, data=steps_good, mean)
```
Plot steps against time interval, averaged across all days
```{r plot_interval}
plot(step_5min$interval,step_5min$steps, type="l", main="steps per time interval",ylab="Steps",xlab="Interval")
```
On average, which interval during the day has the most steps.
```{r max_interval}
step_5min$interval[which.max(step_5min$steps)]
```
#Imputing missing values
How many NAs are there in the original table?
```{r NAs}
steps_na<-which(is.na(steps))
length(steps_na)
```
Merge 5 minute interval with original steps table
```{r merge}
steps_filled<-merge(steps, step_5min,by="interval")
```
Replace NA values with mean of steps values for that time interval
```{r replace_na}
steps_na<-which(is.na(steps_filled$steps.x))
steps_filled$steps.x[steps_na]<-steps_filled$steps.y[steps_na]
```
Create a histogram of the results
```{r new_hist}
steps_day_new<-aggregate(steps.x~date, data=steps_filled, sum)
hist(steps_day_new$steps.x, main="Frequency of Steps/day", xlab="Steps/Day", border="blue", col="orange")
```
It looks like the imputing of NA values increases the middle bar (mean/median) height, but other bars seem unchanged.
Calculate the new mean of the steps per day
```{r new_means_steps/day}
mean_steps<-mean(steps_day_new$steps.x)
mean_steps
```
Calculate the new median of the steps per day
```{r new_median_steps/day}
med_steps<-median(steps_day_new$steps.x)
med_steps
```
It looks like the mean did not change, but the median took on the value of the mean, now that some non-integer values were plugged in.
#Are there differences in activity patterns between weekdays and weekends?
Regenerate steps_filled, and flag whether a date is a weekend or a weekday.
Convert resulting column to factor.
```{r fill_weekdays}
steps_filled<-merge(steps, step_5min,by="interval")
steps_filled$steps.x[steps_na]<-steps_filled$steps.y[steps_na]
steps_filled<-cbind(steps_filled, wkday=weekdays(as.Date(steps_filled$date)))
steps_filled<-cbind(steps_filled, day_type="", stringsAsFactors=FALSE)
for(i in 1:nrow(steps_filled)){
if(steps_filled$wkday[i] %in% c("Saturday","Sunday"))
steps_filled$day_type[i]="Weekend"
else
steps_filled$day_type[i]="Weekday"
}
steps_filled$day_type<-as.factor(steps_filled$day_type)
```
Get average steps per interval and day_type
```{r plot_interva_day_type}
steps_interval_day<-aggregate(steps_filled$steps.x,by=list(steps_filled$interval,steps_filled$day_type),mean)
```
Plot the weekend and weekday results in a panel plot.
```{r day_type_plot}
weekday_intervals<-subset(steps_interval_day, steps_interval_day$Group.2=="Weekday",select=c("Group.1","x"))
weekend_intervals<-subset(steps_interval_day, steps_interval_day$Group.2=="Weekend",select=c("Group.1","x"))
par(mfrow=c(1,2))
plot(weekday_intervals$Group.1,weekday_intervals$x,type="l",xlim=c(0,2400), ylim=c(0,225),main="Weekdays",xlab="Intervals",ylab="Mean Steps/day")
plot(weekend_intervals$Group.1,weekend_intervals$x,type="l",xlim=c(0,2400), ylim=c(0,225),main="Weekends",xlab="Intervals",ylab="")
In RStudio, you can add keep_md: true in your YAML header:
---
title: "Untitled"
output:
html_document:
keep_md: true
---
With this option, you get both HTML and md files.
It worked with knit(), instead of knit2html()
try this:
setwd("working_directory")
library(knitr)
knit("PA1_template.Rmd", output = NULL)
adding output=NULL" was key for me.
Good luck!