简体   繁体   English

当“ R Shiny”中的动作按钮或滑块更改时,隐藏图

[英]Hide plot when action button or slider changes in R Shiny

I have a small Shiny app that generates some data whenever the New data button is pressed. 我有一个小的Shiny应用程序,每当按下“ New data按钮时,该应用程序都会生成一些数据。 The Show plot button shows a hidden plot. Show plot按钮显示隐藏的图。 I would like the plot to be hidden again automatically whenever the New data button is pressed to make a new data set. 我希望每当按下“ New data按钮以创建新数据集时,图都会自动再次隐藏。 A bonus would be for the plot to be hidden also as soon as the slider is changed. 额外的好处是,一旦更改滑块,就可以隐藏该图。 I am not looking for a toggle action. 我不是在寻找切换动作。

I tried adapting this example that uses conditional panel but I could not successfully figure out how to correctly change the values$show between TRUE and FALSE . 我尝试改编使用条件面板的此示例 ,但无法成功找出如何在TRUEFALSE之间正确更改values$show

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "number",
                  label = "Pick a number",
                  min = 6,
                  max = 12,
                  value = 8),
      actionButton("new_data",
                   "New data"),
      actionButton("show_plot",
                   "Show plot")
    ),
    mainPanel(
      tableOutput("char_table"),
      plotOutput(outputId = "car_plot")

    )
  )
)

server <- function(input, output) {

  t <- eventReactive(input$new_data, {
    r <- input$number
    c <- r - 1
    mat <- matrix(sample(0:1,r*c, replace=TRUE),r,c)
  })

  output$char_table <- renderTable({
    t()
  })

  p <- eventReactive(input$show_plot, {
    plot(cars)
  })
  output$car_plot <- renderPlot({
    p()
  })
}

shinyApp(ui = ui, server = server)

You can use a reactive value and a if to control the plot. 您可以使用反应值和if来控制绘图。

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "number",
                  label = "Pick a number",
                  min = 6,
                  max = 12,
                  value = 8),
      actionButton("new_data",
                   "New data"),
      actionButton("show_plot",
                   "Show plot")
    ),
    mainPanel(
      tableOutput("char_table"),
      plotOutput(outputId = "car_plot")

    )
  )
)

server <- function(input, output) {
  showPlot <- reactiveVal(FALSE)

  t <- eventReactive(input$new_data, {
    showPlot(FALSE)
    r <- input$number
    c <- r - 1
    mat <- matrix(sample(0:1,r*c, replace=TRUE),r,c)
  })

  output$char_table <- renderTable({
      t()
  })

  observeEvent(input$number, {
    showPlot(FALSE) 
  })

  observeEvent(input$show_plot, {
    showPlot(TRUE) 
  })

  output$car_plot <- renderPlot({
    if (showPlot())
      plot(cars)
  })
}

shinyApp(ui = ui, server = server)

Alternate solution using shinyjs which is handy in these situations. 使用shinyjs替代解决方案在这些情况下很方便。

library(shiny)
library(shinyjs)

ui <- fluidPage( shinyjs::useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "number",
                  label = "Pick a number",
                  min = 6,
                  max = 12,
                  value = 8),
      actionButton("new_data",
                   "New data"),
      actionButton("show_plot",
                   "Show plot")
    ),
    mainPanel(
      tableOutput("char_table"),
      plotOutput(outputId = "car_plot")

    )
  )
)

server <- function(input, output) {

  t <- eventReactive(input$new_data, {
    hide("car_plot")
    r <- input$number
    c <- r - 1
    mat <- matrix(sample(0:1,r*c, replace=TRUE),r,c)
  })

  output$char_table <- renderTable({
    t()
  })

  observeEvent(input$show_plot, {
    show("car_plot")
  })
  output$car_plot <- renderPlot({
    plot(cars)
  })
}

shinyApp(ui = ui, server = server)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM