0

我正在使用 dplyr 构建多个 lm() 模型。我想允许用户通过 Shiny::sliderInput() 更改 Shiny 应用程序中的自变量值。但只有在“拟合优度”说 R^2 大于阈值时才这样做 - 否则禁用滑块。我尝试使用 shinyjs::disable() 函数。请参见下文,但无法使其正常工作。关于我做错了什么的任何想法?

library(shiny)
library(shinyjs)

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
   sidebarLayout(
      sidebarPanel(
         sliderInput("test","Nice number",min = 1,max = 50,value = 30)
      ),
    mainPanel(
        textOutput("valueText")
      )
   )
))

# Define server to disable slider if value selected

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

  value <- reactive(input$test)
   output$valueText <- renderText(paste(value()))

   #How to diasble slider?
      reactive(if(value()==35){
       shinyjs::disable('test')
     }
   )
   })

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

1 回答 1

1

您必须在 ui.R 中调用 useShinyjs()。

这是代码:

    library(shiny)
    library(shinyjs)

    # Define UI for application that draws a histogram
    ui <- shinyUI(
            tagList(
            useShinyjs(),
            fluidPage(
            sidebarLayout(
                    sidebarPanel(
                            sliderInput("test","Nice number",min = 1,max = 50,value = 30)
                    ),
                    mainPanel(
                            textOutput("valueText")
                    )
            )
    )

    )
    )

    # Define server to disable slider if value selected

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

            value <- reactive(input$test)
            output$valueText <- renderText(paste(value()))

            #How to diasble slider?
            observeEvent(value(), {
                    if(value()==35){
                            shinyjs::disable('test')
                    }  
            })

    })

    # Run the application 
    shinyApp(ui = ui, server = server)
于 2016-10-21T12:49:01.950 回答