【问题标题】:making a main title in ggvis chart interactive with Shiny input使 ggvis 图表中的主标题与 Shiny 输入交互
【发布时间】: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() ......另外,你知道为什么会抛出错误吗?

标签: r shiny ggvis


【解决方案1】:

对于您的服务器,请执行以下操作:

server <- function(input, output) {
    dataTable <- reactive({
        df <- example[example$a==input$option, ]
        df
    })
    observe({
      ggvis(data=dataTable, x=~a, y=~b) %>%
        layer_bars() %>%
        add_axis("x", title="fruits") %>%
        add_axis("x", orient = "top", ticks = 0, 
                 title = paste("chosen option is", input$option),
                 properties = axis_props(
                     axis = list(stroke = "white"),
                     labels = list(fontSize = 0))) %>%
        bind_shiny("chart")
   })
}

observe 将这一切置于“反应式上下文”中,允许您在情节中使用input$option。没有observe shiny 不明白如何处理情节中的反应性。

【讨论】:

  • 该代码也不适用于 add_axis("x", orient = "top", ticks = 0, title = dataTable()$a[1]。这对我来说似乎很奇怪,因为dataTable()$a[1] 是一个固定的字符串,一旦它通过响应函数运行。(我也尝试过 dataTable$a[1] 无济于事)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
相关资源
最近更新 更多