rhandsometable: trying to update table and time series

Good afternoon everyone, i was wondering if you could help me with something, right now i am trying to make an app that allows me to introduce numerical values and from those introduced values the row below gets updated with each value being the sum of the values of the corresponding column

for example if i introduced values in the first table, on the first column named "ENE", the row below in the column named "ENE" is the sum of those values and changes every time i change values in the first table.

I have tried many different things but i haven´t been able to make the app to do it the way i wanted

Another thing i am trying to do is to make the app create a time series based on the values of the second table below

Here is the code

Col1<-c("","","","Región","","","","Usuarios Especiales","")
Col2<-c("Guyana","Capital","Centro Occidente","Ocidente","Sur Occidente","Oriente",
        "Insular","Hidrocapital e Hidrocentro, Minera Loma de Niquel","")
Col3<-c(replicate(8,0),0)
Col3.1<-Col3
Col3.2<-Col3
Col3.3<-Col3
Col3.4<-Col3
Col3.5<-Col3
Col3.6<-Col3
Col3.7<-Col3
Col3.8<-Col3
Col3.9<-Col3
Col3.10<-Col3
Col3.11<-Col3

Tabla_Suma=data.frame("Usuarios"=Col1," "=Col2,"ENE"=Col3,"FEB"=Col3.1,"MAR"=Col3.2,"ABR"=Col3.3,"MAY"=Col3.4,"JUN"=Col3.5,"JUL"=Col3.6,"AGO"=Col3.7,"SEP"=Col3.8,"OCT"=Col3.9,"NOV"=Col3.10,"DIC"=Col3.11)

Suma=data.frame("ENE"=sum(Col3),"FEB"=sum(Col3.1),"MAR"=sum(Col3.2),"ABR"=sum(Col3.3),"MAY"=sum(Col3.4),"JUN"=sum(Col3.5),"JUL"=sum(Col3.6),"AGO"=sum(Col3.7),"SEP"=sum(Col3.8),"OCT"=sum(Col3.9),"NOV"=sum(Col3.10),"DIC"=sum(Col3.11))


library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Plot Time Series"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            helpText("Plot time series with input values on table")
        ),

        # Show a plot of the generated distribution
        mainPanel(
            rHandsontableOutput("table2"),
            h3("Max power Demand (MW)a/"),
            rHandsontableOutput("table3"),
            br(),br(),
            plotlyOutput("plot_four")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output,session) {

    datavalues <- reactiveValues(data=Suma)
    
    output$table2 <-renderRHandsontable({
        if(is.null( Tabla_Suma )){return()}
        rhandsontable( Tabla_Suma , showToolbar=TRUE , search=TRUE , rowDrag = TRUE,
                       columnDrag = TRUE, rowResize = TRUE, minDimension = c(100, 100))
    })
    
    
    datavalues <- reactiveValues(data=Suma)
    
    output$table3 <-renderRHandsontable({
        if(is.null( datavalues$data )){return()}
        rhandsontable( datavalues$data , showToolbar=TRUE , search=TRUE , rowDrag = TRUE,
                       columnDrag = TRUE, rowResize = TRUE, minDimension = c(100, 100))
    })
    
    observeEvent(
        input$table3$changes$changes, # observe if any changes to the cells of the rhandontable
        {
            
            xi=input$table3$changes$changes[[1]][[1]] # capture the row which is changed
            datavalues$data <- hot_to_r(input$table3) # convert the rhandontable to R data frame object so manupilation / calculations could be done
            
            # Calculating the cell value of column C using cell values in column a and b
            # 1 is added to row index because change event row and column indices starts with zero vs R index which starts with 1
            
            datavalues$data[xi+1,3] = datavalues$data[xi+1,1] + datavalues$data[xi+1,2] # calculate column varibale C values based on cell values in column variable a and b
            
        }
    )
    
    output$plot_four <-renderPlotly({
        
        today <- Sys.Date()
        tm <- seq(0, 350, by = 5)
        x <- today + tm
        y <- rnorm(length(x))
        plot_ly(x = ~x, y = ~y, mode = 'lines', text = paste(tm, "days from today"))%>%
            layout(title = "Time series",
                   xaxis = list(title = "Months"),
                   yaxis = list (title = "MW"))
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Thanks for your time and have a nice day.

Hi @JJGML50. The following code can update the table3 by changing the table2. But the code at the bottom, I can't get the purpose, so I commented it out.

Col1<-c("","","","Región","","","","Usuarios Especiales","")
Col2<-c("Guyana","Capital","Centro Occidente","Ocidente","Sur Occidente","Oriente",
        "Insular","Hidrocapital e Hidrocentro, Minera Loma de Niquel","")
Col3<-c(replicate(8,0),0)
Col3.1<-Col3
Col3.2<-Col3
Col3.3<-Col3
Col3.4<-Col3
Col3.5<-Col3
Col3.6<-Col3
Col3.7<-Col3
Col3.8<-Col3
Col3.9<-Col3
Col3.10<-Col3
Col3.11<-Col3

Tabla_Suma=data.frame("Usuarios"=Col1," "=Col2,"ENE"=Col3,"FEB"=Col3.1,"MAR"=Col3.2,"ABR"=Col3.3,"MAY"=Col3.4,"JUN"=Col3.5,"JUL"=Col3.6,"AGO"=Col3.7,"SEP"=Col3.8,"OCT"=Col3.9,"NOV"=Col3.10,"DIC"=Col3.11)

# Suma=data.frame("ENE"=sum(Col3),"FEB"=sum(Col3.1),"MAR"=sum(Col3.2),"ABR"=sum(Col3.3),"MAY"=sum(Col3.4),"JUN"=sum(Col3.5),"JUL"=sum(Col3.6),"AGO"=sum(Col3.7),"SEP"=sum(Col3.8),"OCT"=sum(Col3.9),"NOV"=sum(Col3.10),"DIC"=sum(Col3.11))


library(shiny)
library(rhandsontable)
library(plotly)
library(tidyverse)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Plot Time Series"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      helpText("Plot time series with input values on table")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      rHandsontableOutput("table2"),
      h3("Max power Demand (MW)a/"),
      rHandsontableOutput("table3"),
      br(),br(),
      plotlyOutput("plot_four")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output,session) {
  
  datavalues <- reactiveValues(data=Tabla_Suma)
  
  output$table2 <-renderRHandsontable({
    if(is.null( Tabla_Suma )){return()}
    rhandsontable( datavalues$data , showToolbar=TRUE , search=TRUE , rowDrag = TRUE,
                   columnDrag = TRUE, rowResize = TRUE, minDimension = c(100, 100))
  })
  
  observe({
    req(input$table2$changes$changes)
    isolate({
      changes <- unlist(input$table2$changes$changes[[1]])
      str(changes)
      tab <- datavalues$data
      tab[changes[1] + 1, changes[2] + 1] <- changes[4]
      datavalues$data <- tab
    })
  })
  
  Suma <- reactive({
    colSums(datavalues$data[, -(1:2)]) %>%
      as.matrix() %>%
      t()
  })
  
  output$table3 <-renderRHandsontable({
    if(is.null( Suma() )){return()}
    rhandsontable( Suma() , showToolbar=TRUE , search=TRUE , rowDrag = TRUE,
                   columnDrag = TRUE, rowResize = TRUE, minDimension = c(100, 100))
  })
  
  # observeEvent(
  #   input$table3$changes$changes, # observe if any changes to the cells of the rhandontable
  #   {
  # 
  #     xi=input$table3$changes$changes[[1]][[1]] # capture the row which is changed
  #     datavalues$data <- hot_to_r(input$table3) # convert the rhandontable to R data frame object so manupilation / calculations could be done
  # 
  #     # Calculating the cell value of column C using cell values in column a and b
  #     # 1 is added to row index because change event row and column indices starts with zero vs R index which starts with 1
  # 
  #     datavalues$data[xi+1,3] = datavalues$data[xi+1,1] + datavalues$data[xi+1,2] # calculate column varibale C values based on cell values in column variable a and b
  # 
  #   }
  # )
  # 
  # output$plot_four <-renderPlotly({
  # 
  #   today <- Sys.Date()
  #   tm <- seq(0, 350, by = 5)
  #   x <- today + tm
  #   y <- rnorm(length(x))
  #   plot_ly(x = ~x, y = ~y, mode = 'lines', text = paste(tm, "days from today"))%>%
  #     layout(title = "Time series",
  #            xaxis = list(title = "Months"),
  #            yaxis = list (title = "MW"))
  # 
  # })
}

# Run the application 
shinyApp(ui = ui, server = server)


1 Like

thank you so much it works

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.