【发布时间】:2016-05-24 16:49:05
【问题描述】:
我的闪亮应用中有一个 ggvis 图表,我希望其标题与用户输入进行交互。
在这篇文章之后:Add a plot title to ggvis,我能够为我的图表添加一个标题,但显然让它具有交互性并不像我想象的那么简单。
以下是最小的可重现示例。
library(shiny)
library(ggvis)
example <- data.frame(a=c("apple", "pineapple", "grape", "peach"), b=11:14)
ui <- fluidPage (
selectInput(inputId="option",
label=NULL,
choices= levels(example$a),
selected="apple"),
ggvisOutput("chart")
)
server <- function(input, output) {
dataTable <- reactive({
df <- example[example$a==input$option, ]
df
})
ggvis(data=dataTable, x=~a, y=~b) %>%
layer_bars() %>%
add_axis("x", title="fruits") %>%
add_axis("x", orient = "top", ticks = 0, title = "???", #I want: paste("chosen option is", input$option),
properties = axis_props(
axis = list(stroke = "white"),
labels = list(fontSize = 0))) %>%
bind_shiny("chart")
}
shinyApp(ui=ui, server=server)
感谢您的帮助。
【问题讨论】:
-
您可以使用粘贴将其作为您想要的标题。但是,您需要将
ggivs(...中的ggvis部分通过bind_shiny包裹在observe({})中才能正常工作。 -
我不明白。你能提供一个代码框架吗?我还没有在 Shiny 中使用过 observe() ......另外,你知道为什么会抛出错误吗?