Скрыть график при изменении кнопки или ползунка в R Shiny - PullRequest
0 голосов
/ 28 января 2019

У меня есть небольшое приложение Shiny, которое генерирует данные при каждом нажатии кнопки New data.Кнопка Show plot показывает скрытый график.Я бы хотел, чтобы график снова автоматически скрывался при каждом нажатии кнопки New data для создания нового набора данных.Бонусом будет за то, что сюжет будет скрыт также, как только слайдер будет изменен.Я не ищу действия переключения.

Я попытался адаптировать этот пример , который использует условную панель, но я не смог понять, как правильно изменить values$show между TRUE иFALSE.

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)

Ответы [ 2 ]

0 голосов
/ 28 января 2019

Альтернативное решение с использованием 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)

enter image description here

0 голосов
/ 28 января 2019

Вы можете использовать реактивное значение и 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...