我正在尝试学习如何用ggvis图替换闪亮的静态图.用下面ui.R和server.R文件,我可以得到一个完全正常工作闪亮应用程序时,我的情节输出基地-R或ggplot.当我尝试使用ggvis时出现以下错误.
Error in handlers$add(handler, key, tail) : Key / already in use
我已经尝试将我的文件的位置更改为不同的目录,清除我的全局环境等.到目前为止似乎没有任何工作.
我试图重现最小的可重现的例子.以下是在我的机器上重现错误.我正在输入存储在与ui.R和server.R相同的文件夹中的csv文件中的数据.我添加了一些可用于重现csv文件的数据的dput().
这是一个例子:
ui.R
library(shiny)
library(dplyr)
library(ggvis)
# Define UI
shinyUI(pageWithSidebar(
# Application title
headerPanel("Cricket"),
sidebarPanel(
selectInput("hteam",
label = "Home Team",
choices = c("All Teams", "Australia", "England"), selected = "All teams"),
br()
),
mainPanel(
plotOutput("CrickPlot")
)
))
Run Code Online (Sandbox Code Playgroud)
server.R
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output) {
#### Input raw data
df <- read.csv("mydf.csv", stringsAsFactors=F, header=T)
df1 <- reactive({
hometeam <- input$hteam
if(input$hteam != "All Teams"){ df <- df %>% filter(team==hometeam) }
df %>%
group_by(player) %>%
summarize(totalruns=sum(runs,na.rm=T), totalinns=n() )
})
####
output$CrickPlot <- renderPlot({
tmp <- df1()
tmp$id <- 1:nrow(tmp)
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- tmp[tmp$id == x$id, ]
paste("Name: ", tmp$player[x$id],
"<br>Country: ",
tmp$team[x$id],
"<br>Total runs: ",
tmp$totalruns[x$id])
}
tmp %>%
ggvis(x = ~totalinns,
y = ~totalruns,
size := input_slider(10, 100),
size.hover := 200,
opacity := input_slider(0, 1),
key := ~id) %>%
layer_points() %>%
add_tooltip(all_values, "hover")
# x <- df1()
# plot(x$totalruns, x$totalinns)
})
}
)
Run Code Online (Sandbox Code Playgroud)
这里编码的ggvis比我在现实中使用的更简单.但是,这仍然会重现错误.如果我总结一下我的df并尝试制作一个闪亮的ggvis图表,这段代码就可以很好地运行.此外,仅出于说明目的,如果删除了所有ggvis内容,则在hashmark之后的最后两行将在base-R中生成散点图.因此,我认为这与闪亮应用程序中的ggvis有关.
以下是此示例的mydf数据:
dput(mydf)
structure(list(player = c("CB Fry", "CB Fry", "G Boycott", "G Boycott",
"G Boycott", "G Boycott", "MJ Slater", "MJ Slater", "MJ Slater",
"MJ Slater", "MJ Slater", "MJ Slater", "MJ Slater", "MJ Slater",
"SK Warne", "SK Warne", "SK Warne", "SK Warne", "SK Warne", "SK Warne"
), team = c("England", "England", "England", "England", "England",
"England", "Australia", "Australia", "Australia", "Australia",
"Australia", "Australia", "Australia", "Australia", "Australia",
"Australia", "Australia", "Australia", "Australia", "Australia"
), runs = c(1L, 50L, 68L, 31L, 30L, 23L, 26L, 16L, 123L, 1L,
45L, 43L, 28L, 10L, 15L, 2L, 0L, 14L, 2L, NA)), row.names = c(NA,
-20L), .Names = c("player", "team", "runs"), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
提前致谢.我试图尽可能减少这一点,同时显示与错误相关的所有可能的细节.
在server.R中
output$ggvisplot_ui <- renderUI({
ggvisOutput("ggvisplot")
})
dl <- mtcars
dl %>%
ggvis(~mpg, ~wt) %>%
layer_points() %>%
bind_shiny("ggvisplot")
Run Code Online (Sandbox Code Playgroud)
在ui.R
mainPanel(
htmlOutput("ggvisplot_ui")
)
Run Code Online (Sandbox Code Playgroud)