ツールチップを米国の地図に重ねようとしていますが、どこにホバーしても同じデータが表示されます。また、データが間違っています。文字値ではなく、要素値を通過していると思います。ムービー エクスプローラーの例 ( http://shiny.rstudio.com/gallery/movie-explorer.html ) からヒントを得ようとしましたが、期待どおりに動作しません。私が調べるべきヒントや手がかりはありますか?
更新:関数に呼び出されている引数のみを渡すことができると判断しましたggvis
。したがって、ツールチップ関数にregion
, long
, &が含まれている場合、lat
それらすべてがツールチップに表示されます。Population
andは関数のどこにも表示されないためIncome
、通過していません。どのように進めるかはまだ迷っていますが、どんなアイデアでも素晴らしいでしょう! :)
library(ggplot2)
library(shiny)
library(ggvis)
library(dplyr)
shinyApp(
ui = fluidPage(
#numericInput("n", "n", 1),
ggvisOutput("map")
),
server = function(input, output) {
statesData <- reactive({
states <- data.frame(state.x77)
states$region <- row.names(state.x77) %>% tolower
row.names(states) <- NULL
all_states <- map_data("state") %>%
mutate(region = tolower(region)) %>%
left_join(states)
all_states_unique <- all_states %>%
select(region, Population, Income, Illiteracy, Life.Exp, Murder, HS.Grad, Frost, Area) %>%
unique
states_tooltip <- function(x) {
if (is.null(x)) return(NULL)
if (is.null(x$region)) return(NULL)
# Pick out the movie with this ID
allStates <- isolate(all_states_unique)
state <- allStates[allStates$region == x$region, ]
paste0("<b>", state$region, "</b><br>",
state$Population, "<br>",
state$Income
)
}
all_states %>%
arrange(group, order) %>%
ggvis(x = ~long, y = ~lat) %>%
layer_paths(fill = ~region, stroke := .2) %>%
add_tooltip(states_tooltip, "hover")
})
statesData %>% bind_shiny('map')
}
)