Displaying datatable column values in dollars in Shiny r
Imagine your datatable (myData) is 2 columns by 10 rows.
You want the second row to be in dollars:
myData[,2]<-sapply(myData[,2],function(x) paste0("$",x))
Or, you want rows 6 to 10 in the first column to be percentages:
myData[6:10,1]<-sapply(myData[6:10,1],function(x) paste0(x,"%"))
Or, you want rows 1 to 5 in the second column to be in dollars, you can do:
myData[1:5,2]<-sapply(myData[1:5,2],function(x) paste0("$",x))
Change number format in renderDataTable
just use round command:
output$woeTable <- renderDataTable({
input$tryTree
input$threshold
# The following returns data frame with numeric columns
A = get(input$dfDescr)[['variables']][[input$columns]][['woe']]
A[,2] = round(x = A[,2],digits = 2)
A[,3] = round(x = A[,3],digits = 2)
A
},
options=list(
paging = FALSE,
searching = FALSE)
)
you can also use fnRowCallback
option in renderDataTable
function if you insist to keep data with more digit and just change representation in output:
output$woeTable <- renderDataTable({
input$tryTree
input$threshold
# The following returns data frame with numeric columns
get(input$dfDescr)[['variables']][[input$columns]][['woe']]
},
options=list(
paging = FALSE,
searching = FALSE,
fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))
)
update for DT 1.1:
you should change
fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))
to
rowCallback = I("function( nRow, aData) {ind = 2; $('td:eq('+ind+')', nRow).html( parseFloat(aData[ind]).toFixed(2) );}"))
R Tidyverse spread() function multiple decimal places truncation issue
I recreated the dummy variables in my R envir.
Indeed when print(spead_data)
, i get:
day_of_week Bob Jack Simon
<chr> <dbl> <dbl> <dbl>
1 Friday NA NA 0.169
2 Monday NA 0.235 NA
3 Thursday NA NA 0.736
4 Tuesday 0.764 NA NA
5 Wednesday 0.725 NA NA
However, if you access values directly, for example spead_data$Bob
yields :
[1] NA NA NA 0.7635 0.7253
Here are your 4 digits ! They never left, just the print function of tibbles that is a bit different.
I don't recommend turning your values to factors as @saisaran suggests, you won't be able to use them properly afterwards.
Edit :
if you use print.data.frame(spead_data)
instead of print(spead_data)
, you will get the output you need :
day_of_week Bob Jack Simon
1 Friday NA NA 0.1693
2 Monday NA 0.2346 NA
3 Thursday NA NA 0.7356
4 Tuesday 0.7635 NA NA
5 Wednesday 0.7253 NA NA
Source : https://community.rstudio.com/t/why-do-tibbles-and-data-frames-display-decimal-places-a-bit-differently/5722
Plugging edited Datatable values in a shiny app
A clean solution (rather than assigning values in other environments) would be to use a reactiveVal
in your module which is in sync with the datatable
. You can return this reactive
from your module to make use of it also in the main application:
library(shiny)
library(DT)
library(dplyr)
#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun, modelData, budget){
df <- reactiveVal(modelData) ## this variable will be in sync with your datatable
output$x1 <- DT::renderDataTable({
modelRun()
isolate(
datatable(
df() %>% ## ...you always use the synced version here
mutate(New_Membership = as.numeric(Modified * 0.01)*(budget())),
selection = 'none', editable = TRUE
)
)
})
observeEvent(input$x1_cell_edit, {
new_df <- df()
row <- input$x1_cell_edit$row
col <- input$x1_cell_edit$col
value <- as.numeric(input$x1_cell_edit$value)
new_df[row, col] <- value
df(new_df) ## ...and you make sure that 'df' stays in sync
})
list(updated_df = df) ## ...finally you return it to make use of it in the main app too
}
tableUI <- function(id) {
ns <- NS(id)
dataTableOutput(ns("x1"))
}
ui <- function(request) {
fluidPage(
tableUI("opfun"),
numericInput("budget_input", "Total Forecast", value = 2),
actionButton("opt_run", "Run")
)
}
server <- function(input, output, session) {
df <- data.frame(Channel = c("A", "B","C"),
Current = c(2000, 3000, 4000),
Modified = c(2500, 3500,3000),
New_Membership = c(450, 650,700),
stringsAsFactors = FALSE)
result <- callModule( tableMod,"opfun",
modelRun = reactive(input$opt_run),
modelData = df,
budget = reactive(input$budget_input))
observeEvent(input$opt_run, {
str(result$updated_df())
})
}
shinyApp(ui, server, enableBookmarking = "url")
R Shiny DataTables: Format numbers not by column but by row
You can use the rowCallback
functions:
library(DT)
data <- matrix(c(0,0.64,-0.76234,0.43,1,19),nrow=3,byrow=T)
datatable(data,options=list(
rowCallback=JS("function( row, data, index ) {
$('td:eq(0)', row).html(data[0] % 1 != 0 | data[0]==0 ? (data[0]*100).toFixed(1) +'%':data[0]);
$('td:eq(1)', row).html(data[1] % 1 != 0 | data[1]==0 ? (data[1]*100).toFixed(1) +'%':data[1]);
}
")))
This example will multiply the number by 100, round to 1 decimal and add a percent sign if the number has a decimal part or if it is 0. If not, it leaves the number as is.
It is only doing this on the first and second column of the datatable.
Related Topics
Using Pivot_Longer with Multiple Paired Columns in the Wide Dataset
R - Scaling Numeric Values Only in a Dataframe with Mixed Types
Fill in Na Based on the Last Non-Na Value for Each Group in R
Rsqlite Query with User Specified Variable in the Where Field
Subset a Data.Frame with Multiple Conditions
Preventing Column-Class Inference in Fread()
Nested Facet Plot with Ggplot2
Rename Columns Using 'Starts_With()' Where New Prefix Is a String
R Data.Table Rolling Join "Mult" Not Working as Expected
Tukeys Post-Hoc on Ggplot Boxplot
How to Add Axis Text in This Negative and Positive Bars Differently Using Ggplot2
Fill in Data Frame with Values from Rows Above
Loop for Reverse Geocoding in R
How to Merge Two Data Frames in R by a Common Column with Mismatched Date/Time Values
R: How to Make a Confusion Matrix for a Predictive Model