Shiny Reactivity

前端 未结 2 1461
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 18:47

I\'ve got an application with a large number of parameters. Each parameters has lots of granularity which make finding the desired one a pain. This causes the reactive porti

2条回答
  •  难免孤独
    2020-12-31 19:49

    The answer was given by Joe Cheng in a comment above, but seeing that the OP had difficulty understanding it, I write it out explicitly below, for the record:

    # ui.R
    
    library("shiny")
    shinyUI(
      pageWithSidebar(
        headerPanel("Example")
        ,
        sidebarPanel(
          sliderInput("N", "Number of Samples", min = 2, max = 1000, value = 100)
          ,
          actionButton("action", "Resample")
        )
        ,  
        mainPanel(
          tabsetPanel(      
            tabPanel("Plot", plotOutput("plotSample"))
            ,
            id = "tabs1"
          )
        )
      )
    )
    
    # server.R
    
    library("shiny")
    shinyServer(
      function(input, output, session) { 
        Data <- reactive({
            input$action
            isolate({ 
                return(rnorm(input$N))
                return(x)
            })
        })
      output$plotSample <-renderPlot({
          plot(Data())
        } , height = 400, width = 400
      )
    })
    

    Note that having input$action inside reactive(), where "action" is the actionButton's inputID, is enough to trigger a new rendering of the plot. So you need only one actionButton.

提交回复
热议问题