r - r中shinyjs中的For循环有什么问题?
问题描述
我不知道如何使用 Shinyjs 库在 Shiny R 应用程序中的 javascript 中使 For 循环工作。如果我在脚本中添加任何 For 循环,工作脚本将停止运行。
演示该问题的最小示例代码如下。取消注释 For 循环(删除/*
和*/
行)会使代码不起作用。
请看一看。谢谢你。
library(shiny)
library(shinyjs)
ui <- fluidPage(
# pressing this button increments counter
actionButton("button", "press"),
# text showing counter value
textOutput("text"),
# place to store the javascript
uiOutput("default"),
useShinyjs()
)
server <- function(input, output, session) {
counter <- 1
output$default <- renderUI({
# this script sends current value of the counter back to Shiny as input$result
# For loop is dot doing anything
# if For loop is inside comment - the script works
# if the loop is not commented - the script does not work
tags$script("
Shiny.addCustomMessageHandler('bttn_pressed', function(value) {
var i;
/*
for (i = 0; i < 2; i++) {
}
*/
Shiny.setInputValue('result', value, {priority: 'event'});
});"
)
})
# update text output with the value reported by javascript
observeEvent(input$result, {
output$text <- renderText({input$result})
})
# increment the counter and send the updated value to javascript
observeEvent(input$button, {
counter <<- counter + 1
session$sendCustomMessage("bttn_pressed", counter)
})
}
shinyApp(ui = ui, server = server)