【问题标题】:How do I update a plot generated on button push after abline input如何在 abline 输入后更新按钮按下时生成的图
【发布时间】:2022-01-07 05:02:09
【问题描述】:

假设我有下面的应用程序(提供的示例代码),我想将其用于以下目的:

  • 绘制数据直方图
  • 设置阈值并将 abline 添加到绘图中
  • 设置阈值应生成一个表,其中包含所有高于阈值的值
  • 设置新阈值会更新表格,将新的 abline 添加到绘图中,并且不会使绘图消失。

如果您有任何好的建议,我将非常感激,因为我一直在关注 Shiny 中的反应性问题。


data <- as.data.frame(iris3)

ui <- fluidPage(
  
  sliderInput("ab", "Threshold", min = 0, max = 10, value = 2),
  
  actionBttn(color = "primary",
    inputId = "show",
    label = "Plot",
    style = "stretch",
    size = "lg"
  ),
  
  plotOutput("p"),
  
  DT::dataTableOutput("t")
)

server <- function(input, output, session) {
  
  plot <- eventReactive(input$show, {
    hist(data$`Sepal L..Versicolor`)
  })
  
  output$p <- renderPlot({
    plot()
    ab()
  })
  
  ab <- eventReactive(input$ab, {
    abline(h = input$ab)
  })
  
  output$t <- DT::renderDataTable({
    data[data$`Sepal L..Versicolor` > input$ab,]
  })
}

shinyApp(ui, server)```

【问题讨论】:

  • 我不明白你的程序有什么问题?

标签: r shiny


【解决方案1】:

如果使用“Plot”按钮更新 abline,请将 abline 放入 eventReactive()

data <- as.data.frame(iris3)

ui <- fluidPage(
  
  sliderInput("ab", "Threshold", min = 0, max = 10, value = 2),
  
  actionButton(color = "primary",
             inputId = "show",
             label = "Plot",
             style = "stretch",
             size = "lg"
  ),
  
  plotOutput("p"),
  
  DT::dataTableOutput("t")
)

server <- function(input, output, session) {
  
  plot <- eventReactive(input$show, {
    hist(data$`Sepal L..Versicolor`)
    abline(h = input$ab, col = 'blue')
  })
  
  output$p <- renderPlot({
    plot()
    
  })
  
  output$t <- DT::renderDataTable({
    data[data$`Sepal L..Versicolor` > input$ab,]
  })
}

shinyApp(ui, server)

附:如果“Plot”按钮只更新abline,我建议你去掉按钮,使用滑动条直接更新abline和table。

data <- as.data.frame(iris3)

ui <- fluidPage(
  
  sliderInput("ab", "Threshold", min = 0, max = 10, value = 2),
  
  plotOutput("p"),
  
  DT::dataTableOutput("t")
)

server <- function(input, output, session) {
  
  plot <- reactive({
    hist(data$`Sepal L..Versicolor`)
    abline(h = input$ab, col = 'blue')
  })
  
  output$p <- renderPlot({
    plot()
    
  })
  
  output$t <- DT::renderDataTable({
    data[data$`Sepal L..Versicolor` > input$ab,]
  })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多