我正在尝试使用 rhandsontable 记录用户输入并将其传递给 Shiny 服务器端以进行进一步处理。具体来说,对于以下代码,我想添加一列来记录用户输入并在 flexdashboard valueBox 中显示列的总和。但不知何故,reativeValue 似乎没有反应。无论我更改第一列 Vol_Percent,valueBox 都不会改变。有什么建议吗?谢谢!
---
title: "Test"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
runtime: shiny
---
```{r global, include=FALSE}
packages <- c("flexdashboard", "readr", "dplyr", "rhandsontable", "shiny")
for (p in packages) {
library(p, character.only = TRUE, quietly = TRUE)
}
```
Column {.sidebar}
-----------------------------------------------------------------------
### Input and Control
```{r input_panel}
# Input file
fileInput(inputId = "file_property_input", label = "Upload Properties")
```
Row
-----------------------------------------------------------------------
### Properties
```{r property_table}
# Load input data file
values <- reactiveValues()
df_input <- reactive({
validate(need(input$file_property_input, message = FALSE))
input_file <- input$file_property_input
return(read_csv(input_file$datapath))
})
data <- reactive({
if(is.null(values[["data"]])) {
data <- cbind(Vol_Percent = rep(0, nrow(df_input())), data.frame(df_input()))
} else {
data <- values[["data"]]
}
values[["data"]] <- data
return(data)
})
renderRHandsontable({
rhandsontable(data(), search = TRUE, readOnly = TRUE, height = 400) %>%
hot_col("Vol_Percent", readOnly = FALSE) %>%
hot_cols(fixedColumnsLeft = 1) %>%
hot_context_menu(allowRowEdit = FALSE, allowColEdit = FALSE,
customOpts = list(
search = list(name = "Search",
callback = htmlwidgets::JS(
"function (key, options) {
var srch = prompt('Search criteria');
this.search.query(srch);
this.render();
}"))))
})
```
Row
-----------------------------------------------------------------------
### Input Validility
```{r input_valid}
renderValueBox({
info <- "Input Validated"
valueBox(value = info, icon = ifelse(info == "Input Validated", "fa-check", "fa-times"), color = ifelse(info == "Input Validated", "success", "danger"))
})
```
### Total Percentage
```{r information}
renderValueBox({
rate <- sum(values[["data"]]$Vol_Percent)
valueBox(value = rate, icon = ifelse(rate == 100, "fa-check", "fa-times"), color = ifelse(rate == 100, "success", "warning"))
})
```