English 中文(简体)
如何通过四舍五入绘图值缩小 R 绘图 html 输出的大小
原标题:How to reduce the size of R plotly html output by rounding plot values

我试图缩小我的 RMarkdown html 报告的大小, 更重要的是, 使其快速打开。 html 的报告包含大量的 R 绘图图, 每个图中包含大量数据点 (1000+) 。 考虑到 R Plotly 在 html 文件中存储每个图中的全部原始数据, 我认为减少文件大小的一个很好的选择是四舍五入数据中的小数位。 然而, 我发现, 尽管输入数据是四舍五入的, R Plotly 在 html 文件里仍然保留大量小数位。 因此, 如果数据四舍五入, 文件大小不会减少 。

2个案例,见下文载有原始数据的基数,以及包含四舍五入数据的四舍五入案例。两个案例的文件大小相同。

<强度 > 基准案例 HTML

RawData <- data.frame(Date = seq(as.Date("2024/1/1"), by = "month", length.out = 12),
                      PreciseValue = c(0.1516270, 0.3542629, 0.8339342, 0.5796813, 0.3933472, 0.2937137, 0.1779205, 0.4285533, 0.6841885, 0.3399411,0.99476560, 0.42941527))
RawData$RoundValue <- round(RawData$PreciseValue,2)
fig <- plot_ly(RawData, type =  scatter , mode =  lines )%>%
  add_trace(x = ~Date, y = ~PreciseValue, name =  PreciseValue )
saveWidget(fig, "plotly_base.html", selfcontained = TRUE)

The html file size is 3780kb. If I open the html file and look at the underlying R Plotly data, the stored y data is:

"y":[0.15162704353000001,0.35426295622999998,0.83393426323999997,0.57968136341999998,0.39334726234,0.29371352347000002,0.17792423404999999,0.44352285533000002,0.68418423485000002,0.36623994110000002,0.99476432455999997,0.42941523452699998]

请注意小数点位数比原始数据中位数多。

<强度 > 定位值案例

RawData$RoundValue <- round(RawData$PreciseValue,2)
fig <- plot_ly(RawData, type =  scatter , mode =  lines )%>%
  add_trace(x = ~Date, y = ~RoundValue, name =  RoundValue )

saveWidget(fig, "plotly_round.html", selfcontained = TRUE)

The html file size for the round case is also 3780kb. The underlying data for this case is

"y":[0.14999999999999999,0.34999999999999998,0.82999999999999996,0.57999999999999996,0.39000000000000001,0.28999999999999998,0.17999999999999999,0.44,0.68000000000000005,0.37,0.98999999999999999,0.42999999999999999]

存储的 Y 数据应该像

"y":[0.15, 0.35, 0.83, 0.58, 0.39, 0.29, 0.18, 0.44, 0.68, 0.37, 0.99, 0.43]

有人知道如何配置 R Plotly 以只存储 html 输出中小数位的配置数吗?

问题回答

你做得到的,你做得到的

library(plotly)
library(htmlwidgets)

RawData <- data.frame(
  Date = seq(as.Date("2024/1/1"), by = "month", length.out = 12),
  PreciseValue = c(0.1516270, 0.3542629, 0.8339342, 0.5796813, 0.3933472, 0.2937137, 0.1779205, 0.4285533, 0.6841885, 0.3399411, 0.99476560, 0.42941527)
)

RawData$RoundValue <- round([![enter image description here][1]][1]RawData$PreciseValue, 2)

fig <- plot_ly(RawData, type =  scatter , mode =  lines ) %>%
  add_trace(x = ~Date, y = ~RoundValue, name =  RoundValue )

saveWidget(fig, "plotly_round.html", selfcontained = TRUE)

给予





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签