SO 有很多闪亮的问题询问如何在 UI 中使用输入值。一般答案是使用updateSelect
-家庭或使用renderUI
.
我知道基本上不可能从输入列表中访问值(input$...
)在 UI 中,因为输入列表仅传递到服务器,因此不存在于 UI 环境中。
我不明白的是,有这样的功能conditionalPanel
它访问 JavaScript 中的输入列表并使用它来呈现 UI 输出。
由于这些函数实际上使用了 javascript 中的输入列表,我想知道是否可以构建一些闪亮的函数(让我们将其命名为js()
) 将 javascript 输入列表传递回 R 并使其可以在 UI 中访问。
这将使创建用户界面变得更加容易。
我试图更深入地研究conditionalPanel
函数以更好地理解如何访问 javascript 环境,但条件参数被传递给div()
名为“data-display-if”的属性。从这里开始我不知道如何深入挖掘该功能。
conditionalPanel <- function(condition, ..., ns = NS(NULL)) {
div(`data-display-if`=condition, `data-ns-prefix`=ns(""), ...)
}
最后,我提供了一个功能性闪亮仪表板的代码示例。我注释掉了两条语句:一条不起作用,一条具有虚构功能js()
访问 javascript 中的输入值并将其传递给 R。将来如果有这样的函数就太好了。
library("shiny")
library("data.table")
# Generate data
testDT <- data.table(a1 = c(rep("group1",4),rep("group2",4),rep("group3",4)),
a2 = rep(c("red","blue","green"),4),
x1 = c(5,6,7,3,4,5,2,3,4,2,1,7),
x2 = c(1,2,3,2,3,2,1,4,6,7,3,4),
x3 = c(12,43,64,34,93,16,32,74,84,89,45,67)
)
shinyApp(
ui = fluidPage( # user interface
sidebarLayout( # layout with Sidebar
sidebarPanel( # input sidebarPanel
selectInput(inputId = "subset", label = "Choose group",
choices = c("Group 1" = "group1",
"Group 2" = "group2",
"Group 3" = "group3"),
selected = "group1"),
conditionalPanel(
condition = "input.subset == 'group3'", # input value can be access in javascript
sliderInput(inputId = "range x3", label = "Choose range for x3",
min = 0,
max = max(testDT$x3),
# max = max(testDT[a1 == input$subset]$x3), not possible since input list not available in UI
# max = max(testDT[a1 == js('input.subset')]$x3), a function like js() would be great
c(0,max(testDT$x3)),
step = 1))
), # closes sidebarPanel
mainPanel( # Output in mainPabel
tableOutput("table")
) # closes mainPanel
) # closes sidebarLayout
), # closes fluidPage
server = function(input, output) {
react_testDT <- reactive({
testDT[a1 == input$subset,.(x1_mean = mean(x1),
x2_mean = mean(x2),
x3_mean = mean(x3))]
})
output$table <- renderTable({
react_testDT()
})
}
)