R Shiny App 中的 rhandsontable 更新

rhandsontable update in R Shiny App

我正在尝试构建具有以下功能的 R shiny:

下面是我目前的代码。如果我 select table 单选按钮,并用值更新 table 并点击保存,它会给我错误消息。错误消息表明我调用 hot_to_r() 函数时出现了一些问题。以下是完整的错误消息:

Warning: Error in row.names<-.data.frame: invalid 'row.names' length
Stack trace (innermost first):
    71: row.names<-.data.frame
    70: row.names<-
    69: rownames<-
    68: <Anonymous>
    67: do.call
    66: hot_to_r
    65: observeEventHandler [C:\Users\kmehta\Documents\Projects\Enhanze\Test/server.R#46]
     1: runApp

谢谢, 克里纳

ui.R

library(shiny)
library(shinydashboard)
library(shinyBS)
library(dplyr)
library(gdata)
library(rhandsontable)

#Design sidebar
sidebar <- dashboardSidebar(width = 200, collapsed=F, 
                            sidebarMenu(id="tabs",
                                        menuItem("TEST", tabName = "thick", icon = icon("puzzle-piece"))))

#Design body 
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "thick", 
            box(collapsible=TRUE, width = 4, status = "success", solidHeader = T, title="Input",
                radioButtons(inputId="fileInput", "Choose data input method", choices = c("csv", "table"), selected = 'csv'),  
                conditionalPanel(condition = "input.fileInput == 'csv'", 
                                 fileInput(inputId="file", label = "Upload data in CSV file", accept = c(".csv"))),
                conditionalPanel(condition="input.fileInput == 'table'",
                                 rHandsontableOutput("hot"),
                                 actionButton("save", "Save table")),
                numericInput('Dose', 'mAB dose (mg)', value=0, min=0),
                actionButton('gothick','Run', class='btn btn-info', icon=icon('play-circle-o','fg-lg'))),
            box(collapsible=T, width=6, status = "success", solidHeader = T, title="Table", tableOutput('Table')))
  ))

#Show title and the page (includes sidebar and body)
dashboardPage(skin="blue",
              dashboardHeader(title = "TEST", 
                              titleWidth = 800),
              sidebar,  body)

Server.R

library(shiny)
library(shinydashboard)
library(shinyBS)
library(RxODE)
library(ggplot2)
library(dplyr)
library(gdata)
library(rhandsontable)

Concentration <- c(0,0,0,0,0,0,0,0,0,0)
Viscosity <- c(0,0,0,0,0,0,0,0,0,0)
DF <- data.frame(Concentration, Viscosity)

shinyServer(function(input, output, session){

#Handsontable

  output$hot <- renderRHandsontable({
    rhandsontable(DF, useTypes = F)
  })

  observeEvent(input$save, {
    DF1 = hot_to_r(input$hot)
    finalDF <- DF1
    })

  ThickFastFun <- eventReactive(input$gothick, {
    if (!is.null(input$file)) 
    {dat <- read.csv(inFile$datapath, header=T, stringsAsFactors = F)}
    else {dat <- finalDF}
    return(dat)
  })

  #Table
  ThickFast_Table <- reactiveValues(df = NULL)
  observeEvent(input$gothick, {
    ThickFast_Table$df <- ThickFastFun()})

  output$ThickFastTable <- renderTable({ThickFast_Table$df})

})

这似乎是旧版 rhandsontable 的一个已知问题,可通过 CRAN 获得。

解决方案是从 github.

安装 rhandsontable 包
devtools::install_github("jrowen/rhandsontable", dependencies = T, upgrade_dependencies = T)