1

I am building a shiny app with rhandsontable so that the user can edit values in the table and then update the corresponding plot using an action button. I would also like them to be able to upload a file, which will then populate the table, and then update the plot.

As of right now I have managed to allow the user to upload a file which populates the handsontable, but in order for the action button to update the plot, they must first click into the table and hit enter.

I would like them to be able to update the plot from an uploaded file without having to click into the table and hit enter.Does anybody know how to do this?

Maybe it has to do with input$contents and output$contents not being in-sync as read in the following link, but I am not sure:

https://github.com/jrowen/rhandsontable/blob/master/vignettes/intro_rhandsontable.Rmd#shiny

Example of .csv file that would be uploaded currently:

Currency   Values
EUR            10
GBP            20
CAD             5

My code so far:

library(shiny)
library(rhandsontable)

empty_mat = matrix(1,nrow = 3, ncol = 1)
curr_names = c("EUR","GBP","CAD")
empty_dat = cbind.data.frame(curr_names,empty_mat)
names(empty_dat) = c("Currency","Values")


ui = fluidPage(sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File'),
      rHandsontableOutput('contents'),
      actionButton("go", "Plot Update")
    ),
    mainPanel(
      plotOutput("plot1")
    )
))

server = function(input, output) {

  indat <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(rhandsontable(empty_dat))
    raw_input = read.csv(inFile$datapath, header=T)
    return(rhandsontable(raw_input))
  })

  output$contents <- renderRHandsontable({
    indat()
    })

  portfoliovals <- eventReactive(input$go, {
    live_data = hot_to_r(input$contents)[,2]
    return(live_data)
    })

  output$plot1 <- renderPlot({
    plot(portfoliovals()~c(1,2,3),type = "l")
  })

}


shinyApp(ui, server)

Update 9/27/16:

The author has kindly pushed a new branch of the package on github which for now allows the original code to work without issue. See https://github.com/jrowen/rhandsontable/issues/111 for more details.

pbarron
  • 21
  • 8
  • I think the newest version 0.3.3 available on CRAN solves this. With it, I can immediately create the plot on pressing "Plot Update" with the default data displayed, whereas this didn't happen in previous versions. However, I just tried and after uploading the .cvs file, one still needs to click in the table before pressing the "Plot Update" button to get the graph to update – Bernhard Klingenberg Sep 12 '16 at 18:30
  • I am using version 0.3.3 on my machine, and like you say that seems to work fine with the default data. However, my main question is whether there is some way to upload data and update the plot without having to click into the table first. – pbarron Sep 12 '16 at 18:54

1 Answers1

1

In the end I did manage to get this to work by storing the data in reactiveValues and using a couple of observers. I believe that the eager evaluation of these observers was the key.

New code:

library(shiny)
library(rhandsontable)

empty_mat = matrix(1,nrow = 3, ncol = 1)
curr_names = c("EUR","GBP","CAD")
empty_dat = cbind.data.frame(curr_names,empty_mat)
names(empty_dat) = c("Currency","Values")


ui = fluidPage(sidebarLayout(
sidebarPanel(
  fileInput('file1', 'Choose CSV File'),
  rHandsontableOutput('contents'),
  actionButton("go", "Plot Update")

),
mainPanel(
  plotOutput("plot1")
)
))


server = function(input, output) {

  indat <- reactiveValues(data=empty_dat)

  observe({
    inFile = input$file1
    if (is.null(inFile))
      return(NULL)
    data1 = read.csv(inFile$datapath)
    indat$data <- data1
  })

  observe({
    if(!is.null(input$contents))
      indat$data <- hot_to_r(input$contents)

  })  

  output$contents <- renderRHandsontable({
    rhandsontable(indat$data)
    })

  portfoliovals <- eventReactive(input$go, {
    return(indat$data[,2])
    })

  output$plot1 <- renderPlot({
    plot(portfoliovals()~c(1,2,3),type = "l")
  })

}


shinyApp(ui, server)    
pbarron
  • 21
  • 8