闪亮:使 RHandsontable 在单击时只读
Shiny: Making RHandsontable read only on click
我想让我的 rhandsontable 在单击操作按钮 'Freeze Forecast' 时只读,并在单击 'Edit Forecast' 时激活 table。它应该显示点击 'Generate Forecast' 按钮时的总和输出。
请根据上述条件帮助更正我现有的代码。
UI.R
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')
)
Server.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)
})
})
)
我通过
让这个工作
- 将状态变量添加到名为
readonly
的反应式中
将两个 observerEvent
例程添加到 edit
和 freeze
操作按钮以切换 readonly
。
修改您的 output$table
命令以使用反应性 readonly
变量。
如果您只是使用一个复选框来指示 table 是 editable,然后将该变量连接到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)
})
})
)
看起来像这样:
我想让我的 rhandsontable 在单击操作按钮 'Freeze Forecast' 时只读,并在单击 'Edit Forecast' 时激活 table。它应该显示点击 'Generate Forecast' 按钮时的总和输出。
请根据上述条件帮助更正我现有的代码。
UI.R
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')
)
Server.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)
})
})
)
我通过
让这个工作- 将状态变量添加到名为
readonly
的反应式中
将两个
observerEvent
例程添加到edit
和freeze
操作按钮以切换readonly
。修改您的
output$table
命令以使用反应性readonly
变量。
如果您只是使用一个复选框来指示 table 是 editable,然后将该变量连接到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)
})
})
)
看起来像这样: