R 如何在系统中跨选项卡持久保存事件\ U数据?

R 如何在系统中跨选项卡持久保存事件\ U数据?,r,shiny,R,Shiny,我有一个闪亮的应用程序,我想在其中捕获用户单击的条,并将该值存储在反应式表达式中,以便在其他地方引用以进行过滤。问题是,当我切换选项卡时,被动表达式会重新运行,因此两个选项卡之间的值不会同步 下面我有一个可复制的例子 加载应用程序并单击山羊栏时,底部的选择将更改为“山羊”,但如果随后将选项卡更改为Bar2,则反应式表达式将重新运行,因此将再次返回长颈鹿。因此,我在不同的选项卡上为反应式表达式提供了两个单独的值。如果我在第一个选项卡上选择山羊,我希望在切换到Bar2选项卡时它保持不变,并且仅在再次

我有一个闪亮的应用程序,我想在其中捕获用户单击的条,并将该值存储在反应式表达式中,以便在其他地方引用以进行过滤。问题是,当我切换选项卡时,被动表达式会重新运行,因此两个选项卡之间的值不会同步

下面我有一个可复制的例子

加载应用程序并单击山羊栏时,底部的选择将更改为“山羊”,但如果随后将选项卡更改为Bar2,则反应式表达式将重新运行,因此将再次返回长颈鹿。因此,我在不同的选项卡上为反应式表达式提供了两个单独的值。如果我在第一个选项卡上选择山羊,我希望在切换到Bar2选项卡时它保持不变,并且仅在再次单击时更新

请注意,在本例中,我意识到可以通过从event_data函数中删除source参数来解决此问题,但在我的应用程序中,我有其他图表,我不希望用户能够单击这些图表,因此我需要将源设置为仅这些图表

library(shiny)
library(plotly)
library(ggplot2)
library(shinydashboard)

df_test <- data.frame(c("Giraffes","Goats"),c(1,4))
df_test <- setNames(df_test,c("species","amount"))

ui <- dashboardPage(

  dashboardHeader(title = "Click Example",
                  titleWidth = 300),
  dashboardSidebar(
    width = 300,
    sidebarMenu(
      menuItem("Tab", tabName = "tab")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab",
              fluidRow(
                column(12, tabBox(
                  title = "",
                  id = "tabSet",
                  width = 12,
                  height = 500,
                  tabPanel("Bar1", plotlyOutput(outputId="bar_one")),
                  tabPanel("Bar2", plotlyOutput(outputId="bar_two"))
                )
                ),
                column(12,textOutput(outputId = "selection")))
      )
    )
  )
)

server <- function(input, output, session) {

  click_reactive = reactive({
    d <- event_data("plotly_click",source=input$tabSet)
    if (length(d) == 0) {species = as.vector(df_test$species[1])}
    else {species = as.character(d[4])}
    return(species)
  })

  output$bar_one <- renderPlotly({
    p <- plot_ly(data = df_test, x = ~amount, y = ~species, type = 'bar', orientation = 'h', source = "Bar1")
  })

  output$bar_two <- renderPlotly({
    p <- plot_ly(data = df_test, x = ~amount, y = ~species, type = 'bar', orientation = 'h', source = "Bar2")
  })


  output$selection <- renderText({
    species <- click_reactive()
    return(species)
  })

}

shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
图书馆(GG2)
图书馆(shinydashboard)

df_test您需要将
源代码更改为一个名称:

library(shiny)
library(plotly)
library(ggplot2)
library(shinydashboard)

df_test <- data.frame(c("Giraffes","Goats"),c(1,4))
df_test <- setNames(df_test,c("species","amount"))

ui <- dashboardPage(

  dashboardHeader(title = "Click Example",
                  titleWidth = 300),
  dashboardSidebar(
    width = 300,
    sidebarMenu(
      menuItem("Tab", tabName = "tab")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab",
              fluidRow(
                column(12, tabBox(
                  title = "",
                  id = "tabSet",
                  width = 12,
                  height = 500,
                  tabPanel("Bar1", plotlyOutput(outputId="bar_one")),
                  tabPanel("Bar2", plotlyOutput(outputId="bar_two"))
                )
                ),
                column(12,textOutput(outputId = "selection")))
      )
    )
  )
)

server <- function(input, output, session) {

  v <- reactiveValues()

  observe({
    d <- event_data("plotly_click",source="Bar1")
    if (length(d) == 0) {species = as.vector(df_test$species[1])}
    else {species = as.character(d[4])}
    v$click <- species
  })

  output$bar_one <- renderPlotly({
    p <- plot_ly(data = df_test, x = ~amount, y = ~species, type = 'bar', orientation = 'h', source = "Bar1")
  })

  output$bar_two <- renderPlotly({
    p <- plot_ly(data = df_test, x = ~amount, y = ~species, type = 'bar', orientation = 'h', source = "Bar1")
  })

  output$selection <- renderText({
    v$click
  })

}

shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
图书馆(GG2)
图书馆(shinydashboard)

我要试试这个!谢谢你,汉克猪排,这个有用!我甚至没有想到,你可以使用同一个来源进行多个绘图,但这是有道理的。您选择使用“观察”和“反应”值而不仅仅是“反应”值有什么原因吗?我想我希望您在监视单击事件时具有更大的灵活性,所以我使用了“反应”值。我使用了观察,因为我不是100%地观察到两个人的反应,所以我想同时捕捉这两个人