pbarron pbarron - 2 months ago 91
R Question

How can I update plot from rhandsontable with uploaded data, without clicking into the table first?

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)

Answer

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)