【问题标题】:Setting the width of formattable() object设置 formattable() 对象的宽度
【发布时间】:2016-06-25 15:46:21
【问题描述】:

我正在使用 formattable 包 (AWESOME PACKAGE) 对我从网络上抓取的数据表进行一些条件格式化。我想将生成的对象与另一个 formattable() 表并排放置在 html 文档中。但是,我无法弄清楚如何设置表格的宽度(在函数中或在 knitr 块中),因此无法将两个对象并排放置。

这是我的代码。我意识到这很草率。有人可以帮我在.rmd knitting to html中并排放置两张同一张桌子吗?它实际上是两个不同的 formattable() 表,但这并不重要。

library(formattable, quietly = T)
library(rvest, quietly = T)



#Part 1: Air pollution forecast from sparetheair.org
#read the page
#0-50 Good Green
#51-100 Moderate Yellow
#101-150 Unhealthy for Sensitive Groups Orange
#151-200 Unhealthy  Red
#201-300 Very Unhealthy Purple --> Also red for simplicity
air_quality <- read_html("http://sparetheair.org/stay-informed/todays-     air-quality/five-day-forecast")
#Scrape table
box <- html_nodes(air_quality,"div div .f5day")
content <- html_text(box)
#Text cleaning, removing artifacts
fulltext <- gsub("\n"," ",content)
fulltext1 <- gsub("\n"," ",fulltext)
fulltext2 <- gsub("\r"," ", fulltext1)
fulltext3 <- gsub("\t"," ", fulltext2)
#split the string into a character vector of strings
mytext <- unlist(strsplit(fulltext3," "))
#Leave empty spaces out, 
wholething <- mytext[mytext != ""]
#vector of days
Days <- wholething[1:5]
#character data for table construction
wholedata <- wholething[6:69]
#a subset of Oakland related data
baydata <- wholedata[13:26]  
#convert to data frame exactly as it is on website
mydf <- data.frame(rbind(baydata[5:9], baydata[10:14]),    row.names=c("AQI","PMI"))
#transposed data frame (tidy data, observations in rows, variables in cols)
tmydf <- data.frame(cbind(Days, baydata[5:9], baydata[10:14]), row.names = NULL, stringsAsFactors = F)
#replace the names of the new df with the webscraped days of the week
names(tmydf) <- c("Day","AQI","PMI")
tmydf$PMI <- gsub("PM","",tmydf$PMI)
#Print to an html table the resulting dataframe
#eventually this will have some color coding or reactive element
#focus only on numeric variables
smaller <- tmydf[1:2,]
#Coerce to numeric
smaller$AQI <- as.numeric(smaller$AQI)
smaller$PMI <- as.numeric(smaller$PMI)
#Format ouput based on scale above
nicetable <- formattable(smaller, 
                         list( 
                             AQI = formatter("span", style = x ~ 
                                                 ifelse(x <= 50, style(display = "block",`border-radius` = "4px",
                                                                       padding = "0 4px",background = "green", 
                                                                       color = "white", font.weight = "bold"), 
                                                        ifelse(x <= 100, style(display = "block",`border-radius` = "4px",
                                                                               padding = "0 4px", background = "yellow", 
                                                                               color ="white", font.weight = "bold"), 
                                                               style(display = "block",`border-radius` = "4px",
                                                                     padding = "0 4px",background = "red", 
                                                                     color = "white", font.weight = "bold") ) ) ), 
                             PMI = formatter("span",style = x ~ 
                                                 ifelse(x == 2.5, style(display = "block",`border-radius` = "4px",
                                                                        padding = "0 4px",background = "red", 
                                                                        color = "white", font.weight = "bold"), 
                                                        style(display = "block",`border-radius` = "4px",
                                                              padding = "0 4px",background = "green", 
                                                              color = "white", font.weight = "bold") ) )
                         )
)

nicetable

GithubRepo

【问题讨论】:

  • 我去看看。抱歉,我忘记通过电子邮件回复了。
  • 哦,谢谢肯特!如果您没有时间,请不要担心。

标签: html r knitr r-markdown


【解决方案1】:

我将尝试在代码中进行解释,但要指定宽度,我们只需手动将 formattable 转换为 htmlwidgetas.htmlwidget()。这也应该适用于rmarkdown

library(formattable, quietly = T)
library(rvest, quietly = T)



#Part 1: Air pollution forecast from sparetheair.org
#read the page
#0-50 Good Green
#51-100 Moderate Yellow
#101-150 Unhealthy for Sensitive Groups Orange
#151-200 Unhealthy  Red
#201-300 Very Unhealthy Purple --> Also red for simplicity
air_quality <- read_html("http://sparetheair.org/stay-informed/todays-air-quality/five-day-forecast")
#Scrape table
box <- html_nodes(air_quality,xpath="//div//div[contains(@class, 'f5day')]")
content <- html_text(box)
#Text cleaning, removing artifacts
fulltext <- gsub("\n"," ",content)
fulltext1 <- gsub("\n"," ",fulltext)
fulltext2 <- gsub("\r"," ", fulltext1)
fulltext3 <- gsub("\t"," ", fulltext2)
#split the string into a character vector of strings
mytext <- unlist(strsplit(fulltext3," "))
#Leave empty spaces out, 
wholething <- mytext[mytext != ""]
#vector of days
Days <- wholething[1:5]
#character data for table construction
wholedata <- wholething[6:69]
#a subset of Oakland related data
baydata <- wholedata[13:26]  
#convert to data frame exactly as it is on website
mydf <- data.frame(rbind(baydata[5:9], baydata[10:14]),    row.names=c("AQI","PMI"))
#transposed data frame (tidy data, observations in rows, variables in cols)
tmydf <- data.frame(cbind(Days, baydata[5:9], baydata[10:14]), row.names = NULL, stringsAsFactors = F)
#replace the names of the new df with the webscraped days of the week
names(tmydf) <- c("Day","AQI","PMI")
tmydf$PMI <- gsub("PM","",tmydf$PMI)
#Print to an html table the resulting dataframe
#eventually this will have some color coding or reactive element
#focus only on numeric variables
smaller <- tmydf[1:2,]
#Coerce to numeric
smaller$AQI <- as.numeric(smaller$AQI)
smaller$PMI <- as.numeric(smaller$PMI)
#Format ouput based on scale above
nicetable <- formattable(smaller, 
                         list( 
                           AQI = formatter("span", style = x ~ 
                                             ifelse(x <= 50, style(display = "block",`border-radius` = "4px",
                                                                   padding = "0 4px",background = "green", 
                                                                   color = "white", font.weight = "bold"), 
                                                    ifelse(x <= 100, style(display = "block",`border-radius` = "4px",
                                                                           padding = "0 4px", background = "yellow", 
                                                                           color ="white", font.weight = "bold"), 
                                                           style(display = "block",`border-radius` = "4px",
                                                                 padding = "0 4px",background = "red", 
                                                                 color = "white", font.weight = "bold") ) ) ), 
                           PMI = formatter("span",style = x ~ 
                                             ifelse(x == 2.5, style(display = "block",`border-radius` = "4px",
                                                                    padding = "0 4px",background = "red", 
                                                                    color = "white", font.weight = "bold"), 
                                                    style(display = "block",`border-radius` = "4px",
                                                          padding = "0 4px",background = "green", 
                                                          color = "white", font.weight = "bold") ) )
                         )
)

nicetable

#  so what happens is a formattable object
#   gets converted to an htmlwidget
#   for viewing when interactive
#  to specify a width
#   we have to do the htmlwidget conversion ourselves
#   with as.htmlwidget
as.htmlwidget(nicetable, width=200)



# just use shiny to get helper fluid functions
library(shiny)
library(htmltools)

browsable(
  tagList(
    fluidRow(
      column(
        width = 6,
        as.htmlwidget(nicetable)
      ),
      column(
        width = 6,
        as.htmlwidget(nicetable)
      )
    )
  )
)

【讨论】:

  • 嗨 Kent,当我尝试在 rmd pandoc 中使用 as.htmlwidget 时,我实际上得到了这个错误:无法确定 /Library/Frameworks/R.framework/Versions/3.2/Resources/library/shiny/www/shared/bootstrap/css/../fonts/glyphicons-halflings-regular.woff2' Error: pandoc document conversion failed with error 1 Execution halted 的 mime 类型
  • 会认为 RStudio 会删除 woff2,但我猜他们没有。我还记得formattable包含bootstrap,所以我把答案改成了使用formattable bootstrap依赖。
  • 您好,肯特,感谢您的帮助。那么有没有办法可以删除style() 中的 woff2/更改字体?我看到你对格式化引导程序依赖的更新。泰!
  • 需要一个 pull 或 issue 到 Shiny
  • 感谢@timelyportfolio 领导 rstudio flexdashboard 模板!这解决了我试图在我的可格式化表格上格式化宽度的问题。 github.com/rstudio/flexdashboard
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
相关资源
最近更新 更多