R 使ggvis图表中的主标题与闪亮的输入交互

R 使ggvis图表中的主标题与闪亮的输入交互,r,shiny,ggvis,R,Shiny,Ggvis,我在我闪亮的应用程序中有一个ggvis图表,我希望它的标题能够与用户输入交互 在这篇文章之后:,我可以在我的图表中添加一个标题,但显然使其具有交互性并不像我想象的那么简单 以下是最小可重复性示例 library(shiny) library(ggvis) example <- data.frame(a=c("apple", "pineapple", "grape", "peach"), b=11:14) ui <- fluidPage ( selectInput(inpu

我在我闪亮的应用程序中有一个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)
库(闪亮)
图书馆(ggvis)

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

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")
   })
}
server%
添加_轴(“x”,orient=“top”,刻度=0,
title=粘贴(“所选选项为”,输入$option),
属性=轴/支柱(
轴=列表(笔划=“白色”),
标签=列表(fontSize=0))%>%
绑定(“图表”)
})
}

observe
将这一切置于“反应上下文”中,允许您在绘图中使用
input$选项。如果没有
observe
shinny,他就不知道如何处理绘图中的反应性。

你可以用粘贴将其作为标题。但是,您需要将
ggvis
部分从
ggivs(…
通过
bind_-shinny
包装到
observe({})
中才能工作。我不明白。您能提供一个代码框架吗?我还没有在shinny中使用observe()。另外,您知道为什么会抛出错误吗?代码不能与add_轴(“x”)一起工作,orient=“top”,ticks=0,title=dataTable()$a[1],这对我来说似乎很奇怪,因为dataTable()$a[1]在通过被动函数时是一个固定字符串。(我也尝试了dataTable$a[1],但没有用)