基于 plotly clicks 在 Shiny 中使用 conditionalPanels
Using conditionalPanels in Shiny based on plotly clicks
是否可以创建一个按钮,该按钮仅在使用条件面板在绘图中选择 point/marker 时出现?也许与此类似?:
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="event_data('plotly_click', source='plot').length > 0",
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)
p <- plot_ly(data=df, source="plot") %>%
add_markers(data=df, x=~x, y=~y, type="scatter", mode = "markers")
# Test the output when a point is selected
print(event_data('plotly_click', source='plot'))
p
})
}
shinyApp(ui, server)
您可以检查 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)
是否可以创建一个按钮,该按钮仅在使用条件面板在绘图中选择 point/marker 时出现?也许与此类似?:
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="event_data('plotly_click', source='plot').length > 0",
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)
p <- plot_ly(data=df, source="plot") %>%
add_markers(data=df, x=~x, y=~y, type="scatter", mode = "markers")
# Test the output when a point is selected
print(event_data('plotly_click', source='plot'))
p
})
}
shinyApp(ui, server)
您可以检查 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)