I'm trying to display a frequency table using Shiny in R, but for some reason it insists on showing the frequency to two decimal places. I've tried using round(), but this isn't having any impact. I'm guessing it might be to do with the merge, because when I back up and just display the frequency table, the frequency is shown without unnecessary decimal places...but I'm not sure which exact part is causing the problem, which is why I'm including pretty much all elements of wrangling I do to the code in this example. You need to search for one of the fruits (e.g. apple) to get the table to display and see what I mean.
Thanks for your help!
library("shiny")
library("shinydashboard")
fruit <- as.character(c("apple", "pineapple",
"orange", "watermelon", "apple",
"grape", "orange", "apple", "grape"))
fruit_type <- as.character(c("apple", "pineapple", "orange", "watermelon", "grape", "banana", "coconut"))
fruit_rating <- 1:7
fruit_info <- data.frame(fruit_type, fruit_rating, stringsAsFactors = F)
sidebar <- dashboardSidebar(searchInput("search",
btnSearch = icon("search"),
btnReset = icon("remove")))
body <- dashboardBody(tableOutput("table"))
ui <- dashboardPage(dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output) {
output$table <- renderTable({
fruit_query <- input$search
fruit_count <- plyr::count(fruit)
colnames(fruit_count)[1] <- "fruit_type"
fruit_count$fruit_type <- as.character(fruit_count$fruit_type)
fruit_count <- merge(fruit_count, fruit_info,
by = "fruit_type", all = T)
fruit_count$freq <- ifelse(is.na(fruit_count$freq),
0, fruit_count$freq)
searched_fruit <- fruit_count[fruit_count$fruit_type == fruit_query,]
searched_fruit$freq <- round(searched_fruit$freq)
return(searched_fruit)
})
}
shinyApp(ui, server)
DT::renderDataTable
instead ofrenderTable
, and functionformatRound
. Don't know if it works in the dashboard too. - huan