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

泪湿孤枕 提交于 2019-12-01 12:30:33

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)    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!