R Shiny:每次定制都需要一致的随机样本
R Shiny: need consistent random sample with each customization
这是我的第一个问题,如果我不遵守网站的任何规范,请提前致歉!
我正在试验 R Shiny。我正在尝试制作一个应用程序,允许某人自定义许多不同发行版和内核估计器的组件。然而,在我继续之前,我的问题是:每次我在我的应用程序中调整一个滑块或其他可自定义的选项时,都会绘制一个全新的样本。例如,如果我想降低我的内核带宽,我该如何使用一个示例而不更改它?
现在我有以下 ui.R
和 server.R
代码:
library(shiny)
shinyUI(bootstrapPage(
headerPanel("Kernel Estimation"),
mainPanel(
sliderInput(inputId = "n_breaks",
label = "Number of bins in histogram (approximate):",
min = 1,
max = 50,
value = 25),
sliderInput(inputId = "observations",
label = "Number of observations:",
min = 10,
max = 1000,
value = 500),
checkboxInput(inputId = "density",
label = strong("Show density estimate"),
value = FALSE),
# Display this only if the density is shown
conditionalPanel(condition = "input.density == true",
sliderInput(inputId = "bw_adjust",
label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)),
conditionalPanel(condition = "input.density == true",
selectInput(inputId = "kernel",
label = "Type of Kernel:",
list("Gaussian" = "gaussian", "Epanechnikov" = "epanechnikov", "Rectangular" = "rectangular", "Triangular" = "triangular", "Biweight" = "biweight", "Optcosine" = "optcosine"))),
checkboxInput(inputId = "individual_obs",
label = strong("Show individual observations"),
value = FALSE),
plotOutput(outputId = "main_plot", height = "300px")
)))
aaaaa 还有:
library(shiny)
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
hist(rnorm(input$observations,mean=0,sd=1),
probability = TRUE,
breaks = as.numeric(input$n_breaks),
xlab = "Observations",
main = "Standard Normal Distribution")
if (input$individual_obs) {
rug(rnorm(input$observations,mean=0,sd=1), col = "red")
}
if (input$density) {
dens <- density(rnorm(input$observations,mean=0,sd=1),
kernel = input$kernel,
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
})
})
产量..哦,废话。我成为会员的时间还不够长,无法 post 图片。我会尝试 post this link,但我不知道这是否会将您带到我的应用程序。
非常感谢您的帮助!
我认为你的问题可以通过像这样在反应函数中生成分布来解决:
get_observations <- reactive(
{
return(rnorm(input$observations,mean=0,sd=1))
})
if (input$individual_obs) {
rug(get_observations(), col = "red")
}
if (input$density) {
dens <- density(get_observations(),
kernel = input$kernel,
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
get_observations
只会在input$observations
改变的时候改变。
这是我的第一个问题,如果我不遵守网站的任何规范,请提前致歉!
我正在试验 R Shiny。我正在尝试制作一个应用程序,允许某人自定义许多不同发行版和内核估计器的组件。然而,在我继续之前,我的问题是:每次我在我的应用程序中调整一个滑块或其他可自定义的选项时,都会绘制一个全新的样本。例如,如果我想降低我的内核带宽,我该如何使用一个示例而不更改它?
现在我有以下 ui.R
和 server.R
代码:
library(shiny)
shinyUI(bootstrapPage(
headerPanel("Kernel Estimation"),
mainPanel(
sliderInput(inputId = "n_breaks",
label = "Number of bins in histogram (approximate):",
min = 1,
max = 50,
value = 25),
sliderInput(inputId = "observations",
label = "Number of observations:",
min = 10,
max = 1000,
value = 500),
checkboxInput(inputId = "density",
label = strong("Show density estimate"),
value = FALSE),
# Display this only if the density is shown
conditionalPanel(condition = "input.density == true",
sliderInput(inputId = "bw_adjust",
label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)),
conditionalPanel(condition = "input.density == true",
selectInput(inputId = "kernel",
label = "Type of Kernel:",
list("Gaussian" = "gaussian", "Epanechnikov" = "epanechnikov", "Rectangular" = "rectangular", "Triangular" = "triangular", "Biweight" = "biweight", "Optcosine" = "optcosine"))),
checkboxInput(inputId = "individual_obs",
label = strong("Show individual observations"),
value = FALSE),
plotOutput(outputId = "main_plot", height = "300px")
)))
aaaaa 还有:
library(shiny)
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
hist(rnorm(input$observations,mean=0,sd=1),
probability = TRUE,
breaks = as.numeric(input$n_breaks),
xlab = "Observations",
main = "Standard Normal Distribution")
if (input$individual_obs) {
rug(rnorm(input$observations,mean=0,sd=1), col = "red")
}
if (input$density) {
dens <- density(rnorm(input$observations,mean=0,sd=1),
kernel = input$kernel,
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
})
})
产量..哦,废话。我成为会员的时间还不够长,无法 post 图片。我会尝试 post this link,但我不知道这是否会将您带到我的应用程序。
非常感谢您的帮助!
我认为你的问题可以通过像这样在反应函数中生成分布来解决:
get_observations <- reactive(
{
return(rnorm(input$observations,mean=0,sd=1))
})
if (input$individual_obs) {
rug(get_observations(), col = "red")
}
if (input$density) {
dens <- density(get_observations(),
kernel = input$kernel,
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
get_observations
只会在input$observations
改变的时候改变。