I have this project idea in my mind and I am trying to use Shiny and interface with Python. To see if it can work I made this simple test app:
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")
})
}
and the python script (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
My problem is that the R variables rvar1 & rvar2 don't update from 0 to 1. How can I get those variables to update to the corresponding value of blink1 & blink2 from the Python script? Is it even possible using the rPython package? If not, any suggestions on how this can be done?
Thank you!