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

女生的网名这么多〃 提交于 2019-12-01 10:28:05

问题


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.


回答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)    


来源:https://stackoverflow.com/questions/39454157/how-can-i-update-plot-from-rhandsontable-with-uploaded-data-without-clicking-in

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