如何不运行会出错的块

如何解决如何不运行会出错的块

我正在制作一个 html 文件,其中包含每周更新的图像。我正在使用 Rmarkdown 文件来指定图像的文件路径:

# -*- coding: Latin-1 -*-
import os
import math
import tkinter as tk
from tkinter import *
from tkinter import font


# Create an invisible root window.
root = tk.Tk()
root.attributes("-alpha",0.0)

# Create a Toplevel for the application.
win = tk.Toplevel(root)
win.overrideredirect(True)
win.geometry('+400+100')

# Make root forward taskbar minimize and restore events to Toplevel instance.
root.bind("<Unmap>",lambda _: win.withdraw())
root.bind("<Map>",lambda _: win.deiconify())


font1 = font.Font(family = 'Helvetica',size = 15)
font3 = font.Font(family = 'Helvetica',size = 8,weight = 'bold')
font2 = font.Font(family = 'Helvetica',size = 20,weight = 'bold')

def aaa(colora,colorb,colorc,colord,colore,colorf):
    Dark = Theme('#18181F','#0C0C0F',"#505063",'#D1DDFF','#D12319',"#D1665F")
    Ecrire('',Light)

def aaaa(colora,colorf):
    Light = Theme('#F1EDF5','#D1D2E8',"#EDD5EB",'#323133','#6F2EB0',"#C47ED6")
    Ecrire('',Dark)

def strl(list):

        list ="".join(list)
        return list

pixel = tk.PhotoImage(width=1,height=1)

a =95
b =95

theme = 0
t = 0

ecrit = []
nombre_de_multdiv = 0
nombre_de_sous_add = 0

def Del(self):
    global ecrit
    del ecrit[-1]
    affichage = strl(ecrit)
    self.label1['text'] = affichage
    self.label1.place(anchor = 'e',x = 450,y = 75)

def Del_all(self):
    global ecrit
    del ecrit[:]
    affichage = strl(ecrit)
    self.label1['text'] = affichage
    self.label1.place(anchor = 'e',y = 75)

def Ecrire(symbole,self):

    ecrit.append(symbole)
    affichage = strl(ecrit)
    self.label1['text'] = affichage
    self.label1.place(anchor = 'e',y = 75)


def Ecrire_Resulat(valeur,self):
    valeur = strl(valeur)
    self.label1['text'] = valeur

def Calcul(self):
    global nombre_de_multdiv
    global nombre_de_sous_add

    for i in range(len(ecrit)):
        if ecrit[i]  == '/' or ecrit[i] == '*':
            nombre_de_multdiv +=1
    for i in range(len(ecrit)):
        if ecrit[i]  == '+' or ecrit[i] == '-':
            nombre_de_sous_add +=1

    for i in range(nombre_de_multdiv):
        for i in range(len(ecrit)):
            if ecrit[i] == '*' or ecrit[i] == '/':
                numero = i
                nb = i
                nbb = i

                for i in range(len(ecrit)-numero-1):
                    if ecrit[i+1 + numero] != '/' and ecrit[i+1 + numero] != '*' and ecrit[i+1 + numero] != '+' and ecrit[i+1 + numero] != '-':
                        nb +=1
                    elif ecrit[i+1+numero] == '/' or ecrit[i+1+numero] == '*' or ecrit[i+1+numero] == '+' or ecrit[i+1+numero] == '-':
                        break

                for i in range(numero):
                    i = -i
                    if ecrit[i-1+numero] != '/' and ecrit[i-1+numero] != '*' and ecrit[i-1+numero] != '+' and ecrit[i-1+numero] != '-':
                        nbb -=1
                    elif ecrit[i-1+numero] == '/' or ecrit[i-1+numero] == '*' or ecrit[i-1+numero] == '+' or ecrit[i-1+numero] == '-':
                        break

                nombre1 = "".join(ecrit[numero+1:nb+1])
                nombre1 = float(nombre1)
                nombre2 = "".join(ecrit[nbb:numero])
                nombre2 = float(nombre2)

                if ecrit[numero] =='*':
                    resultat = nombre1 * nombre2
                    if resultat == int(resultat):
                        resultat = int(resultat)
                    resultat = list(str(round(resultat,5)))
                else:
                    resultat = nombre2 / nombre1
                    if resultat == int(resultat):
                        resultat = int(resultat)
                    resultat = list(str(round(resultat,5)))

                del ecrit[nbb:nb+1]
                for i in range(len(resultat)):
                    ecrit.insert(nbb+i,resultat[i])
                break

    for i in range(nombre_de_sous_add):
        for i in range(len(ecrit)):
            if ecrit[i] == '+' or ecrit[i] == '-':
                numero = i
                nb = i
                nbb = i

                for i in range(len(ecrit)-numero-1):
                    if ecrit[i+1 + numero] != '/' and ecrit[i+1 + numero] != '*' and ecrit[i+1 + numero] != '+' and ecrit[i+1 + numero] != '-':
                        nb +=1
                    elif ecrit[i+1+numero] == '/' or ecrit[i+1+numero] == '*' or ecrit[i+1+numero] == '+' or ecrit[i+1+numero] == '-':
                        break

                for i in range(numero):
                    i = -i
                    if ecrit[i-1+numero] != '/' and ecrit[i-1+numero] != '*' and ecrit[i-1+numero] != '+' and ecrit[i-1+numero] != '-':
                        nbb -=1
                    elif ecrit[i-1+numero] == '/' or ecrit[i-1+numero] == '*' or ecrit[i-1+numero] == '+' or ecrit[i-1+numero] == '-':
                        break

                nombre1 = "".join(ecrit[numero+1:nb+1])
                nombre1 = float(nombre1)
                nombre2 = "".join(ecrit[nbb:numero])
                nombre2 = float(nombre2)

                if ecrit[numero] =='+':
                    resultat = nombre1 + nombre2
                    if resultat == int(resultat):
                        resultat = int(resultat)
                    resultat = list(str(round(resultat,5)))
                else:
                    resultat = nombre2 - nombre1
                    if resultat == int(resultat):
                        resultat = int(resultat)
                    resultat = list(str(round(resultat,resultat[i])
                break

    Ecrire_Resulat(ecrit,self)


class Theme:
    def move_window(self,event):
        win.geometry('+{0}+{1}'.format(event.x_root - self.x,event.y_root - self.y))

    def set_xy(self,event):
        self.x=event.x_root - win.winfo_x()
        self.y=event.y_root - win.winfo_y()
        return self.x,self.y;

    def boutontheme(self,colora,colorf):
        self.openmenu(colora,colorf)
        if self.t == 0:
            Light = Theme('#F1EDF5',"#915ec4")
            self.t = 1
            Ecrire('',Light)
        else:
            Dark = Theme('#18181F',"#D1665F")
            self.t = 0
            Ecrire('',Dark)

    def openmenu(self,colorf):
        if self.ouvert == False:
            self.b_theme.config(bg = colore)
            if self.t == 0:
                self.b_theme_dark = tk.Button(win,state=disABLED,text = 'Dark theme',font = font3,borderwidth=0,highlightthickness=0,compound="c",image = pixel,height = 20,width = 70,activebackground =colore,bg =colora,fg = colord,command = lambda: self.boutontheme(colora,colorf))
                self.b_theme_light = tk.Button(win,state=norMAL,text = 'Light theme',colorf))
            else:
                self.b_theme_dark = tk.Button(win,colorf))

            self.b_theme_dark.place(anchor = 'nw',x = 5,y = 25)
            self.b_theme_light.place(anchor = 'nw',y = 45)
            self.ouvert = True
            return;

        if self.ouvert == True:
            self.b_theme.config(bg = colorb)
            self.b_theme_dark.destroy()
            self.b_theme_light.destroy()
            self.ouvert = False
            return;

    def __init__(self,colorf):
        if colora == '#18181F':
            self.t = 0
        else:
            self.t = 1

        self.ouvert = False
        self.x = 0
        self.y=0

        #creation menu

        self.canvas_menu = Canvas(win,width =500,height =25,bg = colora,highlightthickness=0)
#        self.exit = Canvas(bg = colora,highlightthickness=0)
        self.exit = Canvas(win,highlightthickness=0)
        self.exit.create_oval(0,20,fill= colore,width = 0)
        self.exit.create_text(10,10,text="x",fill = colord,font= font3)
#        self.exit.bind("<Button-1>",lambda e: win.destroy())
        self.exit.bind("<Button-1>",lambda e: root.destroy())

        #affichage menu

        self.exit.place(anchor = 'nw',x = 475,y = 2)
        self.canvas_menu.grid(row = 0,column = 0,columnspan = 5)

        #creation des canvas

        self.canvas_screen = Canvas(win,height =100,bg =colorb,highlightthickness=0)
        self.canvas_keyb_num = Canvas(win,width =300,height =400,highlightthickness=0)
        self.canvas_keyb_op = Canvas(win,width =200,highlightthickness=0)

        #affchage des canvas

        self.canvas_screen.grid(row = 1,columnspan = 5)
        self.canvas_keyb_num.grid(row = 2,columnspan = 3,rowspan = 4)
        self.canvas_keyb_op.grid(row = 2,column = 3,columnspan = 2,rowspan = 4)

        # creation des boutons

        self.b1 = Button(win,text ='1',activebackground =colorc,foreground=colord,height = a,width = b,font = font1,command=lambda: Ecrire('1',self))
        self.b2 = Button(win,text ='2',command=lambda: Ecrire('2',self))
        self.b3 = Button(win,text ='3',command=lambda: Ecrire('3',self))
        self.b4 = Button(win,text ='4',command=lambda: Ecrire('4',self))
        self.b5 = Button(win,text ='5',command=lambda: Ecrire('5',self))
        self.b6 = Button(win,text ='6',command=lambda: Ecrire('6',self))
        self.b7 = Button(win,text ='7',command=lambda: Ecrire('7',self))
        self.b8 = Button(win,text ='8',command=lambda: Ecrire('8',self))
        self.b9 = Button(win,text ='9',command=lambda: Ecrire('9',self))
        self.b_point = Button(win,text ='.',command=lambda: Ecrire('.',self))
        self.b0 = Button(win,text ='0',command=lambda: Ecrire('0',self))

        self.b_plus = Button(win,text ='+',command=lambda: Ecrire('+',self))
        self.b_moins = Button(win,text ='-',command=lambda: Ecrire('-',self))
        self.b_fois = Button(win,text ='*',command=lambda: Ecrire('*',self))
        self.b_diviser = Button(win,text ='/',command=lambda: Ecrire('/',self))
        self.b_del = Button(win,text ='DEL',command=lambda: Del(self))
        self.b_del_all = Button(win,text ='CE',command=lambda: Del_all(self))
        self.b_egale = Button(win,text ='=',bg = colore,activebackground =colorf,width = 295,command= lambda: Calcul(self))

        #affichage des nombres

        #colone 1

        self.b7.grid(column = 0,row = 2)
        self.b4.grid(column = 0,row = 3)
        self.b1.grid(column = 0,row = 4)
        self.b_point.grid(column = 0,row = 5)

        #colone 2

        self.b8.grid(column = 1,row = 2)
        self.b5.grid(column = 1,row = 3)
        self.b2.grid(column = 1,row = 4)
        self.b0.grid(column = 1,row = 5)

        #colone 3

        self.b9.grid(column = 2,row = 2)
        self.b6.grid(column = 2,row = 3)
        self.b3.grid(column = 2,row = 4)

        #operateurs

        self.b_plus.grid(column = 3,row = 2)
        self.b_moins.grid(column = 4,row = 2)
        self.b_fois.grid(column = 3,row = 3)
        self.b_diviser.grid(column = 4,row = 3)
        self.b_del.grid(column = 3,row = 4)
        self.b_del_all.grid(column = 4,row = 4)
        self.b_egale.grid(column = 2,row = 5,columnspan = 3)

        #ecriture

        self.label1 = tk.Label(win,text = '',justify = tk.RIGHT,font = font2,fg = colord)
        self.label1.place(anchor = 'e',y = 75)
        self.b_theme = tk.Button(win,text = 'Themes',width = 50,command= lambda: self.openmenu(colora,colorf))
        self.b_theme.place(anchor = 'nw',y = 2)

        self.canvas_menu.bind('<1>',self.set_xy)
        self.canvas_menu.bind('<B1-Motion>',self.move_window)

if theme == 0:
    Dark = Theme('#18181F',"#D1665F")
else:
    Light = Theme('#F1EDF5',"#915ec4")

#win.title('calculator')
win.master.title('Calculator')
#win.resizable(height=False,width=False)  # Not needed with overrideredirect.
win.mainloop()

我将每周生成这些报告,所以一周我们可能有用户 2 的图像,而一周我们可能没有用户 2 的图像。所以我知道我可以编织并运行 html 并设置错误 =真的。但这仍然会在缺少图像的 html 输出显示错误消息。有没有办法设置一个条件,如果文件不存在,该块将不会在我读入文件的地方运行?我使用magick包从参数中指定的路径中读取每个图像。

解决方法

您也可以将 R 语句传递给 knitr 选项,如下所示。

---
title: "test conditional chunks"
output: html_document
params: 
  user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
  user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---

```{r setup,include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r condition 1,echo=FALSE,eval=file.exists(params$user1)}
# plot image
```

```{r condition 1,eval=file.exists(params$user2)}
# plot image
```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?