I am using Rstudio Version 1.1.447. on a Dell Latitude E5470 using Windows 10. I am trying to create a presentation for an assignment using R markdown and knittr and ioslides. The resultant presentation has a complete black page instead of the graph. The graph however does display in Rstudio if I run the code on its own outside of R markdown.
Here is my code:
```{r, echo = TRUE, include=FALSE}
library("plotly")
library("ggplot2")
library("datasets")
data("state")
x <- state.area
y <- state.x77[,1]*1000
z <- state.x77[,5]
fac <- state.division
newdf <- as.data.frame(cbind(state.area, state.x77[,1]*1000, state.x77[,5], state.division))
colnames(newdf) <- c("Area", "Population", "Murder", "Division")
p <-plot_ly(newdf,
x="Area",
y="Population",
z="Murder",
type="scatter3d",
mode="markers",
color="Division")
p
```
Try the same but removing the {r, echo = TRUE, include=FALSE} and replace it with just {r}.
Here is a complete working answer.
---
title: "Plotly in Markdown Example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("plotly")
library("ggplot2")
library("datasets")
data("state")
x <- state.area
y <- state.x77[,1]*1000
z <- state.x77[,5]
fac <- state.division
```
## Plot title goes here
Awesome comments about plot output goes here:
```{r}
newdf <- as.data.frame(cbind(state.area, state.x77[,1]*1000, state.x77[,5], state.division))
colnames(newdf) <- c("Area", "Population", "Murder", "Division")
p <-plot_ly(newdf,
x="Area",
y="Population",
z="Murder",
type="scatter3d",
mode="markers",
color="Division")
p
```
Related
I am trying to knit a document using plotly to generate the images and rmarkdown. Below is a basic example:
---
title: "Test"
author: "Me"
date: "`r Sys.Date()`"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
library(webshot)
library(webshot2)
```
## R Markdown
This is an R Markdown document. It is a test.
```{r test, echo=FALSE}
x <- seq(-1, 1, 0.1)
y <- dnorm(x, 0, 0.5)
pl1 <- plot_ly(x=x, y=y, type='scatter', mode='lines') %>%
config(mathjax='cdn') %>%
plotly::layout(
showlegend=TRUE,
legend=list(itemsizing='trace', orientation='h',
xanchor='center', x=0.5), margin=list(b=80, l=45, r=30, t=80),
title=list(text=
TeX("M \\text{ updates for } N=n=1 \\text{ for } P(update) = \\gamma_{i}.")))
# pl1
htmlwidgets::saveWidget(widget = pl1, file = "pl1.html")
webshot(url = "pl1.html", file = "pl1.pdf")
The problem is that when I knit the document, the htmlwidgets function generates the correct rendering of Latex (top image), but the webshot function does not (bottom image). Also, I have tried using fig.show='hold' but this doesn't help either.
I have also tried using webshot2 and, when using webshot I have installed webshot::install_phantomjs()
I am trying to build a leaflet map in R markdown. But I get the error mentioned in the title. Here is my code:
---
title: "Types of buildings in Luxembourg"
author: "Elide Nicotra"
date: '2022-05-19'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set
library(leaflet)
library(dplyr)
library(sf)
```
## R Markdown file
## Points map
```{r}
lux_lng <- 6.1319
lux_lat <- 49.6116
points_lng <- lux_lng + rnorm(26)/10
points_lat <- lux_lat + rnorm(26)/10
popup_content <- sample(LETTERS, 26)
leaflet(width='100%') %>%
setView(lng=lux_lng,lat=lux_lat, zoom=9) %>%
addTiles() %>%
addMarkers(lng=points_lng, lat=points_lat, popup=popup_content)
```
## GeoJSON and loading data
```{r}
Buildings <- rgdal::readOGR("BuildingsLux.geojson", verbose = FALSE)
data <- data.frame(Buildings#data)
```
Error: unexpected ',' in "Schools <- (data$name == "schools","
## Unique names and types of buildings
```{r}
Names <- unique(data$name)
Types <- unique(data$type)
```
## Categorical classification
```{r}
catpal <- colorFactor(topo.colors(length(Types)), Types)
Buildingscol <- catpal(data$type)
```
## continuous classification
## Buildingscol <- contpal(as.integer(as.factor(NullApartment$name)))
```{r}
leaflet(data = Types , width='100%') %>%
setView(lng=points_lng, lat=points_lat, zoom=9) %>%
addTiles() %>%
addPolygons(color=Buildingscol, popup = data$type) %>%
addLegend(position = "bottomright", labels=Types, col = catpal(Types))
```
Error in polygonData.default(data) :
Don't know how to get path data from object of class character
Does anyone see the problem please?
I am trying to create a table in RMarkdown that looks similar to the following example:
---
title: "Example"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r cars, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(Hmisc)
latex(mtcars, file = "", cgroup = c("mpg", "cyl"), n.cgroup = c(1,10))
```
I would like to group columns 2 through 10. Any ideas on how I can accomplish this with the Hmisc package or any other R package?
I think just using a blank header name for the first column gives you what you want:
latex(mtcars, file = "", cgroup = c("", "cyl"), n.cgroup = c(1,10))
Result:
Using my package:
library(huxtable)
hux_cars <- as_hux(mtcars, add_colnames = TRUE)
hux_cars <- insert_row(hux_cars, c('mtcars', 'cyl', rep('', 9)))
colspan(hux_cars)[1, 2] <- 10
align(hux_cars)[1, 2] <- 'centre'
bold(hux_cars)[1, ] <- TRUE
position(hux_cars) <- 'left'
quick_pdf(hux_cars)
Which produces:
Have published in rPubs the following codes of rPresentation.
Some of the charts are not displaying properly. Just wondering what I am missing ?Guidance in this regard will be helpful.
Thanks
Link of presentation : http://rpubs.com/shan/Presentation3
Charts
========================================================
### Lets display some charts.
```{r results = 'asis', comment = NA}
library(rCharts)
library(ggplot2)
n1 <- nPlot(carat ~ cut , group = "color", data = diamonds[1:40,], type = "multiBarChart")
n1
```
Plot
=============================================================
### Plotting Chart from economics dataset
```{r results = 'asis', comment = NA}
library(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1
```
Plot
============================================================
### Lets plot some Geographical Data
```{r gvisMergeExample, results='asis', echo=FALSE}
library(googleVis)
Geo <- gvisGeoChart(Exports, locationvar='Country', colorvar='Profit',
options=list(height=300, width=350))
Tbl <- gvisTable(Exports, options=list(height=300, width=200))
plot(gvisMerge(Geo, Tbl, horizontal=TRUE))
```
Plot
=========================================
### Lets plot Fruit vs Year
```{r MotionChartExample, results='asis', tidy=FALSE}
library(googleVis)
M <- gvisMotionChart(Fruits, 'Fruit', 'Year', options=list(width=400, height=350))
plot(M)
```
I have made a rpresenter file. The code is displayed below. When I published in rPubs, slides with words are displayed. But the motion charts and other charts are not displayed, showing:
AccessDeniedAccess Denied573371FF628EBE0CmrHb5GPTc+1Ul1K15psqFpJ/f6dovc+frwQougDVtROA6ZQajWyye0Jr9CvH
I had gone through the RStudio documentation. I was wondering how to display the charts properly. Thanks in anticipation.
Presentation On Datasets
========================================================
author: Harry
date: 8/27/2016
autosize: true
Selecting The Data
========================================================
### We will select the diamonds dataset available in package 'ggplot2'.
Lets look at the data.
```{r}
library(ggplot2)
head(diamonds)
```
Summary Of Data
========================================================
### Lets view the summary of data
```{r}
library(ggplot2)
summary(diamonds)
```
Charts
========================================================
### Lets display some charts.
```{r results = 'asis', comment = NA}
library(rCharts)
library(ggplot2)
n1 <- nPlot(carat ~ cut , group = "color", data = diamonds[1:40,], type = "multiBarChart")
n1
```
Plot
=============================================================
### Plotting Chart from economics dataset
```{r results = 'asis', comment = NA}
library(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1
```
Plot
============================================================
### Lets plot some Geographical Data
```{r gvisMergeExample, results='asis', echo=FALSE}
library(googleVis)
Geo <- gvisGeoChart(Exports, locationvar='Country', colorvar='Profit',
options=list(height=300, width=350))
Tbl <- gvisTable(Exports, options=list(height=300, width=200))
plot(gvisMerge(Geo, Tbl, horizontal=TRUE))
```
Plot
=========================================
### Lets plot Fruit vs Year
```{r MotionChartExample, results='asis', tidy=FALSE}
library(googleVis)
M <- gvisMotionChart(Fruits, 'Fruit', 'Year', options=list(width=400, height=350))
plot(M)
```