Я пытаюсь запустить R-скрипт в bash из python, получить строку json из этого R-скрипта и использовать ее в python скрипте.
странная вещь это работает, когда я запускаю Rscript в начале моего python сценария. но когда он пытается запустить его после запуска gui, он выдает ошибку в сценарии R. последняя строка, где я могу выполнить свои подпроцессы, находится прямо перед window = TK ()
, сообщение об ошибке: Линия между ** это проблема. в первый раз, когда он работает нормально, в седьмой раз это не так:
import subprocess
import json
import pandas as pd
from tkinter import *
from tekstprint import question
Qresponse = "please run file first"
Rresponse = "please run secund file first"
execcommand = '/PROGRA~1/R/R-3.6.2/bin/Rscript --vanilla /path/RScripts/Rscript_Inputargs.R ' + question()
**Rresponse = subprocess.check_output(execcommand, shell=False) **
def clicked2():
clicked1()
execcommand = '/PROGRA~1/R/R-3.6.2/bin/Rscript --vanilla /path/Rscript_Inputargs.R ' + question()
**Rresponse = subprocess.check_output(execcommand, shell=False)**
data = json.loads(Rresponse)
df = pd.DataFrame(data)
output2.configure(text=df)
# gui start code
#window
window = Tk()
window.title("pryte converter")
#firstframe
frame1 = Frame(window)
frame1.pack(side = LEFT)
#secund frame
frame2 = Frame(window)
frame2.pack(side = LEFT)
#runbuttons
def clicked1():
Qresponse = question()
output1.configure(text=Qresponse)
print(Qresponse)
btn1 = Button(frame1, text="run first file" , command=clicked1)
btn1.grid(column=0 , row=1)
btn2 = Button(frame2, text="run secund file" , command=clicked2)
btn2.grid(column=0 , row=1)
output1 = Label(frame1, text=Qresponse)
output1.grid(column=0, row=0)
output2 = Label(frame2, text=Rresponse)
output2.grid(column=0 , row=0)
window.mainloop()
код R:
library(jsonlite)
options(echo=FALSE) # if you want see commands in output file
args <- commandArgs(trailingOnly = TRUE)
#print(args)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandArgs(trailingOnly=FALSE))
start_date <- as.Date(args[1])
name <- args[2]
n <- as.integer(args[3])
rm(args)
# Some computations:
x <- rnorm(n)
png(paste(name,".png",sep=""))
plot(start_date+(1L:n), x)
#dev.off()
y <- summary(x)
df = data.frame(y=matrix(y),row.names=names(y))
print(toJSON(df))