Ich habe diese Projektidee im Kopf und versuche Shiny zu verwenden und mit Python zu interagieren. Um zu sehen, ob es funktionieren kann ich gemacht diesen einfachen Test App:Wie bekomme ich eine Python-Variable in R?
UI:
# UI
library(shiny)
library(shinydashboard)
### Dashboard sidebar erstellen
sidebar <- dashboardSidebar(
)
body <- dashboardBody(
fluidPage(
# infoboxen
valueBoxOutput("box1"),
infoBoxOutput("box2"),
# buttons
actionButton("but1", "Change Value 1 to TRUE"),
actionButton("but2", "Change Value 2 to TRUE"),
actionButton("but3", "Change Value 1 to FALSE"),
actionButton("but4", "Change Value 2 to FALSE")
)
)
# Hier kommt alles zusammen
shinyUi <- dashboardPage(skin = "blue",
dashboardHeader(title = "Python to R Test"),
sidebar,
body
)
Server:
# Server für Test App
library(rPython)
library(shiny)
library(shinydashboard)
shinyServer <- function(input, output) {
# python scrip laden
python.load("python_script.py")
# python variable einer R variable zuweisen
rvar1 <- python.get("blink1")
rvar2 <- python.get("blink2")
# Buttons
observeEvent(input$but1, {
python.call("func1", bool1 = TRUE)
})
observeEvent(input$but2, {
python.call("func2", bool2 = TRUE)
})
observeEvent(input$but3, {
python.call("func1", bool1 = FALSE)
})
observeEvent(input$but4, {
python.call("func2", bool2 = FALSE)
})
# Infobox
output$box1 <- renderValueBox({
valueBox(rvar1, width = 3, icon = NULL, href = NULL, subtitle = "test", color = "green")
})
output$box2 <- renderInfoBox({
infoBox(rvar2, width = 3, "Status", subtitle = "test", color = "blue")
})
}
und der Python-Skript (python_script.py):
#!/usr/bin/python
# Script das eine Variable blinkt/Zweck: integration mir R
blink1 = 0
blink2 = 0
def func1(bool1):
if bool1 == True:
blink1 = 1
print blink1
else:
blink1 = 0
print blink1
return blink1
def func2(bool2):
if bool2 == True:
blink2 = 1
print blink2
else:
blink2 = 0
print blink2
return blink2
Mein Problem ist, dass die R-Variablen rvar1 & rvar2 nicht von 0 auf 1 aktualisieren. Wie kann ich diese Variablen auf den entsprechenden Wert von blink1 & blink2 aus dem Python-Skript aktualisieren? Ist es überhaupt möglich, das rPython-Paket zu verwenden? Wenn nicht, irgendwelche Vorschläge, wie dies getan werden kann?
Vielen Dank!
Versuchen Hinzufügen 'return' Zeilen in Python Funktionen. – Parfait
Danke, aber es funktioniert nicht. – naitbas
Wirklich? Wie es ist, gibt "print" keinen Wert von der Funktion zurück. Bitte zeigen Sie, wie Sie die Rendite hinzugefügt haben. Siehe [hier] (https://stackoverflow.com/a/15441904/1422451). – Parfait