1

我想让我的 rhandsontable 在单击操作按钮“冻结预测”时只读,并在单击“编辑预测”时激活表格。单击“生成预测”按钮时,它应该向我显示总和输出。

请根据上述条件帮助更正我现有的代码。

用户界面

packages <- c( "shiny", "data.table", "devtools", "shinysky","googleVis","scales","rhandsontable" )
lapply( packages, require, character.only = TRUE )

jsResetCode <- "shinyjs.reset = function() {history.go(0)}" #JS Code to refresh the App

did_recalc <- FALSE

ui <- fluidPage(
  # Application title
  titlePanel("Scenario Planner Test App"),

    br(),br(),
  actionButton("recalc", "Generate Forecast"),
  actionButton("edit", "Edit Forecast"),
  actionButton("freeze", "Freeze Forecast"),br(),br(),
  rHandsontableOutput('table'),br(),br(),
  textOutput('restitle'),
  textOutput('result')

)

服务器.R

Sys.setenv(R_ZIPCMD="/usr/bin/zip")
packages <- c( "shiny", "data.table", "devtools", "shinysky","googleVis","scales","reshape2" )
lapply( packages, require, character.only = TRUE )

disableActionButton <- function(id,session) {
  session$sendCustomMessage(type="jsCode1",
                            list(code= paste("$('#",id,"').prop('disabled',true)"
                                             ,sep="")))
}

enableActionButton <- function(id,session) {
  session$sendCustomMessage(type="jsCode2",
                            list(code= paste("$('#",id,"').prop('disabled',false)"
                                             ,sep="")))
}


shiny::shinyServer( function(input,output,session)({
  values <- reactiveValues(data=as.data.frame(runif(2)))

  observe({
    input$recalc
    values$data <- as.data.frame(runif(2))
  })

  observe({
    if(!is.null(input$table))
     values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    rhandsontable(values$data)
    })

  observe({
    input$freeze
    print("freeze")
    ##if(!is.null(input$table))
    print("2freeze")
    rhandsontable(values$data)  %>%
    hot_table(readOnly = TRUE)
  })


  output$restitle <- renderText({ 
          "Sum Output"
     })

  output$result <- renderText({ 
    sum(values$data)
  })
}) 
)
4

1 回答 1

1

我得到了这个工作

  • 将状态变量添加到您的响应式调用中readonly
  • 将两个observerEvent例程添加到editfreeze操作按钮以切换readonly

  • 修改您的output$table命令以使用反应readonly变量。

如果您只是使用复选框来指示表格是可编辑的,然后将该变量连接到参数,那会更容易,并且不需要很多这些元素readOnly,但有时您需要这样做,所以我像这样解决了它。

完整的server.R代码在这里:

packages <- c("shiny","data.table","devtools","shinysky","googleVis","scales","reshape2")
lapply(packages,require,character.only = TRUE)

disableActionButton <- function(id,session) {
  session$sendCustomMessage(type = "jsCode1",
                            list(code = paste("$('#",id,"').prop('disabled',true)"
                                             ,sep = "")))
}

enableActionButton <- function(id,session) {
  session$sendCustomMessage(type = "jsCode2",
                            list(code = paste("$('#",id,"').prop('disabled',false)"
                                             ,sep = "")))
}


shiny::shinyServer(function(input,output,session)({

  values <- reactiveValues(data = as.data.frame(runif(2)),readonly=FALSE)

  observe({
    input$recalc
    values$data <- as.data.frame(runif(2))
  })

  observe({
    if (!is.null(input$table))
      values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    rhandsontable(values$data,readOnly=values$readonly)
  })

  observeEvent(input$edit, {
   values$readonly <- FALSE
  })

  observeEvent(input$freeze,{
    values$readonly <- TRUE
  })


  output$restitle <- renderText({
    "Sum Output"
  })

  output$result <- renderText({
    sum(values$data)
  })
})
)

它看起来像这样:

在此处输入图像描述

于 2016-12-29T12:16:55.610 回答