这就是我到目前为止在R中的ggvis包.
mtcars %>% ggvis(x = ~disp) %>%
layer_lines(y = ~wt, stroke := "red") %>%
layer_lines(y = ~mpg) %>%
add_axis("y", orient = "left", title = "Weight (lb/1000)") %>%
add_axis("y", orient = "right", title= "Miles/(US) gallon") %>%
add_axis("x", title = "Displacement (cu.in.)")
Run Code Online (Sandbox Code Playgroud)
我不能让左Y轴代表wt比例数据.
这输出:

最好用一个例子来解释.
我有一个data.frame名为的向量或列vec:
vec <- c(NA, NA, 1, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA)
Run Code Online (Sandbox Code Playgroud)
我想一个矢量化处理(不是for循环)来改变三个后NA一个时1,观察到.
结束向量将是:
c(NA, NA, 1, 1, 1, 1, NA, 1, 1, 1, 1, NA, NA, NA)
Run Code Online (Sandbox Code Playgroud)
如果我们有:
vec <- c(NA, NA, 1, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA)
Run Code Online (Sandbox Code Playgroud)
结束向量看起来像:
c(NA, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA)
Run Code Online (Sandbox Code Playgroud)
一个写得很糟糕的解决方案是:
vec2 <- vec …Run Code Online (Sandbox Code Playgroud) 有一些奇怪的行为dygraph.
使用for循环时,dygraph我没有结果.
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
for(i in 1:2){
dygraph(lungDeaths[, i])
}
Run Code Online (Sandbox Code Playgroud)
另一方面,当我使用lapply我确实得到预期的结果
lapply(1:2, function(i) dygraph(lungDeaths[, i]))
Run Code Online (Sandbox Code Playgroud)
我实际上想在我自己的数据集中使用for循环R Markdown并迭代不同的列,但即使我使用lapply"解决方法",它也不会绘制dygraphs
R Markdown代码
---
title: "Untitled"
author: "dimitris_ps"
date: "28 May 2015"
output: html_document
---
```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
lapply(1:2, function(i) dygraph(lungDeaths[, i]))
```
Run Code Online (Sandbox Code Playgroud)
而当我逐列运行它时,它的工作原理
---
title: "Untitled"
author: "dimitris_ps"
date: "28 May 2015"
output: html_document
---
```{r echo=FALSE}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths) …Run Code Online (Sandbox Code Playgroud) 在表面上,他们似乎都在做同样的事情.但似乎后者as(,"character")更强大.
举个例子考虑以下内容:
library(rvest)
temp <- html("http://www.example.com/")
temp <- temp %>% html_node("div p")
str(temp)
#Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr>
as.character(temp)
#Error in as.vector(x, "character")
# cannot coerce type 'externalptr' to vector of type 'character'
Run Code Online (Sandbox Code Playgroud)
而as(temp, "character")给出
#[1] "<p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>"
Run Code Online (Sandbox Code Playgroud) 该read_html函数生成一个xml_document,我想保存,稍后加载它来解析它.
问题是加载xml_document后,其中没有html.
library(rvest)
library(magrittr)
doc <- read_html("http://www.example.com/")
doc %>% html_node("h1") %>% html_text
Run Code Online (Sandbox Code Playgroud)
我明白了: [1] "Example Domain"
但是当我首先保存xml_document doc对象并再次加载它时,似乎一切都已被清除.
save(doc, file=paste0(getwd(), "/example.RData"))
rm(doc)
load(file=paste0(getwd(), "/example.RData"))
doc %>% html_node("h1") %>% html_text
Run Code Online (Sandbox Code Playgroud)
我明白了: Error: No matches
或者当我运行时doc我得到:{xml_document}一个空的xml_document.
也是这样的情况,当我运行它doc,在加载它之后,我得到一条消息,RStudio已经停止工作.
我在两台不同的Windows机器上试过它,遇到了同样的问题.
sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] …Run Code Online (Sandbox Code Playgroud) 我将如何RSelenium并行运行?
以下是rvest并行使用的示例
library(RSelenium)
library(rvest)
library(magrittr)
library(foreach)
library(doParallel)
URLsPar <- c("http://www.example.com/", "http://s5.tinypic.com/n392s6_th.jpg", "http://s5.tinypic.com/jl1jex_th.jpg",
"http://s6.tinypic.com/16abj1s_th.jpg", "http://s6.tinypic.com/2ymvpqa_th.jpg")
(detectCores() - 1) %>% makeCluster %>% registerDoParallel
ws <- foreach(x = 1:length(URLsPar), .packages = c("rvest", "magrittr", "RSelenium")) %dopar% {
URLsPar[x] %>% read_html %>% as("character")}
stopImplicitCluster()
Run Code Online (Sandbox Code Playgroud) 假设我有以下数据框架
set.seed(123)
df <- data.frame(var1=(runif(10)>0.5)*1)
Run Code Online (Sandbox Code Playgroud)
var1 可以有任何类型/数量的级别,而不是具体的0和1
我想创建一个var2每次var1更改时增加1 而不使用afor loop
这种情况下的预期结果是:
data.frame(var1=(runif(10)>0.5)*1, var2=c(1, 2, 3, 4, 4, 5, 6, 6, 6, 7))
var1 var2
0 1
1 2
0 3
1 4
1 4
0 5
1 6
1 6
1 6
0 7
Run Code Online (Sandbox Code Playgroud)
数据框的另一个选择可能是:
df <- data.frame(var1=c("a", "a", "1", "0", "b", "b", "b", "c", "1", "1"))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,结果应该是:
var1 var2
a 1
a 1
1 2
0 3
b 4
b 4
b 4 …Run Code Online (Sandbox Code Playgroud) 到目前为止,我有以下代码作为示例:
library(DT)
datatable(iris, options = list(pageLength = 5)) %>%
formatStyle(
'Sepal.Width',
backgroundColor = styleInterval(3, c('gray', 'yellow'))
)
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是仅根据条件突出显示特定的“单元格”。
例如如果 iris[3, 2] > 3.1那么背景颜色应该是黄色。
供参考http://rstudio.github.io/DT/
sessionInfo() DT_0.1
我有一个DT内部Rmarkdown,我想在悬停在表数据上时弹出一个图像.
我所拥有的似乎工作,但它扭曲了datatable.
它增加了表行高度以适合图像.我试图减少行大小,css但没有运气.
这是Rmarkdown我到目前为止:
---
title: "Untitled"
author: "dimitris_ps"
date: "3 September 2016"
output: html_document
---
<style type="text/css">
.imgTooltip {
visibility: hidden;
}
.ItemsTooltip:hover .imgTooltip {
visibility: visible;
}
td {
height: 14px;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DT)
df <- structure(list(a = c("<a class=\"ItemsTooltip\" href=\"http://www.example.com\" target=\"_blank\"><img class=\"imgTooltip\" src=\"https://i.stack.imgur.com/uSSEu.jpg\"/>my stackoverflow Avatar</a>",
"<a class=\"ItemsTooltip\" href=\"http://www.example.com\" target=\"_blank\"><img class=\"imgTooltip\" src=\"https://i.stack.imgur.com/uSSEu.jpg\"/>my stackoverflow Avatar</a>"
), b = c("<a class=\"ItemsTooltip\" href=\"http://www.example.com\" target=\"_blank\"><img class=\"imgTooltip\" …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个运动折线图。功能如下所示
library(highcharter)
library(magrittr)
highchart() %>%
hc_chart(type = "line") %>%
hc_yAxis(max = 12, min = 0) %>%
hc_xAxis(categories = c(1, 1.7, 1, 0)) %>%
hc_add_series(data = list(
list(sequence = c(1,1,1,1)),
list(sequence = c(NA,2,2,2)),
list(sequence = c(NA,NA,5,5)),
list(sequence = c(NA,NA,NA,10))
)) %>%
hc_motion(enabled = TRUE, labels = 1:4, series = 0)
Run Code Online (Sandbox Code Playgroud)
但我希望最终结果如下所示,使用hc_motion选项
hchart(data.frame(xx=c(1, 1.7, 1, 0), yy=c(1, 2, 5, 10)),
type = "line", hcaes(x = xx, y = yy))
Run Code Online (Sandbox Code Playgroud)
即问题是在第一种情况下,运动图将其视为xAxis类别,而我希望它像带有直线的散点图。
r ×10
dt ×2
r-markdown ×2
css ×1
datatables ×1
doparallel ×1
dygraphs ×1
ggvis ×1
plot ×1
rselenium ×1
rvest ×1
xml ×1