首页 > 解决方案 > Shiny.setInputValue 不是函数错误

问题描述

我很困惑为什么以下代码会产生“Shiny.setInputValue 不是函数”错误:

library(shiny)
library(htmltools)

# Define UI for application that draws a histogram
ui <- fluidPage(
    
    
    # Application title
    titlePanel("Test"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            
        ),

        # Show a plot of the generated distribution
        mainPanel(
          shiny::tags$script(htmltools::HTML('
  quantityaa = 1;
  console.log(quantityaa);
  Shiny.setInputValue("hi", quantityaa);
                                  '))
       ,
       
        )
        )
        
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    hi <- reactive({ input$hi})
    print(hi)
    
}

# Run the application 
shinyApp(ui = ui, server = server)

产生此错误的代码有什么问题?我看不出有什么问题,但我一定是错过了什么。

标签: rshiny

解决方案


那是因为 Shiny 还没有准备好。使用shiny:connected事件:

$(document).on("shiny:connected", function() {
  // your awesome JavaScript here can use Shiny.setInputValue
});

推荐阅读