0
我有一个问题,在我的Shiny应用程序ggvis中使用工具提示。我想用更多的信息有关ggvis点一点,这就是为什么我创建功能,需要id
变量:ggvis绘图消失与工具提示和checkBoxInput闪亮
add_info <- function(x) {
if(is.null(x)) return(NULL)
if (is.null(x$id)) return(NULL)
df2<- isolate(data)
df <- df2[df2$id == x$id, ]
paste0(df$info,
df$number)
}
,当我在闪亮的应用即复选框按钮,在工作的时候取消选中所有按钮的启动问题,错误出现:
Error in eval(substitute(expr), envir, enclos) :
wrong result size (2), expected 0 or 1
我知道是因为我的过滤条件排除所有的数据。但是在这种情况下,当我点击所有checkBox选项时(如下面的Line plot),我想看到一个空的图。怎么做?
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
wellPanel(checkboxGroupInput("variable",label = "",
choices = list("a","b","c","d"),
selected = c("a"))),
br(),br(),br(),br(),br(),br(),br(),br(),
wellPanel(checkboxGroupInput("variable2",label = "",
choices = list("a","b","c","d"),
selected = c("a")))
),
mainPanel(
ggvisOutput("scatter_plot"),
ggvisOutput("line_plot")
)
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
dataset <- reactive({
df <- df1 %>%
filter(name %in% input$variable)
df
})
data <- reactive({
dataset() %>%
mutate(id = 1:n())
})
vis2 <- reactive({
add_info <- function(x) {
if(is.null(x)) return(NULL)
if (is.null(x$id)) return(NULL)
df2 <- isolate(data())
df <- df2[df2$id == x$id, ]
paste0(df$info,"<br>",
df$number)
}
data() %>%
ggvis(~number2, ~number, fill = ~name) %>%
layer_points(size := 100,
size.hover := 240,
key := ~id) %>%
add_tooltip(add_info,"hover")
})
vis2 %>% bind_shiny("scatter_plot")
# LinePlot
dataset_line <- reactive({
df_line <- df1 %>%
filter(name %in% input$variable2)
})
vis <- reactive({
dataset_line() %>%
ggvis(~number2, ~number, stroke = ~name) %>%
layer_lines()
})
vis %>% bind_shiny("line_plot")
})
global.R
df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2,
info = "info")
df1_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,5,0.01),20,replace = T)
@docendodiscimus,+1,它进一步简化了它! – cdeterman 2015-02-23 14:11:37
@ docendodiscimus,cdeterman谢谢你们!真的帮助! – Nicolabo 2015-02-23 14:23:25
@ docendodiscimus,cdeterman嗨,再次。我现在尝试使用它的代码,但是当我解除所有选择时,出现错误:'eval中的错误(替代(expr),envir,enclos):上限值必须大于下限值'。我记不清了,但可能我重新安装'dplyr'软件包。 – Nicolabo 2015-03-07 13:30:31