0

我正在尝试使用以下功能构建一个闪亮的 R:

  • 用户有两个选项来输入需要在应用程序中进一步处理的数据、上传 csv 文件或在应用程序中填写表格。
  • 我能够让 csv 文件上传工作正常。问题在于表格部分。当用户选择输入表格时,会出现一个 2 列 x 10 行的表格。用户更新并保存表格后,应将表格转换为数据框以进行进一步处理。

以下是我到目前为止的代码。如果我选择表格单选按钮,并使用值更新表格并点击保存,它会给我错误消息。错误消息表明我调用 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

谢谢,克里纳

用户界面

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)

服务器.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})

})
4

1 回答 1

1

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

解决方案是从 github 安装 rhandsontable 包。

devtools::install_github("jrowen/rhandsontable", dependencies = T, upgrade_dependencies = T)
于 2017-11-15T02:13:21.840 回答