我为 knitr 创建了一个自定义块引擎,knitr::knit_engines$set()它采用一些 YAML 规范,解释它们,并创建一个绘图作为输出。如果 knitr 文档呈现为 HTML,我会创建闪亮的输出,我将其转换为 HTML, htmltools::knit_print.shiny.tag()并且效果很好。但是,如果我渲染为 pdf/docx/odf,我会创建一个静态绘图对象而不是闪亮的输出。那个位很好,但我可以让它呈现的是绘图的文本表示(即列表对象),而不是绘图的图像。
我需要以某种方式将图像(我假设)传递给 knitr::knit_engines$set() 的输出,但是如果我尝试将绘图保存为 png,然后将其作为输出传递,我只会得到文件的文本表示路径代替。
knitr::knit_engines$set(example = function(options) {
#### read options$code and do stuff - works fine
p <- ggplot(data = mpg) + geom_jitter(aes(x = cty, y = hwy))
##### output results
# Shiny - outputs html text. Works fine
if(knitr::opts_knit$get("rmarkdown.pandoc.to") == "html"){
shiny::plotOutput(p) %>%
htmltools::knit_print.shiny.tag()
# Static output formats (pdf, etc)
} else {
#### attempt 1 ####
p # returns list object
#### attempt …Run Code Online (Sandbox Code Playgroud) 我有一组测量的树木直径,我试图绘制一个直方图,核心密度估计叠加在python的顶部.seaborn模块让我这么简单,但是我找不到指定kde对负数应该为零的方法(因为树不能有负树直径).
我目前得到的是:
seaborn.distplot(C77_diam, rug=True, hist=True, kde=True)
Run Code Online (Sandbox Code Playgroud)
我查看了seaborn.kdeplot这是distplot调用的函数,但找不到任何有用的函数.有没有人知道这是否可以用seaborn完成,如果没有,是否可以更普遍地使用matplotlib?
我只是开始使用seaborn,因为我无法弄清楚如何用pyplot.hist()覆盖kde pyplot.plot().
我正在尝试构建一个简单的程序来提醒我在使用计算机时休息一下.我对python有一个合理的理解,但以前从未玩过GUI编程或线程,所以以下基本上是从stackoverflow复制/粘贴:
import threading
import time
import Tkinter
class RepeatEvery(threading.Thread):
def __init__(self, interval, func, *args, **kwargs):
threading.Thread.__init__(self)
self.interval = interval # seconds between calls
self.func = func # function to call
self.args = args # optional positional argument(s) for call
self.kwargs = kwargs # optional keyword argument(s) for call
self.runable = True
def run(self):
while self.runable:
self.func(*self.args, **self.kwargs)
time.sleep(self.interval)
def stop(self):
self.runable = False
def microbreak():
root = Tkinter.Tk()
Tkinter.Frame(root, width=250, height=100).pack()
Tkinter.Label(root, text='Hello').place(x=10, y=10)
threading.Timer(3.0, root.destroy).start()
root.mainloop()
return()
thread = …Run Code Online (Sandbox Code Playgroud) 我原来的python是:
if i.count('<') and i.count('>') == (0 or 1):
pass
else:
print('error')
Run Code Online (Sandbox Code Playgroud)
这传递i ='<>'并失败,i ='<>>'这就是我想要的.然而它也失败了i =''我不想要也无法理解.
在ipython3中,我已经摆弄了这么长时间才能归结为抽象的
0 == (0 or 1)
Run Code Online (Sandbox Code Playgroud)
奇怪地返回False.我猜这与0 =假1 =真有关,但即使经过相当多的谷歌后,它对我来说仍然没有意义.
我是否真的需要将原始代码改写为更长时间,而且我的想法更加丑陋:
(i.count('<') and i.count('>') == 0) or (i.count('<') and i.count('>') == 1)
Run Code Online (Sandbox Code Playgroud)