r - 我的闪亮代码的功能(单独的面板过滤器并为 radioButtons 选项插入 popify)
问题描述
朋友们我有两个问题希望得到您的帮助:首先,我想将我的两个过滤器和面板上的滑块输入分开。他们都在一起。我想让它们用一些水平线或一个特定的标签分开,使这种分离。第二个是我使用 popify 来描述使用的过滤器。在这种情况下,我使用单选按钮。但是,我想单独为过滤器选项做这件事。例如,对于过滤器 1,我还想为“所有属性”和“排除属性”选项插入 popify。那么,为同一个过滤器分开三个popify,这可能吗?可执行代码如下。
library(shinyBS)
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
popify(
radioButtons("filter1", h3("Select properties"),
choices = list("All properties" = 1,
"Exclude properties" = 2),
selected = 1),
title= "Select Proprierties",
content = paste0("Filter 1 refers to.....")),
popify(
radioButtons("filter2", h3("Select farms"),
choices = list("All farms" = 1,
"Exclude farms" = 2),
selected = 1),
title= "Select farms",
content = paste0("Filter 2 refers to.....")),
popify(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 20,
value = 30),
title = "Number of bins",
content = paste0("Number of bins refers to.....")),
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
非常感谢你!!
解决方案
这是一种方法。它使用 JavaScript 为每个单选按钮分配一个 ID。然后addPopover
就可以使用了。
js <- "
$(document).ready(function(){
$('#dist input[type=radio]~span').each(function(i, $el){
$(this).attr('id', 'dist' + i);
});
});
"
library(shiny)
library(shinyBS)
ui <- fluidPage(
tags$head(tags$script(HTML(js))),
radioButtons("dist", "Distribution type:",
c("Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp")),
plotOutput("distPlot"),
popify(span(), title = "") # an empty shinyBS component, in order to use addPopover
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm)
hist(dist(500))
})
addPopover(
session,
id = "dist0",
title = "Normal distribution",
content = "It is also called the Gaussian distribution",
placement = "right"
)
addPopover(
session,
id = "dist1",
title = "Uniform distribution",
content = "All outcomes are equally probable",
placement = "right"
)
addPopover(
session,
id = "dist2",
title = "Log-normal distribution",
content = "The exponential of the normal distribution",
placement = "right"
)
addPopover(
session,
id = "dist3",
title = "Exponential distribution",
content = "It is memoryless",
placement = "right"
)
}
shinyApp(ui, server)
您必须根据inputId
您的单选按钮组更改 JS 代码,并将生成的 id 的前缀设置为您想要的前缀:
js <- "
$(document).ready(function(){
$('#YOUR_inputId_GOES_HERE input[type=radio]~span').each(function(i, $el){
$(this).attr('id', 'THE_PREFIX_YOU_WANT' + i);
});
});
"
这生成了 ids THE_PREFIX_YOU_WANT0
,THE_PREFIX_YOU_WANT1
等。用于addPopover
:
addPopover(session, id = "THE_PREFIX_YOU_WANT0", ......)
推荐阅读
- kotlin - 如何在 build.gradle.kts 中访问 gradle 的 --test 属性
- git - .gitignore 似乎没有忽略文件
- sql-server - 如何在不同的服务器上触发备份创建
- c# - 控制台应用程序具有与作为服务不同的处理结果
- ruby-on-rails - Shopify 应用陷入重定向循环
- java - Java Spring - 无法将图像保存到静态文件夹
- javascript - DataTable + AngularJS - 在点击属性上传递数据属性
- django - 无法从另一个模型中获取图像字段“'ImageFileDescriptor' 类型的 /api/users/profile 参数的 TypeError 不可迭代”
- c++ - LNK1181:无法打开输入文件“QtXlsx.lib”
- angular - 如何在 Angular 的下拉菜单中使用 Show Class