我遇到以下示例ggvis代码的问题,该代码用于创建一个图表,当您将鼠标悬停在该组的任何成员上时,该图表会突出显示整组点.然后,当你徘徊时,我希望突出显示消失.发生的事情是突出显示最初起作用,但是当你悬停时,突出显示保持不变,只有当你将鼠标悬停在另一组点上然后再次将它们悬停时它们才会消失.
library(magrittr)
library(dplyr)
library(ggvis)
library(shiny)
dat <- iris %>% select(-Species) %>% dist %>% cmdscale %>% data.frame %>% tbl_df %>% mutate(Species = iris$Species) %>%
data.frame
Props <- reactiveValues(Size = rep(50, length.out = nrow(dat)), Stroke = rep("white", length.out = nrow(dat)))
hoveron <- function(data, ...) {
Props$Size[dat$Species == data$Species] <- 150
print("hoveron!")
Props$Stroke[dat$Species == data$Species] <- "black"
}
hoveroff <- function(...) {
Props$Size <- rep(50, length.out = nrow(dat))
print("hoveroff!")
Props$Stroke <- rep("white", length.out = nrow(dat))
}
dat %>%
ggvis(~X1, ~X2, fill …Run Code Online (Sandbox Code Playgroud) 下面的可重复示例.每当我包含一个ggvis数字时,我都会在桌面上丢失格式.
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
library(dplyr)
library(ggvis)
library(knitr)
```
The following table looks fine...
```{r echo=FALSE, results='asis'}
cars %>% kable(format = 'markdown')
```
As long as I don't include this plot below
```{r, echo=FALSE}
pressure %>%
ggvis(x = ~temperature, y = ~pressure) %>%
layer_bars()
```
Run Code Online (Sandbox Code Playgroud) 在我闪亮的应用程序中,我有两个选项卡:选项卡 1 有一个 checkboxInput 和一个 selectInput,它在 server.R 中编码为 renderUI,并且仅在选中该框时才显示。在突片2中,存在GGVIS功能,用于绘制通过在选项卡1中所示的选择input时通过反应函数进行的数据帧。
出乎意料的是,selectInput 没有显示在选项卡 1 中,除非我先单击选项卡 2 然后返回选项卡 1,即使 selectInput 仅依赖于同一选项卡中的复选框,即选项卡 1。
显然,我没有正确理解反应式函数的概念。你能指出我的错误在哪里吗?谢谢!
PS 结构相当复杂,但它是我的“真实”应用程序所需要的。
用户界面
library(ggvis)
shinyUI(navbarPage("",
tabPanel("1",
checkboxInput("START", label = "Start calculations", value = F),
htmlOutput("SELECT")
),
tabPanel("2",
ggvisOutput("PLOT")
)
))
Run Code Online (Sandbox Code Playgroud)
服务器
library(ggvis)
shinyServer(function(input, output, session) {
output$SELECT<-renderUI({
if (input$START==T) {
selectInput("SELECTINPUT","Make your choice:",c(1,2))
}
})
PLOTDF<-reactive({
if (is.null(input$SELECTINPUT)==F) {
plotdf<-data.frame(c(1,1,2,2),c(1,2,3,4))
colnames(plotdf)<-c("X","Y")
plotdf
}
})
reactive({
PLOTDF() %>% ggvis(~X,~Y) %>% layer_points
}) %>% bind_shiny("PLOT")
})
Run Code Online (Sandbox Code Playgroud) 我正在使用ggvisR中的包装附带的内置"可卡因"数据库来可视化每个州的可卡因效力计数.dplyr还使用了R包.
这是cocaine数据集的前六行:
state potency weight month price
1 WA 77 217 1 5000
2 CT 51 248 1 4800
3 FL 68 43 1 3500
4 OH 69 123 1 3500
5 MS 75 118 1 3400
6 VA 73 127 1 3000
Run Code Online (Sandbox Code Playgroud)
目标是input_select()在ggvis包中使用创建下拉菜单,其中可以选择各种状态并查看该状态的效力计数的直方图.我们设法用这段代码做到了:
state <- as.vector(unique(cocaine$state))
cocaine %>%
ggvis(~potency) %>%
filter(state == eval(input_select(
choices = state,
selected = "NY",
label = "State"))) %>%
layer_histograms(binwidth = 2)
Run Code Online (Sandbox Code Playgroud)
问题是为什么我们需要表达式input_select()被"评估" eval(). …
我希望能够
这听起来很简单 - 我的代码是:
minx = minx = input_numeric(1, 'Min x-val')
maxx = input_numeric(1, 'Max x-val')
data.frame(train.dt) %>%
ggvis(x = ~plot_idx, y = ~val) %>%
layer_lines() %>% add_axis('x') %>%
scale_numeric('x', domain = c(minx, maxx), clamp = T)
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我收到此错误消息:
"r [i1]中的错误 - r [-length(r):-(length(r) - lag + 1L)]:二元运算符的非数字参数".
如果我用例如1和10替换domain参数中的minx和maxx,我的图表就可以很好地绘制(但是是静态的).有任何想法吗?
谢谢!
我可以看到ggvis,rCharts等如何适应server.r + ui.r构造.我现在正在尝试制作HTML用户界面,但我无法找到有关将交互式图表传递给HTML用户界面的任何线索.有线索吗?
调查了ggvis,rCharts,NBD3.没有调查的情节.避免使用googleVis.
Evan Farrell之前曾提出过一个类似的问题,但没有回复 - https://groups.google.com/forum/#!topic/ggvis/GsZQRl3VOZ4
编辑:这些需要自定义绑定吗?任何更简单的替代品?我认为输出将是javascript生成的SVG,所以是不可能手动包含库并且SVG刚刚绑定到div元素?
编辑2:我可以以某种方式将我的数据集传递给UI,并在index.html本身构建一个D3图表
export_png()R ggvis 包中的函数要求我们vg2png从 node.js 模块安装程序vega。(来源:R 文档)
我在 Windows 上安装了 node 和 npm,然后运行了npm install vega.
这是我运行的输出npm install vega:
C:\Users\username>npm install -g trifacta/vega
\
> canvas@1.1.6 install C:\Users\username\AppData\Roaming\npm\node_modules\vega\node_modules\canvas
> node-gyp rebuild
|
C:\Users\username\AppData\Roaming\npm\node_modules\vega\node_modules\canvas>node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bi
n\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild
Node Commands
Syntax:
node {operator} [options] [arguments]
Parameters:
/? or /help - Display this help message.
list - List nodes or node history or the cluster
listcores - List cores on the cluster
view …Run Code Online (Sandbox Code Playgroud) 当我键入以下代码时
library(data.table)
library(ggvis)
age <- as.integer(c(1,4,8,5,4,3,2,4,5,4,3))
disk <- c(3,3,3,3,3,3,3,3,3,3,3)
dt <- data.table(age, disk)
df <- data.frame(age, disk)
dt %>%
ggvis(x=~age) %>%
layer_histograms()
df %>%
ggvis(x=~age) %>%
layer_histograms()
Run Code Online (Sandbox Code Playgroud)
使用数据框时,会生成直方图.但是,当使用数据表时,我收到错误消息:
Guessing width = 2 # range / 30
Error: This code should not be reached.
Run Code Online (Sandbox Code Playgroud)
没有情节打印.有人可以告诉我为什么数据表输入会产生这个错误吗?
谢谢
使用这个小数据集:
df <- structure(list(colour = structure(c(1L, 2L, 1L, 2L), .Label = c("Black",
"White"), class = "factor"), variable = c("A", "A", "B", "B"),
value = c(1, 2, 0.74, 0.85)), row.names = c(NA, -4L), .Names = c("colour",
"variable", "value"), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我可以轻松创建一个垂直堆叠的条形图 ggvis
library(ggvis)
df %>%
ggvis(x=~variable, y=~value, fill=~colour) %>%
group_by(colour) %>%
layer_bars()
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何有一个水平堆叠的条形图.我想我不知何故应该使用,layer_rects但到目前为止我能得到的最好只是一组绘制.
df %>%
ggvis(x =~value, y=~variable, fill =~ colour) %>%
group_by(colour) %>%
layer_rects(x2 = 0, height = band())
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个带有 ggvis 绘图和单选按钮的闪亮应用程序。我有 ggvis 创建的三个图。用户可以根据他们选择的单选选项切换不同的绘图。
例如,如果用户选择A,则用户界面上仅显示plot1。如果用户选择 B,则绘图切换到绘图 2。我的问题是我不知道如何将图与单选按钮连接起来。我已经挣扎了几个小时了。非常感谢你的帮助!我下面有一些示例代码。
df <- data.frame(Student = c("a","a","a","a","a","b","b","b","b","b","c","c","c","c"),
year = c(seq(2001,2005,1),seq(2003,2007,1),seq(2002,2005,1)),
col1 = runif(14,min = 50,max = 100),
col2 = runif(14,min = 120,max = 200),
col3 = runif(14,min = 60,max = 200),stringsAsFactors=F)
Run Code Online (Sandbox Code Playgroud)
代码:
ui = (fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("stu","Choose Student",
choice = unique(df$Student)),
radioButtons("col","Switch Plot",
choices = c("A", "B","C"),
selected = "A")
),
mainPanel(ggvisOutput("plot1")
))
))
server = function(input,output,session){
dataInput = reactive({
gg = df[which(df$Student == input$stu),]
})
vis1 = reactive({
data = dataInput() …Run Code Online (Sandbox Code Playgroud)