gpt4 book ai didi

javascript - Shiny - 动态生成的输入

转载 作者:行者123 更新时间:2023-12-02 20:56:19 28 4
gpt4 key购买 nike

在我 Shiny 的代码中,我正在动态生成输入,并且我试图使这些输入中的任何变化触发observe()。

我找到了这个链接Shiny - observe() triggered by dynamicaly generated inputs这非常有用,但不适用于所有类型的输入。我在链接中的代码中添加了额外的输入这就是代码:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(title = ""),
dashboardSidebar(
),
dashboardBody(
tags$script("$(document).on('change', '.dynamicSI select', function () {
Shiny.onInputChange('lastSelectId',this.id);
// to report changes on the same selectInput
Shiny.onInputChange('lastSelect', Math.random());
});"),



numericInput("graph_tytle_num","Number of Graph Title elements",value = 1,min = 1,max = 10),
uiOutput("graph_title"),
plotOutput("plot")
)
)

server <- function(input, output, session) {

#elements of graphic titles
output$graph_title <- renderUI({
buttons <- as.list(1:input$graph_tytle_num)
# use a div with class = "dynamicSI" to distinguish from other selectInput's
div( class = "dynamicSI",
lapply(buttons, function(i)
column(3,
selectInput(inputId = paste0("title1_element",i),
label = paste("Title element",i),
choices = paste0(LETTERS[i],seq(1,i*2)),
selected = 1),
radioButtons(inputId = paste0("title2_element",i),
label = paste("Title1 element",i),
choices = c("Yes","No"),
selected = "Yes"),
numericInput(inputId = paste0("title3_element",i),
label = paste("Title element",i),value=1),
dateInput(inputId = paste0("title4_element",i),
label = paste("Title element",i),
value="1900-01-01")
)
)
)
})

# react to changes in dynamically generated selectInput's
observe({
input$lastSelect

if (!is.null(input$lastSelectId)) {
cat("lastSelectId:", input$lastSelectId, "\n")
cat("Selection:", input[[input$lastSelectId]], "\n\n")
}

isolate({ #I dont want to have the numericInput input$graph_tytle_num to be a trigger
#Create the graph title
title <- c()
for(i in 1:input[["graph_tytle_num"]]){
title <- paste(title,input[[paste0("title1_element",i)]],input[[paste0("title2_element",i)]],
input[[paste0("title3_element",i)]],input[[paste0("title4_element",i)]])
}

output$plot <-renderPlot({hist(rnorm(100,4,1),
breaks = 10,
main = title)})
})

})

}

shinyApp(ui, server)

如果我将 JS 部分中的“.dynamicSI select”替换为“.dynamicSI :input”,那么我会收到错误

如果我从代码中删除 dateInput 并在 JS 中进行更改,则观察将由 selectInput 和 numericInput 触发,但不会由 radioButtons 触发。

如何让所有这些都触发​​我的观察?

谢谢

最佳答案

获取inputId的方式取决于小部件的类型。

您可以按照以下步骤操作。

div(class = "dynamicSI",
lapply(buttons, function(i)
column(
width = 3,
div(class = "selector",
selectInput(inputId = paste0("title1_element",i),
label = paste("Title element",i),
choices = paste0(LETTERS[i],seq(1,i*2)),
selected = 1)
),
div(class = "radio",
radioButtons(inputId = paste0("title2_element",i),
label = paste("Title1 element",i),
choices = c("Yes","No"),
selected = "Yes")
),
div(class = "input",
numericInput(inputId = paste0("title3_element",i),
label = paste("Title element",i),value=1)
),
div(class = "date",
dateInput(inputId = paste0("title4_element",i),
label = paste("Title element",i),
value = "1900-01-01")
)
)
)
)

在 JavaScript 中,您可以将 Shiny.setInputValue 与选项 {priority: 'event'} 结合使用。这取代了 Shiny.onInputChange,而不需要使用 Math.random() 的“技巧”。

js <- "
$(document).on('change', '.dynamicSI .selector select', function(){
Shiny.setInputValue('lastSelectId', this.id, {priority: 'event'});
});
$(document).on('change', '.dynamicSI .radio input', function(){
Shiny.setInputValue('lastSelectId', $(this).attr('name'), {priority: 'event'});
});
$(document).on('change', '.dynamicSI .input input', function(){
Shiny.setInputValue('lastSelectId', this.id, {priority: 'event'});
});
$(document).on('change', '.dynamicSI .date input', function(){
Shiny.setInputValue('lastSelectId', $(this).parent().attr('id'), {priority: 'event'});
});
"

另外,在server中,不要将observeisolate一起使用,最好使用observeEvent .

完整的应用程序:

library(shiny)
library(shinydashboard)

js <- "
$(document).on('change', '.dynamicSI .selector select', function(){
Shiny.setInputValue('lastSelectId', this.id, {priority: 'event'});
});
$(document).on('change', '.dynamicSI .radio input', function(){
Shiny.setInputValue('lastSelectId', $(this).attr('name'), {priority: 'event'});
});
$(document).on('change', '.dynamicSI .input input', function(){
Shiny.setInputValue('lastSelectId', this.id, {priority: 'event'});
});
$(document).on('change', '.dynamicSI .date input', function(){
Shiny.setInputValue('lastSelectId', $(this).parent().attr('id'), {priority: 'event'});
});
"

ui <- dashboardPage(
dashboardHeader(title = ""),
dashboardSidebar(),
dashboardBody(
tags$head(tags$script(HTML(js))),

numericInput("graph_tytle_num", "Number of Graph Title elements",
value = 1, min = 1, max = 10),
uiOutput("graph_title"),
plotOutput("plot")
)
)

server <- function(input, output, session) {

#elements of graphic titles
output$graph_title <- renderUI({
buttons <- as.list(1:input$graph_tytle_num)
div(class = "dynamicSI",
lapply(buttons, function(i)
column(
width = 3,
div(class = "selector",
selectInput(inputId = paste0("title1_element",i),
label = paste("Title element",i),
choices = paste0(LETTERS[i],seq(1,i*2)),
selected = 1)
),
div(class = "radio",
radioButtons(inputId = paste0("title2_element",i),
label = paste("Title1 element",i),
choices = c("Yes","No"),
selected = "Yes")
),
div(class = "input",
numericInput(inputId = paste0("title3_element",i),
label = paste("Title element",i),value=1)
),
div(class = "date",
dateInput(inputId = paste0("title4_element",i),
label = paste("Title element",i),
value = "1900-01-01")
)
)
)
)
})

# react to changes in dynamically generated selectInput's
observeEvent(input$lastSelectId, {

cat("lastSelectId:", input$lastSelectId, "\n")
cat("Selection:", input[[input$lastSelectId]], "\n\n")

title <- c()
for(i in 1:input[["graph_tytle_num"]]){
title <- paste(title,input[[paste0("title1_element",i)]],input[[paste0("title2_element",i)]],
input[[paste0("title3_element",i)]],input[[paste0("title4_element",i)]])
}

output$plot <-renderPlot({hist(rnorm(100,4,1),
breaks = 10,
main = title)})

})

}

shinyApp(ui, server)

关于javascript - Shiny - 动态生成的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61475419/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com