Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在RMarkdown中只有一个交互式绘图图表呈现为HTML_R_Markdown_R Markdown_Plotly_R Plotly - Fatal编程技术网

在RMarkdown中只有一个交互式绘图图表呈现为HTML

在RMarkdown中只有一个交互式绘图图表呈现为HTML,r,markdown,r-markdown,plotly,r-plotly,R,Markdown,R Markdown,Plotly,R Plotly,我正在使用R标记绘制曲面 p1 <- plot_ly() %>% add_surface(z=z,x=wRange,y=yRange) %>% layout(showlegend=FALSE,scene=list(xaxis=list(title="wMult"),yaxis=list(title="yMult"),zaxis=list(title="MAE"))) p1 p1%add_surface(z=z,x=wRange,y=yRange)%%>%布局(showlege

我正在使用R标记绘制曲面

p1 <- plot_ly() %>% add_surface(z=z,x=wRange,y=yRange) %>% layout(showlegend=FALSE,scene=list(xaxis=list(title="wMult"),yaxis=list(title="yMult"),zaxis=list(title="MAE")))
p1
p1%add_surface(z=z,x=wRange,y=yRange)%%>%布局(showlegend=FALSE,scene=list(xaxis=list(title=“wMult”)、yaxis=list(title=“yMult”)、zaxis=list(title=“MAE”))
p1
稍后,我将通过执行以下操作向该曲面添加点:

p2 <- p1 %>% add_markers(z=MAE1,x=wMult1,y=yMult1) %>% layout(showlegend=FALSE)
p2
p2%add_标记(z=MAE1,x=wMult1,y=yMult1)%%>%layout(showlegend=FALSE)
p2
在那之后不久,我试图通过在p2的顶部添加另一个标记来绘制p3

p3 <- p2 %>% add_markers(z=MAE2,x=wMult2,y=yMult2) %>% layout(showlegend=FALSE)
p3
p3%add_标记(z=MAE2,x=wMult2,y=yMult2)%%>%layout(showlegend=FALSE)
p3
不幸的是,在HTML中只有p1呈现为交互式图表。p2和p3显示为空白,大致与图表的大小相同,但在查看器和浏览器中都没有任何内容。如果我使用web inspector,我可以看到它正在尝试渲染plotly对象,但它看起来是空的

如果我直接在RStudio中运行相同的代码,我可以查看添加了额外标记的绘图,但在我整理标记时它们不会渲染

这是怎么回事

数据集可在以下位置获得:

以下是迄今为止的完整降价代码:

---
title: "Gradient Descent Demo"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
setwd("[your working directory]")
data = read.csv("mpg.csv")
require(plotly)
```

## Our Dataset

Let's take a look at some sample data. It shows attributes of several old cars and their fuel economy measured in miles per gallon. 

```{r c1}
head(data)
```

We'll try to predict a given car's mpg using only its weight and year.
```{r c2}
data <- data[,c("mpg","weight","year")]
pairs(data)
```

## Create a Hypothesis
Our hypothesis will be that we can get an approximation of the mpg by multipling the car's weight by some number "wMult" and adding that to the car's year multiplied by some other number "yMult". Let's just pick some numbers to start.
```{r c3, include=FALSE}
mod1 <- lm(mpg~weight+year,data=data)
bias1 <- mod1$coefficients[1]
```
```{r}
wMult1 <- -.02
yMult1 <- -2
```
We can turn this into a prediction.
(Ignore the bias - I cheated and did some behind-the-scenes pre-work.)
```{r c4}
data$mpgPred1 <- wMult1*data$weight + yMult1*data$year + bias1
head(data)
```
Ok so we have predictions. They're clearly pretty bad since they're negative, and most cars don't get negative miles per gallon. But can we measure how bad they are?

## Evaluate the Hypothesis
We need some measure of how good (or bad) our prediction is. We'll use the Mean Absolute Error ("MAE"). As the name suggests, this is calculated finding the average of the absolute difference between each predicted value and actual value.
```{r c5}
MAE1 <- mean(abs(data$mpgPred1-data$mpg))
MAE1
```
Ok so on average we're only off by about 250 mpg. Surely we can do better.

## Adjust the Hypothesis
What to use for our next hypothesis? Well we assign new wMult and yMult values and see how we do.
```{R c6}
wMult2 <- wMult1 + .03
yMult2 <- wMult2 - 1.2
data$mpgPred2 <- wMult2*data$weight + yMult2*data$year + bias1
head(data)
```
Our predictions look better (At least they're positive!), but they're still pretty far off. Let's see how much better or worse they are.

## Evaluate the Hypothesis - Round 2
```{R c7}
MAE2 <- mean(abs(data$mpgPred2-data$mpg))
MAE1
MAE2
```
Now we're only off by 50 on average. Still pretty terrible, but better than before.

## Adjust the Hypothesis - There has to be a better way.
Ok so instead of just continuing to make random guesses, let's develop a way to intelligently update our hypothesis.

Thankfully, since we're only using two variables for our analysis, we can pretty easily visualize the effect of every reasonable combination of wMult and yMult.
```{R c8, include=FALSE}
plotdata <- data.frame(wCoef=double(),yCoef=double(),MAE=double())
wRange <- seq(mod1$coefficients[2]-300*summary(mod1)$coefficients["weight","Std. Error"],mod1$coefficients[2]+300*summary(mod1)$coefficients["weight","Std. Error"],length.out=201) 
yRange <- seq(mod1$coefficients[3]-300*summary(mod1)$coefficients["year","Std. Error"],mod1$coefficients[3]+300*summary(mod1)$coefficients["year","Std. Error"],length.out=201)
for(i in wRange)
{for(j in yRange)
{
  preds <- (i*data$weight) + (j*data$year) + bias1
  resid <- preds-data$mpg
  MAE = mean(abs(resid))
  newRec <- data.frame(wCoef=i,yCoef=j,MAE=MAE)
  plotdata <- rbind(plotdata,newRec)
}
}
z <- matrix(plotdata$MAE,nrow=201,ncol=201)
```
```{R c9}
p1 <- plot_ly() %>% add_surface(z=z,x=wRange,y=yRange) %>% layout(showlegend=FALSE,scene=list(xaxis=list(title="wMult"),yaxis=list(title="yMult"),zaxis=list(title="MAE")))
p1
```
Great - we can visibly explore this graph and see what some good weights might be. The best one is the one that minimizes the MAE. That's the center spot at the middle of the valley, where the crease seems to dip slightly.

Let's add our first hypothesis to this chart to see where it falls.
```{R c10,warning=F}
p2 <- p1 %>% add_markers(z=MAE1,x=wMult1,y=yMult1) %>% layout(showlegend=FALSE)
p2
```
And let's add our second one
```{R c11}
p3 <- p2 %>% add_markers(z=MAE2,x=wMult2,y=yMult2) %>% layout(showlegend=FALSE)
p3
```
Ok so it turns out our second guess actually overshot. This means that if we kept updating our hypothesis in the same manner, we'd actually get worse with each new step.

## Letting the machine do it
As I mentioned before, this approach works because we only have 2 variables we're working with. But if we had more, we'd be dealing with spaces greater than 3 dimensions. This gets hard to visualize.

Thankfully there's a way for the machine to navigate those higher dimensional spaces. We'll continue to use this two dimensional approach for now to help illustrate the approach.
---
标题:“梯度下降演示”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
setwd(“[您的工作目录]”)
数据=read.csv(“mpg.csv”)
要求(详细地)
```
##我们的数据集
让我们看看一些样本数据。它显示了几辆旧车的属性以及它们的燃油经济性(以每加仑英里数为单位)。
```{r c1}
总目(数据)
```
我们将尝试仅使用重量和年份来预测给定汽车的mpg。
```{r c2}

数据在HTML领域,像
id
这样的属性很重要。发生的情况是,绘图的
div
id继承自先前的绘图。这在HTML中是不允许的。因此,您每次都需要重新创建绘图,以便它们不会继承绘图id。我找不到重置id以防止此问题的
plotly
函数,因此我的答案是遵循严格的“不继承以前的绘图”策略:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
```

# First plot

```{r pressure, echo=FALSE}
p1 <- plot_ly(source = "plot1") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure)
p1
```

# Second plot

```{r pressure2, echo= FALSE}
p2 <- plot_ly(source = "plot2") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure) %>%
  add_markers(x = pressure$temperature, y = pressure$pressure+10)
p2
```
---
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
图书馆(绘本)
```
#第一个情节
```{r压力,回波=假}
p1%
添加_标记(x=压力$温度,y=压力$压力)
p1
```
#第二个情节
```{r pressure2,echo=FALSE}
p2%
添加_标记(x=压力$温度,y=压力$压力)%>%
添加_标记(x=压力$温度,y=压力$压力+10)
p2
```

旁注:如果您在一个闪亮的环境中,每个绘图都会被包装在一个需要唯一命名的渲染/输出组合中。

在HTML领域,像
id
这样的属性很重要。发生的情况是,绘图的
div
id继承自先前的绘图。这在HTML中是不允许的。因此,您每次都需要重新创建绘图,以便它们不会继承绘图id。我找不到重置id以防止此问题的
plotly
函数,因此我的答案是遵循严格的“不继承以前的绘图”策略:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
```

# First plot

```{r pressure, echo=FALSE}
p1 <- plot_ly(source = "plot1") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure)
p1
```

# Second plot

```{r pressure2, echo= FALSE}
p2 <- plot_ly(source = "plot2") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure) %>%
  add_markers(x = pressure$temperature, y = pressure$pressure+10)
p2
```
---
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
图书馆(绘本)
```
#第一个情节
```{r压力,回波=假}
p1%
添加_标记(x=压力$温度,y=压力$压力)
p1
```
#第二个情节
```{r pressure2,echo=FALSE}
p2%
添加_标记(x=压力$温度,y=压力$压力)%>%
添加_标记(x=压力$温度,y=压力$压力+10)
p2
```

旁注:如果您在一个闪亮的环境中,每个绘图都会被包装在一个需要唯一命名的渲染/输出组合中。

您能分享一个包含rmarkdown代码的可复制示例吗?数据集在这里提供。我将用完整的标记代码更新我的帖子。你能分享一个包含rmarkdown代码的可复制示例吗?数据集在这里。我将用完整的降价代码更新我的帖子。谢谢你的帮助。每次重新定义完整的绘图似乎都很好。这有点烦人,因为我最终会有一个绘图,作为梯度下降过程的一部分,迭代添加多达100个标记,但至少现在我知道它为什么会这样,我可以在到达之前考虑故障排除。非常感谢。我同意这很烦人。也许开发者会在你向下游添加元素时添加重命名绘图的选项。谢谢你的帮助。每次重新定义完整的绘图似乎都很好。这有点烦人,因为我最终会有一个绘图,作为梯度下降过程的一部分,迭代添加多达100个标记,但至少现在我知道它为什么会这样,我可以在到达之前考虑故障排除。非常感谢。我同意这很烦人。当您向下游添加元素时,开发人员可能会添加重命名绘图的选项。