代码之家  ›  专栏  ›  技术社区  ›  SNT

无法使用shinyjs重置shinyjs应用程序中的输入

  •  0
  • SNT  · 技术社区  · 7 年前

    here 我试着用 shinyjs 通过给 id div UI

    library(shiny)
    library(DT)
    library(dplyr)
    library(shinyjs)
    
    #### Module 1 renders the first table
    tableMod <- function(input, output, session, modelRun,reset,modelData,budget){
    
      output$x1 <- DT::renderDataTable({
        modelRun()
        isolate(
          datatable(
            modelData %>% 
              mutate(Current  = as.numeric(Current)*(budget())),
            selection = 'none', editable = TRUE
          )
        )
      })
    
      observeEvent(reset(), {
        shinyjs::reset("input-panel")
      })  
    }
    tableUI <- function(id) {
      ns <- NS(id)
      dataTableOutput(ns("x1"))
    }
    
    ui <- function(request) {
      fluidPage(
        div(shinyjs::useShinyjs(), id = "input-panel",
        tableUI("opfun"),
        numericInput("budget_input", "Total Forecast", value = 2),
        actionButton("opt_run", "Run"),
        actionButton("opt_reset", "Reset")
      ))
    }
    
    server <- function(input, output, session) {
    
      df <- data.frame(Channel = c("A", "B","C"),
                       Current = c(2000, 3000, 4000),
                       Modified = c(2500, 3500,3000),
                       New_Membership = c(450, 650,700),
                       stringsAsFactors = FALSE)
    
      callModule( tableMod,"opfun",
                  modelRun = reactive(input$opt_run),
                  reset = reactive(input$opt_reset),
                  modelData = df,
                  budget = reactive(input$budget_input))
    
      observeEvent(input$opt_run, {
        cat('HJE')
      })
    
    }
    
    shinyApp(ui, server, enableBookmarking = "url")
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   amrrs    7 年前

    这是部分解决方案,其中 run 按钮不是真正需要的。因为 input$budget_input 是一个无功值,当输入值更改时,表将自动刷新。

    library(shiny)
    library(DT)
    library(dplyr)
    library(shinyjs)
    
    tableUI <- function(id) {
      ns <- NS(id)
      tagList(
    
        div(id = ns("input-panel"),
        h1("Tests "),
    
        numericInput(ns("budget_input"), "Total Forecast", value = 2),
        actionButton(ns("opt_run"), "Run"),
        actionButton(ns("opt_reset"), "Reset"),
    
        dataTableOutput(ns("x1"))
        )
      )
    
    
    
    }
    
    
    #### Module 1 renders the first table
    tableMod <- function(input, output, session,modelData){
    
    
     # observeEvent(input$opt_run, ignoreInit = TRUE, {
     #   Multiplier <- reactiveVal(input$budget_input)
     #   cat(Multiplier())
     # })
    
    
    #  observeEvent(input$opt_run,{
    
        output$x1 <-  DT::renderDataTable({
    
       #  isolate(
        datatable(
          modelData %>% 
            mutate(Current  = as.numeric(Current)*(input$budget_input)),
          selection = 'none', editable = TRUE
            )
      #  )
      })
        #})
    
    
      observeEvent(input$opt_reset,{
    
        shinyjs::reset("input-panel")
    
        })
    #  }) 
    
    }
    
    
    
    ui <- fluidPage(
      useShinyjs(debug = TRUE),    
      tableUI("opfun")
    )
    
    server <- function(input, output, session) {
    
      df <- data.frame(Channel = c("A", "B","C"),
                       Current = c(2000, 3000, 4000),
                       Modified = c(2500, 3500,3000),
                       New_Membership = c(450, 650,700),
                       stringsAsFactors = FALSE)
    
      callModule( tableMod,"opfun",
                 # modelRun = reactive(input$opt_run),
               # reset = reactive(input$opt_reset),
                  modelData = df
               #   budget = reactive(input$budget_input)
               )
    
    
    
    }
    
    shinyApp(ui, server, enableBookmarking = "url")