Skip to content

Instantly share code, notes, and snippets.

@martin1007
Created August 18, 2018 20:16
Show Gist options
  • Save martin1007/816c14b54c5a28319400403fedec8b05 to your computer and use it in GitHub Desktop.
Save martin1007/816c14b54c5a28319400403fedec8b05 to your computer and use it in GitHub Desktop.
R Shiny infinite loop
After two numbers are inputed, the program is supposed to multiply them and output in the third row of dataframe.
However, program goes to infinite loop. Also, multiplication should not truncate or round up/down the double numbers.
Please advise how to solve it. Thank you
library(shiny)
library(rhandsontable)
create_df <- function(){
GM<-data.frame(matrix(0.0, ncol = 5, nrow = 3))
col_names <- c("2015", "2016", "2017", "2018", "2019")
row_names <- c("Array1", "Array2", "Product")
colnames(GM) <- col_names
rownames(GM) <- row_names
return(GM)
}
ui <- fluidPage(
titlePanel("GM"),
mainPanel(
rHandsontableOutput("GM"))
)
server <- function(input, output) {
v = reactiveValues()
observe({input$GM
if (!is.null(input$GM)) {
v$GM <- hot_to_r(input$GM)
print(v$GM) # to see if values get rounded down
} else {
v$GM <- create_df()
}
})
observe({
v$GM[3,]<-v$GM[1,] * v$GM[2,]
})
output$GM <- renderRHandsontable({
rhandsontable(v$GM, rowHeaderWidth = 150) %>%
hot_col(colnames(v$GM)[1], format = "0,0.00") %>%
hot_col(colnames(v$GM)[2], format = "0,0.00") %>%
hot_col(colnames(v$GM)[3], format = "0,0.00") %>%
hot_col(colnames(v$GM)[4], format = "0,0.00") %>%
hot_col(colnames(v$GM)[5], format = "0,0.00") %>%
hot_cols(colWidths = 100) %>%
hot_rows(rowHeights = 30)
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment