0

I'm in the process of making 29 shiny apps that are all very similar but have data from different states. I've been successfully copying and pasting the code and just changing the data. I'm displaying outcomes of homeless and housed students and I want to round percentages to the nearest tenth. I'm having a problem where R is adding an extra 0 to the end of some percentages, but only for the homeless group and only with a certain handful of variables. It doesn't happen every time and even among the few variables it's been happening with (which are all formatted the same, if that matters) it's not always the same ones in each app. I can't figure out if it's an R issue or a shiny issue, sometimes they're showing up correctly in R and don't get messed up until I run the app, but sometimes it happens regardless of running the app. Is this some sort of bug? Is there any workaround? The best solution I've found so far is just to manually enter those labels, but that's kind of a pain. This is the code I'm using to round.

CAWtabkypct$Homeless <- percent(round(CAWtabkypct$Homeless, 3)) 
CAWtabkypct$Housed <- percent(round(CAWtabkypct$Housed, 3))

This is the output I get, and what the graphs look like in the app.

output graphs

I wouldn't even know how to begin to share a reproducible example, so please let me know if anyone knows what's going on or if there's any more information I can provide.

EDIT: here is a very minimal reproducible example:

CAWtabkyhml <- c(0.3756009, 0.2974416, 0.3269574)
CAWtabkyhsd <- c(0.1410886, 0.2504221, 0.6084893)

CAWtabkypcthml <- percent(round(CAWtabkyhml, 3))
CAWtabkypcthsd <- percent(round(CAWtabkyhsd, 3))
Rachel
  • 13
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. I cannot replicate the problem with the information provided. – MrFlick Oct 24 '19 at 20:15
  • 1
    "Extra zeroes" can be for various reasons, almost all of them related to how R *represents* things on the console. Some controls include `options(c("digits","scipen"))` (I encourage you read `?options` to learn what they do). However, typically the fix involves *telling R what you want*, since it is just guessing; for that, I suggest `sprintf` or `format`. – r2evans Oct 24 '19 at 20:16
  • this is a bit tough.. A reproducible example would be providing the data frame CAWtabkypct? And what is the percent function? – StupidWolf Oct 24 '19 at 20:19
  • I was clearly thinking too big because I wasn't expecting something so minimal to reproduce the problem but it did (at least on my end), so I edited the post with a reproducible example. – Rachel Oct 24 '19 at 20:54

1 Answers1

1

The percent() function does it's own rounding. You should just use that feature rather than bothering with round()

CAWtabkypcthml <- scales::percent(CAWtabkyhml, accuracy=.1)
CAWtabkypcthsd <- scales::percent(CAWtabkyhsd, accuracy=.1)
CAWtabkypcthml
# [1] "37.6%" "29.7%" "32.7%"
CAWtabkypcthsd
# [1] "14.1%" "25.0%" "60.8%"
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This seems to be working, thank you for the suggestion! I was actually unable to reproduce the error at home last night so I'm not really sure a reproducible example is possible, but do you (or anyone) have any idea why that would have been happening? Doesn't really matter now that this solution works, it just seems so random. – Rachel Oct 25 '19 at 13:54
  • It really has to do with the fact that computers store floating point numbers in a binary representation rather than a decimal representation like we are used to seeing them. Numbers don't keep track of how many decimal places they have. So rounding gets tricky and rounding a rounded value might not always produce the same results. – MrFlick Oct 25 '19 at 15:07