如何在不首先单击表格的情况下使用上载的数据从Rhandsontable更新情节? [英] How can I update plot from rhandsontable with uploaded data, without clicking into the table first?

查看:85
本文介绍了如何在不首先单击表格的情况下使用上载的数据从Rhandsontable更新情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有rhandsontable的闪亮应用程序,以便用户可以编辑表格中的值,然后使用操作按钮更新相应的绘图.我也希望他们能够上传文件,然后将文件填充到表格中,然后更新绘图.

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.

到目前为止,我已经设法允许用户上传一个文件,该文件填充了可动手制作表,但是为了使操作按钮更新绘图,他们必须首先单击表格并按Enter.

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.

我希望他们能够从上传的文件中更新绘图,而不必单击表并按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?

也许与以下链接中读取的input $ contents和output $ contents不同步有关,但是我不确定:

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

当前将上载的.csv文件示例:

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)

更新9/27/16:

作者好心地在github上推送了该软件包的一个新分支,该分支目前允许原始代码正常工作. 有关更多详细信息,请参见 https://github.com/jrowen/rhandsontable/issues/111

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.

推荐答案

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

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.

新代码:

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)    

这篇关于如何在不首先单击表格的情况下使用上载的数据从Rhandsontable更新情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆