leaflet map not rendering in html document using rmarkdown - leaflet

Here is my code
---
date: "7 December 2018"
output: html_document
---
## 7 December 2018
```{r, echo=FALSE}
library(leaflet)
library(jsonlite)
citibike <- fromJSON("http://citibikenyc.com/stations/json")
stations <- citibike$stationBeanList
m = leaflet(stations) %>% addTiles(urlTemplate = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>% addCircles(lat = ~latitude, lng = ~longitude, weight = 5, radius = ~availableBikes, popup = paste("Station:", stations$stationName, "<br>", stations$availableBikes, "available bikes", "<br>", stations$availableDocks, "available docks")) %>% addControl("Available Bikes in NYC on 12/07/2018", position = "topright")
m
```
Using Knit, the html document created only shows the date but not the map. The map is created without any problem when using that code in the console of RStudio.
I have downloaded the latest version of leaflet from github. I use Windows 10.
platform x86_64-w64-mingw32 arch x86_64
os mingw32 system x86_64,
mingw32 status major
3 minor 5.1
year 2018 month 07
day 02 svn rev 74947
language R version.string R version
3.5.1 (2018-07-02) nickname Feather Spray

Related

How to convert geom_point(aes()) + geom_vline(aes()) to Plotly?

I found this tutorial online that helps convert ggplot2's geom_abline() to a Plotly graph: https://plotly.com/ggplot2/geom_abline/
It looks like we can simply make such conversion using ggplotly():
library(ggplot2)
library(plotly)
p <- ggplot(data, aes(x=x_val, y=y_val, colour=color_val)) +
geom_point() +
geom_vline(aes(xintercept=xintercept_val), colour=color_val)
ggplotly(p)
However, I cannot convert my ggplot2 graph into a Plotly graph with the following code:
# notice that both my x_val and xintercept_val are dates.
# here's my ggplot2 code:
gg <- ggplot(data) +
geom_point(aes(
x_val,
y_val,
color=color_val,
shape=shape_val
)) +
geom_vline(aes(
xintercept=xintercept_val,
color=color_val
))
ggplotly(gg)
Here's a screenshot of my ggplot2 graph (I cropped out the legends):
Here's a screenshot of my Plotly graph using ggplotly(gg):
Not sure why the vertical lines aren't showing up in Plotly.
Looks like you stumbled over a bug in ggplotly (perhaps you should raise an issue on github). The issue is that ggplotly internally converts the dates to numerics (same with categorical variables). However, inspecting the JSON representation via plotly_json shows that the xintercepts in geom_vline are not converted. That's why they don't show up. However, as a workaround you can make the conversion manually using as.numeric().
As you provided no data I use a simple example dataset from the plotly website to which I added some dates. Try this:
dat <- read.table(header=TRUE, text='
cond xval yval
control 11.5 10.8
control 9.3 12.9
control 8.0 9.9
control 11.5 10.1
control 8.6 8.3
control 9.9 9.5
control 8.8 8.7
control 11.7 10.1
control 9.7 9.3
control 9.8 12.0
treatment 10.4 10.6
treatment 12.1 8.6
treatment 11.2 11.0
treatment 10.0 8.8
treatment 12.9 9.5
treatment 9.1 10.0
treatment 13.4 9.6
treatment 11.6 9.8
treatment 11.5 9.8
treatment 12.0 10.6
')
dat$xval <- rep(as.Date(paste0("2020-", 1:10, "-01")), 2)
max_date1 <- dat[dat$cond == "control", "xval"][which.max(dat[dat$cond == "control", "yval"])]
max_date2 <- dat[dat$cond == "treatment", "xval"][which.max(dat[dat$cond == "treatment", "yval"])]
# The basic scatterplot
p <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) +
geom_point()
# Add colored lines for the date of the max yval of each group
p <- p +
geom_vline(aes(xintercept=as.numeric(max_date1)), colour="green") +
geom_vline(aes(xintercept=as.numeric(max_date2)), colour="lightblue")
p
fig <- ggplotly(p)
fig
Gives me this plot:

Error when importing tm Vcorpus into Quanteda corpus

This code snippet worked just fine until I decided to update R(3.6.3) and RStudio(1.2.5042) yesterday, though it is not obvious to me that is the source of the problem.
In a nutshell, I convert 91 pdf files into a volatile corpus named Vcorp and confirm that I created a volatile corpus as follows:
> Vcorp <- VCorpus(VectorSource(citiesText))
> class(Vcorp)
[1] "VCorpus" "Corpus"
Then I attempt to import this tm Vcorpus into quanteda, but keep getting an error message, which I did not get before (eg the day before the update).
> data(Vcorp, package = "tm")
> citiesCorpus <- corpus(Vcorp)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 8714, 91
Any suggestions? Thank you.
Impossible to know the exact problem without a) version information on your packages and b) a reproducible example.
Why use tm at all? You could have created a quanteda corpus directly as:
corpus(citiesText)
Converting a VCorpus works fine for me.
library("quanteda")
## Package version: 2.0.1
library("tm")
packageVersion("tm")
## [1] ‘0.7.7’
reut21578 <- system.file("texts", "crude", package = "tm")
VCorp <- VCorpus(
DirSource(reut21578, mode = "binary"),
list(reader = readReut21578XMLasPlain)
)
corpus(VCorp)
## Corpus consisting of 20 documents and 16 docvars.
## text1 :
## "Diamond Shamrock Corp said that effective today it had cut i..."
##
## text2 :
## "OPEC may be forced to meet before a scheduled June session t..."
##
## text3 :
## "Texaco Canada said it lowered the contract price it will pay..."
##
## text4 :
## "Marathon Petroleum Co said it reduced the contract price it ..."
##
## text5 :
## "Houston Oil Trust said that independent petroleum engineers ..."
##
## text6 :
## "Kuwait"s Oil Minister, in remarks published today, said ther..."
##
## [ reached max_ndoc ... 14 more documents ]

Why does my R notebook produce a blank html document

For some reason, my R notebook is producing a blank HTML document. When I'm ready to knit the document to an html notebook, my browser opens up the file and it is a blank document. I'm pressing the "knit" button, then "html" from R Studio.
Here is my code:
---
title: "Rate Hole Model"
output: html_document
---
```{r}
library(tidyverse)
library(plotly)
library(rmarkdown)
library(knitr)
```
```{r}
veh_age <- mc2 %>%
filter(cummulative < 51)
plot_ly(veh_age, x = ~unit_age, y = ~loss_ratio, color = ~rating_class_name) %>%
add_markers(text = ~paste(rating_class_name, "<br />", 'unit age: ',
unit_age, "<br />", 'loss ratio: ', loss_ratio), hoverinfo =
'text') %>%
layout(title = 'Comp Loss Ratio by Unit Age/Rating Class')
```
I'm not sure what happened. I'm on R version 3.5.1Has anyone ran into this problem?
My Libraries were installed to my Home directory : \Home\firstname.lastname\documents. This directory is a network located resource. When the last step of the process ran, the proper permission was not available to the called application (Pandoc). I am running 64 bit Win 10 with 64 bit RStudio version 1.1.456 and the 3.5 version of R. When I moved (reinstalled) the packages/libraries to a local folder : c:\Program Files\RStudio\Packages the HTML rendered in the browser.

pander on aov in knitr does not print?

I'm trying to print the outcome of an anova like so:
library(pander)
m.aov = aov(Sepal.Width ~ Species * Sepal.Length, iris)
pander(m.aov, split.table=Inf)
and I get this as expected if I type it into the console:
----------------------------------------------------------------------
Df Sum Sq Mean Sq F value Pr(>F)
-------------------------- ---- -------- --------- --------- ---------
**Species** 2 11.34 5.672 76.48 2.329e-23
**Sepal.Length** 1 4.769 4.769 64.3 3.368e-13
**Species:Sepal.Length** 2 1.513 0.7566 10.2 7.19e-05
**Residuals** 144 10.68 0.07417 NA NA
----------------------------------------------------------------------
Table: Analysis of Variance Model
However, if I embed this into a knitr chunk, I don't get the table:
```{r, results='asis'}
library(pander)
m.aov = aov(Sepal.Width ~ Species * Sepal.Length, iris)
pander(m.aov, split.table=Inf)
```
Knit the above and one obtains
```r
pander(m.aov, split.table=Inf)
```
i.e., the code chunk with no output.
Question: Is this a bug (in knitr? pander?) or something I've overlooked? How can I work around it?
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C LC_TIME=en_AU.UTF-8
[4] LC_COLLATE=en_AU.UTF-8 LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8
[7] LC_PAPER=en_AU.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.8 pander_0.5.1 vimcom_1.0-0 setwidth_1.0-3 colorout_1.0-3
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.5 formatR_1.0 Rcpp_0.11.2 stringr_0.6.2 tools_3.0.2

How to match daily data from monthly using Matlab?

I have montly macroeconomic data series and I am planning to use them for a weekly (every Monday) regression analysis. How can I match a data point which release once a month to my date template( 4 times during that month) since the new point release and so on.
for u=2:size(daily,1)
l=find(dailytemplate(u)==monthly)
%# when the monthly date is not equal to my daily template
if isempty(l)
%# I need a clearver code for this part to find the previous release
dailyclose(u)=dailyclose(u-1)
else
dailyclose(u)=monthlyclose(l)
end
end
UPDATE from comment
I have the following monthly macro data. I want to use them to feed the weekly dates. For example, at March 31/03/2012 the M-input was 2.7. So any weekly date after that date should be
W_output=2.7
until the April 30/04/2012. Then the weekly W_output will be 2.3 which is the new monthly point, M_input. The following table provides examples for the weekly W_ouput and monthly M_Input:
08/06/2012 1.7
30/06/2012 1.7
01/06/2012 1.7
31/05/2012 1.7
25/05/2012 2.3
30/04/2012 2.3
18/05/2012 2.3
31/03/2012 2.7
11/05/2012 2.3
29/02/2012 2.9
04/05/2012 2.3
31/01/2012 2.9
27/04/2012 2.7
31/12/2011 3
20/04/2012 2.7
format long g
%Create a vector of dates (what I am assuming your date template looks like, this is march 31 and the next 9 mondays that follow it)
datetemplate = [datenum('2012/03/31')];
for i = 1:10
datetemplate(i + 1) = datetemplate(i) + 7;
end
datetemplate';
%Your macro ecos input and dates
macrochangedate = [datenum('2012/03/31');datenum('2012/04/30')]
macrochangedate = [macrochangedate [2.7; 2.3]]
for i = 1:size(macrochangedate,1)
result(datetemplate >= macrochangedate(i,1)) = macrochangedate(i,2);
end
Results:
result =
2.7
2.7
2.7
2.7
2.7
2.3
2.3
2.3
2.3
2.3
2.3
datestr(datetemplate)
ans =
31-Mar-2012
07-Apr-2012
14-Apr-2012
21-Apr-2012
28-Apr-2012
05-May-2012
12-May-2012
19-May-2012
26-May-2012
02-Jun-2012
09-Jun-2012