如何在不首先点击 table 的情况下,使用上传的数据更新 rhandsontable 的绘图?

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

我正在使用 rhandsontable 构建一个闪亮的应用程序,以便用户可以编辑 table 中的值,然后使用操作按钮更新相应的绘图。我还希望他们能够上传一个文件,然后填充 table,然后更新情节。

截至目前,我已经成功地允许用户上传一个填充 handsontable 的文件,但是为了让操作按钮更新情节,他们必须首先点击 table 然后回车。

我希望他们能够从上传的文件中更新情节,而无需单击 table 并点击 enter.Does 有人知道如何做到这一点吗?

可能与 input$contents 和 output$contents 不同步有关 link,但我不确定:

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

当前要上传的 .csv 文件示例:

Currency   Values
EUR            10
GBP            20
CAD             5

到目前为止我的代码:

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)

2016 年 9 月 27 日更新:

作者已在 github 上推送了一个新的包分支,现在允许原始代码正常工作。 有关详细信息,请参阅 https://github.com/jrowen/rhandsontable/issues/111

最后,我确实设法通过将数据存储在 reactiveValues 中并使用了几个观察器来实现它。我相信这些观察者的热切评价是关键。

新代码:

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)