Rhandsontable in Shiny

拟墨画扇 提交于 2020-05-17 04:17:53

问题


EDIT: I included observeEvent() in server.R as described in this video : https://www.youtube.com/watch?v=BzE1JmC0F6Q. It works partially. When I update A and B, the graph gets updated, but the columns C,D and E are giving wrong values and the number of rows keep growing. Any help is appreciated.

I have 2 columns A and B. The user has the option to drag and drop values into A and B , and hence I am using rhandsontable (Source: https://jrowen.github.io/rhandsontable/)

I have the code below for the ui.R and server.R. The app basically has an editable table and a ggplotly output. Based on what I enter in columns A and B, columns C,D and E should get updated automatically based on some formula which I have included in server.R. When I run this code, the graphs and the columns don't update. What should I be doing?

ui.r

library(shiny)
shinyUI(fluidPage(
  fluidRow(
    column(4, rHandsontableOutput("table")),
    column(4, plotlyOutput("plot"))
    )
  ))

server.R

shinyServer(function(input, output, session) {

  df<- data.frame(A=c(9,15,25,35,40,45,50,55,60,65,75,85,95,105,120,125),
                  B=c(0.5,1.5,3,8,16,23,30,36,43,58,64,78,92,98,99,100))

  for(i in 2:(nrow(df))) 
  {
    df[i,3]<-df[i-1,1]+(df[i,1]-df[i-1,1])/2
    df[i,4]<-(df[i,2]-df[i-1,2])
    df[i,5]<- scientific(df[i,4]/df[i,3]/100,digits=3)
  }

  datavalues<-reactiveValues(data=df)

 output$table <- renderRHandsontable({

    mytab <- rhandsontable(datavalues$data, colHeaders=TRUE, rowHeaders=TRUE)
    return(mytab)
  })


  observeEvent(

    input$table$changes$changes,
    {
      xi= input$table$changes$changes[[1]][[1]]
      datavalues$data<-hot_to_r(input$table)

      for(i in 2:(nrow(df)))
      {

        datavalues$data[xi+i,3]<-datavalues$data[i-1,1]+(datavalues$data[i,1]-datavalues$data[i-1,1])/2
        datavalues$data[xi+i,4]<-(datavalues$data[i,2]-datavalues$data[i-1,2])
        datavalues$data[xi+i,5]<- scientific(datavalues$data[i,4]/datavalues$data[i,3]/100,digits=3)
      }

    }
  )




  output$plot<-renderPlotly({

    plt<-
      ggplot(datavalues$data,aes(A,B))+
      geom_point()+
      geom_line()+
      xlab("A")+
      ylab("B")

    p<-config(ggplotly(plt),mathjax = 'cdn')
    p

  })

来源:https://stackoverflow.com/questions/60878248/rhandsontable-in-shiny

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