闪亮的应用程序中的“清除”按钮不清除图表

时间:2018-03-26 15:27:53

标签: r shiny

我最近开始研究闪亮。使用以下代码,当调用shinyApp(ui, server)并打开闪亮的应用程序时,按钮1和5可以工作,但我无法通过“清除”按钮清除这些图。如何正确实现“清除”按钮?

library(MASS)
library(shiny)
ui <- fluidPage(
   titlePanel("Machine Learning"),
   sidebarLayout(
     sidebarPanel("Tasks"),
     mainPanel("Regression")
   ),
   actionButton("btn1","1"),
   actionButton("btn2","2"),
   actionButton("btn3","3"),
   actionButton("btn4","4"),
   actionButton("btn5","5"),
   actionButton("reset","Clear"),
   plotOutput("task1"),
   plotOutput("task5")
 )
 server <- function(input, output){
   task1<-reactiveValues(data=NULL)
   observeEvent(input$btn1, {
     output$task1 <- renderPlot({
       air = airquality [, c("Ozone", "Solar.R", "Wind", "Temp")]
       f = lm.ridge (Ozone ~ ., data = air, subset = !is.na(Solar.R) & !is.na(Ozone), lambda = seq (1, 10000, by = 10))
       plot (f)
     })
   })
   observeEvent(input$btn5, {
     output$task5 <- renderPlot({
       head(longley)
       a = longley[,c("GNP.deflator","GNP", "Unemployed","Armed.Forces","Year","Employed")]
       head(a)
       length(a$GNP)
       a1 = a[1:8,]
       a2 = a[9:16,]
       l = numeric(26)
       for(i in seq (0,25)){
         l[i+1] = 10^(-3 + 0.2*i)
       }
       l
       f = lm.ridge (GNP ~ ., data = a1, subset = !is.na(GNP), lambda = l)
       plot(f)
       summary (f)
     })
   })
 }
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?如果您想要,请务必使用shinyjs

library(shiny)
library(MASS)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  titlePanel("Machine Learning"),
  sidebarLayout(
    sidebarPanel("Tasks"),
    mainPanel("Regression")
  ),
  actionButton("btn1","1"),
  actionButton("btn2","2"),
  actionButton("btn3","3"),
  actionButton("btn4","4"),
  actionButton("btn5","5"),
  actionButton("reset","Clear"),
  plotOutput("task1"),
  plotOutput("task5")
)
server <- function(input, output){

  tasks <- reactiveValues(data=NULL)

  observeEvent(input$btn1,{
    show("task1")
    air = airquality [, c("Ozone", "Solar.R", "Wind", "Temp")]
    f = lm.ridge (Ozone ~ ., data = air, subset = !is.na(Solar.R) & !is.na(Ozone), lambda = seq (1, 10000, by = 10))
    tasks$d1 <- f
  })

  observeEvent(input$reset,{
    hide("task1")
    hide("task5")
    tasks$d1 <- NULL
    tasks$d5 <- NULL
  })

  output$task1 <- renderPlot({
    if(is.null(tasks$d1)){
      return()
    }
    plot(tasks$d1)
  })

  observeEvent(input$btn5,{
    show("task5")
    a = longley[,c("GNP.deflator","GNP", "Unemployed","Armed.Forces","Year","Employed")]
    head(a)
    length(a$GNP)
    a1 = a[1:8,]
    a2 = a[9:16,]
    l = numeric(26)
    for(i in seq (0,25)){
      l[i+1] = 10^(-3 + 0.2*i)
    }
    l
    f = lm.ridge (GNP ~ ., data = a1, subset = !is.na(GNP), lambda = l)
    tasks$d5 <- f
  })

  output$task5 <- renderPlot({
    if(is.null(tasks$d5)){
      return()
    }
    plot(tasks$d5)
  })


}
shinyApp(ui, server)

enter image description here