Нажмите на несколько графиков в каждой итерации в блестящем приложении - PullRequest
0 голосов
/ 29 августа 2018

В моем приложении Shiny я использую цикл for для создания двух разных графиков за одну итерацию, и я бы хотел, чтобы пользователь мог просматривать каждую из них по отдельности.

Переменная count ведет подсчет количества нажатий кнопки, однако при каждом щелчке создаются два графика, и отображается только тот, который был отображен последним.

Как бы я мог показать каждый график, используя кнопку действия?

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  output$plot <- renderPlot(plot(df[[1]],df[[2]]))        

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw the plot if count is less than 6
      output$plot <- renderPlot(plot(df[[1]],df[[count]],main = count))
      output$plot <- renderPlot(plot(df[[3]],df[[count]],main = count))
    }
    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  plotOutput("plot")
)

shinyApp(ui = ui, server = server)

1 Ответ

0 голосов
/ 30 августа 2018

Как сказал Грегор де Киллиа, вы можете использовать разные сюжеты.

Редактировать 1: Показать только сюжет1

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  # Make two different plots here
  output$plot1 <- renderPlot(plot(df[[1]],df[[2]]))        
  output$plot2 <- renderPlot(plot(df[[3]],df[[1]])) 

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw the plot if count is less than 6
      # Update both these plots
      output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))
      # Generate this plot but do not output
      plot2 <- reactive({
        renderPlot(plot(df[[3]],df[[count]],main = count))
      })
    }
    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  # Output both plots separately
  plotOutput("plot1")
  # Don't show this
  # plotOutput("plot2")
)

shinyApp(ui = ui, server = server)

Редактировать 2: На основе обновленного понимания, вот как вы можете достичь того, что требуется:

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  output$plot1 <- renderPlot(plot(df[[1]],df[[2]]))

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw plot if count < 6

      # In every iteration, first click is an odd number,
      # for which we display the first plot

      if(count %% 2 != 0){
        # Draw first plot if count is odd
        output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))  
      }
      else{
        # Second click is an even number, 
        # so we display the second plot in the previous iteration,
        # hence df[[count - 1]]

        # Draw second plot if count is even
        output$plot1 <- renderPlot(plot(df[[3]],df[[count-1]],main = count))
      }
    }

    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  # Output plot
  plotOutput("plot1")
)

shinyApp(ui = ui, server = server)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...