Skip to content

Instantly share code, notes, and snippets.

@martin1007
Created August 18, 2018 19:46
Show Gist options
  • Save martin1007/d14c1e7f97266195f6cf7f07d0f5322a to your computer and use it in GitHub Desktop.
Save martin1007/d14c1e7f97266195f6cf7f07d0f5322a to your computer and use it in GitHub Desktop.
R Shiny number formatting issues in rhandsontable
I created a simple table using rhandsotable library and no matter how I format it,
number always gets rounded down or up to closest integer.
For example, if I type in 1.02, the result shown is 1. For 3.05, result shown is 3.
Please kindly advise how to solve this issue. Thank you.
library(shiny)
library(rhandsontable)
GM <- data.frame(matrix(0.0, ncol = 5, nrow = 3))
col_names <- c("2015", "2016", "2017", "2018", "2019")
row_names <- c("Worst", "Base", "Best")
colnames(GM) <- col_names
rownames(GM) <- row_names
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 <- GM
}
})
output$GM <- renderRHandsontable({
rhandsontable(v$GM, rowHeaderWidth = 150) %>%
hot_col("2015", format = "0,0.00%") %>%
hot_col("2016", format = "0,0.00%") %>%
hot_col("2017", format = "0,0.00%") %>%
hot_col("2018", format = "0,0.00%") %>%
hot_col("2019", format = "0,0.00%") %>%
hot_cols(colWidths = 100) %>%
hot_rows(rowHeights = 30)
})
}
shinyApp(ui = ui, server = server)
@tobybot11
Copy link

I believe I'm reproducing the error once rendered in ShinyApp.. but want to confirm.

When I render this code / shinyApp in RStudio.. it renders this rhandonstable as html/javascfript.. and when I start to interact with the table..

  • If you type in 1.02 in the 2015 column/worst row 'cell', then you see 100%.
  • If you then type in 1.02 in the same 2015 column/worst row 'cell' a second time, then you see 102%.
    Is this the issue? Or is it something else?

I'm going thru and trying it in rhandsontable by itself now.

@tobybot11
Copy link

For the above problem i referenced.. I tried in RStudio without shiny, with only rhandsontable.. and it works as expected.
If I type in 1.02 in the 'A1' cell and it displays 102%.
If I type in 1.1111111, then it shows the expected 111.11%

image

I'll look into it a bit more later but it seems like it's the way shinyApp is rendering this rhandsontable that is the issue.
At least in terms of the issue i described above.

Do you see your problem in rhandsontable byitself?

@martin1007
Copy link
Author

Hello Toby,

Thank you for advises.

Yes, this is the problem:

If you type in 1.02 in the 2015 column/worst row 'cell', then you see 100%.
If you then type in 1.02 in the same 2015 column/worst row 'cell' a second time, then you see 102%.

Looking forward to hearing from you.

@tobybot11
Copy link

Have you entered an issue in the github repo for rhandsontable https://github.com/jrowen/rhandsontable

The issue is similar, if not exactly the same, to these entries jrowen/rhandsontable#256
and .. https://community.rstudio.com/t/rhandsontable-decimal-places-rounded-even-with-format-0-00/9344

I don't entirely agree with the final conclusion in the rstudio thread.

I think the issue is the interaction between shinyApp and rhandsontable and how data is passed between them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment