如何让动作按钮刷新情节?

时间:2017-03-21 12:08:45

标签: r shiny

我希望绘图仅在以下情况下刷新:更改滑块并按动作按钮。在改变radioButton之后,我不希望剧情重新演绎。我怎样才能做到这一点?我试过这个:

library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))


ui <- fluidPage(
  sliderInput("slider1","slider1", value=5, min=1, max=10),
  sliderInput("slider2","slider2", value=0.8, min=0, max=1),
  radioButtons("check1","check1",choices = c("red","blue","green")),
  actionButton("refreshColours","refreshColours"),
  plotOutput("rys")
)


server <- function(input,output){

  pp <- reactive({
    ggplot(dt, aes(x,y)) + 
      geom_point(size=input$slider1, alpha=input$slider2, colour=isolate(input$check1))
  })

  pp <- eventReactive(input$refreshColours{pp})

  output$rys <- renderPlot({
    pp()
  })

}


shinyApp(ui=ui, server=server)

但是我不知道如何在按下操作按钮后刷新情节。

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题

library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(1000), y=runif(1000))

ui <- fluidPage(
  sliderInput("slider1","slider1", value=5, min=1, max=10),
  sliderInput("slider2","slider2", value=0.8, min=0, max=1),
  radioButtons("check1","check1",choices = c("red","blue","green")),
  actionButton("refreshColours","refreshColours"),
  plotOutput("rys")
)


server <- function(input,output){
  pp <- eventReactive(c(input$refreshColours,input$slider2,input$slider1),{
    ggplot(dt, aes(x,y)) + 
      geom_point(size=input$slider1, alpha=input$slider2, colour=isolate(input$check1))
  })
  output$rys <- renderPlot({
    pp()
  })
}

shinyApp(ui=ui, server=server)