How to get these shinyApp parameters to be “reacti

2019-07-28 00:21发布

问题:

I'm building a simple shinyApp that plots a normal distribution given two quantiles (lbv and ubv) corresponding to 5% and 95% probability (a 90% confidence interval). The quantiles are user-defined inputs.

To get the mean and sd of the normal PDF, I'm using get.norm.par() from the rriskDistributions package, like so:

dpar <- get.norm.par(p=c(0.05,0.95),q=c(lbv,ubv),plot=F)
mean <- dpar[1]
sd <- dpar[2]

How do I get the shinyApp to react to input changes in the UI? I'm new to Shiny - it seems I have to use reactive() and refresh(?) but I can't make sense of where/how to use it. Any tips would be appreciated.

The code below generates the shinyApp but it is not "reactive" to changes in user-defined inputs.

# Set libraries
library(shiny)
library(rriskDistributions)

# Global variables can go here
lb <- 0.05
ub <- 0.95
lbv <- 200
ubv <- 1000
dpar <- get.norm.par(p=c(lb,ub),q=c(lbv,ubv),plot=F)
mean <- dpar[1]
sd <- dpar[2]

x1 <- lbv - (ubv-lbv)/2 # set my x-axis left bound
x2 <- ubv + (ubv-lbv)/2 # set my x-axis right bound

xseq<-seq(x1,x2,.1)

densities<-dnorm(xseq, mean,sd)

ui <- fluidPage(
        titlePanel("Parameters"),
        sidebarLayout(
                sidebarPanel(
                        numericInput('lbv', 'Lower Bound Value', lbv),
                        numericInput('ubv', 'Upper Bound Value', ubv)

                ),
                mainPanel(
                        plotOutput('plot')
                )
        )
)

server <- function(input, output) {    
        output$plot <- renderPlot({
        plot(xseq, densities, col="darkgreen", xlab="", ylab="Density", type="l",lwd=2, cex=2, main="Normal Density", cex.axis=.8)
        })
}

shinyApp(ui = ui, server = server)

I've tried making the following changes based on an example here, and I can tell it won't work but not sure what changes to make....

 sidebarPanel(
                      numericInput('lbv', 'Lower Bound Value', lbv),
                      numericInput('ubv', 'Upper Bound Value', ubv),
                      actionButton(inputId = "refresh", label = "Refresh" , 
                                      icon = icon("fa fa-refresh"))

and

dataInput <- reactive({
    get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)
    mean <- get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)[1]
    sd <- get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)[2]

    x1 <- input$lbv - (input$ubv-input$lbv)/2 # set my x-axis left bound
    x2 <- input$ubv + (input$ubv-input$lbv)/2 # set my x-axis right bound

    xseq<-seq(x1,x2,.1)

    densities<-dnorm(xseq, mean,sd)
})

回答1:

We need to make objects that rely on input$ reactive, see below:

# Set libraries
library(shiny)
library(rriskDistributions)

# Global variables can go here
lb <- 0.05
ub <- 0.95


ui <- fluidPage(
  titlePanel("Parameters"),
  sidebarLayout(
    sidebarPanel(
      numericInput('lbv', 'Lower Bound Value', 200),
      numericInput('ubv', 'Upper Bound Value', 1000)
    ),
    mainPanel(
      plotOutput('plot')
    )
  )
)

server <- function(input, output) {    

  xseq <- reactive({
    x1 <- input$lbv - (input$ubv - input$lbv)/2 # set my x-axis left bound
    x2 <- input$ubv + (input$ubv - input$lbv)/2 # set my x-axis right bound
    # return
    seq(x1, x2, 0.1)
  })

  densities <- reactive({
    dpar <- get.norm.par(p = c(lb, ub), q = c(input$lbv, input$ubv), plot = FALSE)
    mean <- dpar[1]
    sd <- dpar[2]
    # return
    dnorm(xseq(), mean, sd)
  })

  output$plot <- renderPlot({
    plot(xseq(), densities(),
         col = "darkgreen", xlab="", ylab="Density", type="l",lwd=2, cex=2,
         main="Normal Density", cex.axis=.8)
  })
}

shinyApp(ui = ui, server = server)