Monday, November 11, 2019

rhandsontable update in R Shiny App



I am trying to get an R shiny built with the following functionalities:





  • The user have two options to input their data that needs further processing in the app, upload csv file or fill in a table within the app.

  • I am able to get csv file upload working fine. The issue is with table portion. When user selects to input table, a 2 columns by 10 rows table appear. Once user updates it and saves the table, the table should be converted to data frame for further processing.



Below is my code so far. If I select table radiobutton, and update the table with values and hit save, it gives me error message. The error message indicate some issue with my calling of hot_to_r() function. Below is full error message:



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:
67: do.call
66: hot_to_r
65: observeEventHandler [C:\Users\kmehta\Documents\Projects\Enhanze\Test/server.R#46]
1: runApp


Thanks,

Krina



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})


})

Answer



This seems to be a known issue with older version of rhandsontable that is available through CRAN.



The solution is to install rhandsontable package from github.



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


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...