如何让这些 ShinyApp 参数成为“反应式"?输入更改 [英] How to get these shinyApp parameters to be "reactive" to input changes

查看:51
本文介绍了如何让这些 ShinyApp 参数成为“反应式"?输入更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的 ShinyApp,它绘制正态分布,给出两个分位数(lbv 和 ubv),对应于 5% 和 95% 的概率(90% 的置信区间).分位数是用户定义的输入.

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.

为了获得普通 PDF 的均值和标准差,我使用了 rriskDistributions 包中的 get.norm.par(),如下所示:

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]

如何让 ShinyApp 对 UI 中的输入变化做出反应?我是 Shiny 的新手 - 似乎我必须使用 react() 和 refresh(?) 但我不知道在哪里/如何使用它.任何提示将不胜感激.

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.

下面的代码生成了 ShinyApp,但它不会对用户定义的输入变化做出反应".

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)

我尝试根据示例进行以下更改 here,我可以说它行不通,但不确定要进行哪些更改....

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"))

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)
})

推荐答案

我们需要让依赖于input$的对象变成reactive的,见下图:

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)

这篇关于如何让这些 ShinyApp 参数成为“反应式"?输入更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆