根据绘图点击在Shiny中使用conditionalPanels

时间:2018-08-20 16:41:46

标签: r shiny conditional plotly

是否可以创建一个仅在使用条件面板在绘图中选择了点/标记时才显示的按钮?也许与此类似?:

#include <stdio.h>
#include <inttypes.h>

int main(void)
{
    printf("%" PRIx32, (uint32_t)-1 << 4);
}

1 个答案:

答案 0 :(得分:1)

您可以检查reactive内部的条件,然后将结果发送到conditionalPanel

## context: server.R
output$condition <- reactive({
  length(event_data('plotly_click', source='plot')) > 0
})
outputOptions(output, "condition", suspendWhenHidden = FALSE)

## context: ui.R
conditionalPanel(condition = "output.condition", ...)

如果您想知道为什么需要致电outputOptions,请参阅this question

这是该应用的完整版本

library(plotly)

ui <- fluidPage(
  titlePanel("Conditional panels"),
  column(2, wellPanel(
    sliderInput("n", "Number of points:",
                min = 10, max = 200, value = 50, step = 10))),

  column(5, plotlyOutput("scatterPlot", height = 300)), 
  column(5,conditionalPanel(
    condition="output.condition", 
    downloadButton("mod_pdf", "Download Plots as PDF"))
  )
)

server <- function(input, output) {

  output$scatterPlot <- renderPlotly({
    x <- rnorm(input$n)
    y <- rnorm(input$n)
    df <- data.frame("x"=x, "y"=y)

    plot_ly(data=df, source="plot") %>%
      add_markers(data=df, x=~x, y=~y, type="scatter", mode = "markers")
  })

  output$condition <- reactive({
    length(event_data('plotly_click', source='plot')) > 0
  })
  outputOptions(output, "condition", suspendWhenHidden = FALSE)
}

shinyApp(ui, server)