1

我正在尝试使用默认的 rhandsontable 表创建一个闪亮的应用程序,然后用户有两个选项来更新它:

  1. 直接在 rhandsontable 桌子上手工完成或
  2. 使用数字输入和actionButton

我的代码在这里:

library(shiny)
library(rhandsontable)
library(data.table)

data_pop <- data.table(`Number` = c(1, 1, 2, 2, 3, 3), 
                       `Country` = c("Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina", "Armenia"),
                       `Population` = c(123, 456 , 789, 246 , 369 , 159))

ui <- navbarPage(title = tags$b(tags$span("APP")), id = "nav", fluid = TRUE,
                 
                 tabPanel(tags$b(tags$span("Model")), 
                          sidebarLayout(
                            sidebarPanel( 
                              numericInput("number", label = "Number", value = 0, min = 0, max = Inf),
                              actionButton("load", "Load", align = 'right'),
                            ),
                            mainPanel(
                              rHandsontableOutput("out_table"), 
                            )
                          )
                 )
)

server <- function(input, output) {
  
  default <- data.table(`Country` = "", 
                        `Column` = as.integer(0), 
                        stringsAsFactors = FALSE)

  values <- reactiveValues()

  observe({
    
    if (!is.null(input$table)) {
      table <- hot_to_r(input$out_table_country)
    } else {
      if (is.null(values[["table"]])) {
        
        default <- eventReactive(input$load, {
          data_pop[`Number` == input$number, .(`Country`, `Population`)]
        })
        table <- default
      }
      else {
        table <- values[["table"]]
      }
    }
    values[["table"]] <- table
  })
  
  output$out_table <- renderRHandsontable({
    table <- values[["table"]]
    if (!is.null(table))
      rhandsontable(table, stretchH = "all") %>%
      hot_col(col = "Country", type = "autocomplete", source = data_pop$Country, strict = T) %>%
      hot_col(col = "Column", format = "0,0") 
  })
}

shinyApp(ui = ui, server = server)

当我尝试default用反应性表替换表时出现问题,我得到:

Error in as.vector: cannot coerce type 'closure' to vector of type 'list'

我不知道它是否与hot_col列上的格式有关......我尝试了很多解决方案,但没有任何效果,非常感谢任何帮助!

非常感谢,

费边

4

0 回答 0