I used formattable
package in R to create a conditionally colored table rendered in the viewer as an html page (This is the link the the forum response that helped me to do this (thanks for the responder!): How to conditionally color table elements using formattable and color_tile?). I want to save it exactly like if I click export and save as a web page within the viewer.
I am trying to save the html page seen in the Viewer window with the conditional coloring. I've tried the two solutions here but am not able to save with their colors still present:
Save View() output of RStudio as html
Save html result to a txt or html file
TLDR: I want to save the output from the code below (Credit to Ruth Gg) with the colors still present basically via code not with the click and point button within viewer.
Here is the code to make the table I am trying to save from the previous StackOverflow forum:
library(formattable)
# Set custom colors
green <- "#71CA97"
red <- "#ff7f7f"
# Make example table
col1 <- c(1.2, 4.2, 5.6, 7.1)
col2 <- c(5.0, 1.3, 10.3, 6.0)
col3 <- c(4.7, 6.3, 1.5, 6.3)
mydata <- data.frame(col1, col2, col3)
# Define color_tile_mean function
color_tile_mean <- function (...) {
formatter("span", style = function(x) {
style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(x < mean(x) , red, green)) # Remember to change the colors!
})}
# Use it just like color_tile but without colors
formattable(mydata, align=c("c", "c", "c"),list(
col1=color_tile_mean(),
col2=color_tile_mean(),
col3=color_tile_mean()
)
)
Here is one of the packages I tried to use:
library(formattable)
# Set custom colors
green <- "#71CA97"
red <- "#ff7f7f"
# Make example table
col1 <- c(1.2, 4.2, 5.6, 7.1)
col2 <- c(5.0, 1.3, 10.3, 6.0)
col3 <- c(4.7, 6.3, 1.5, 6.3)
mydata <- data.frame(col1, col2, col3)
# Define color_tile_mean function
color_tile_mean <- function (...) {
formatter("span", style = function(x) {
style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(x < mean(x) , red, green)) # Remember to change the colors!
})}
bboop <- formattable(mydata, align=c("c", "c", "c"),list(
col1=color_tile_mean(),
col2=color_tile_mean(),
col3=color_tile_mean()
))
htmlwidgets::saveWidget(widget = bboop, file = f)
#f is string of filepath
Error when trying to use htmlwidgets::saveWidget
:
Error in .getNamespace(pkg) :
invalid type/length (symbol/0) in vector allocation
Does anyone have any advice? Sorry this is long. I wanted to be thorough.