如何在R中保存flextable作为png

时间:2018-05-08 04:05:16

标签: r png viewer flextable

我已经按照链接R save FlexTable as html file in script的建议,但似乎我遇到了一个不同的问题,因为这个解决方案对我不起作用。 vanilla.table ()函数生成flextable ()函数以外的对象。

我正在使用flextable因为它允许所需的格式化可能性

示例:

library(flextable)
library(rtable)

# The example below work.
myft <- vanilla.table(
   head(mtcars) )
myft
writeLines(as.html(myft), "MyFlexTable.html")

# The example below does not work.
myft <- regulartable(
  head(mtcars), 
  col_keys = c("am", "carb", "gear", "mpg", "drat" ))
myft
writeLines(as.html(myft), "MyFlexTable.html")

ps:我知道可以通过点击“导出&gt;另存为图像”手动下载照片,但我需要它编程

提前感谢!

1 个答案:

答案 0 :(得分:3)

要将flextable保存为png,首先需要将其保存为html文件,然后使用webshot从html文件中获取png。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
import sklearn.cross_validation

# Load the data
oecd_bli = pd.read_csv("C:/Users/Excel/Desktop/Briefcase/PDFs/ALL PYTHON & R CODE SAMPLES/Hands-On Machine_Learning_with_Scikit_Learn_and_Tensorflow/GDP Per Capita/oecd_bli.csv", thousands=',')

gdp_per_capita = pd.read_csv("C:/Users/Excel/Desktop/Briefcase/PDFs/ALL PYTHON & R CODE SAMPLES/Hands-On Machine_Learning_with_Scikit_Learn_and_Tensorflow/GDP Per Capita/gdp_per_capita.csv",thousands=',')

# view first 10 rows of data frame
oecd_bli[:10]
gdp_per_capita[:10]


country_stats = pd.merge(oecd_bli, gdp_per_capita, left_index=True, right_index=True)
country_stats[:10]

X = np.c_[country_stats["GDP"]]
Y = np.c_[country_stats["VALUE"]]
print(X)
print(Y)


# Visualize the data
country_stats.plot(kind='scatter', x="GDP", y='VALUE')
plt.show()



# Select a linear model
lin_reg_model = sklearn.linear_model.LinearRegression()
# Train the model
lin_reg_model.fit(X, Y)
# Make a prediction for Cyprus
X_new = [[22587]] # Cyprus' GDP per capita
print(lin_reg_model.predict(X_new))

enter image description here