1

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!

naitbas
  • 11
  • 7

1 Answers1

1

Still requiring the return in Python, simply assign the python.call() to the rvars:

# Buttons
observeEvent(input$but1, {
   rvar1 <- python.call("func1", TRUE)
})

observeEvent(input$but2, {
   rvar2 <- python.call("func2", TRUE)
})

observeEvent(input$but3, {
   rvar1 <- python.call("func1", FALSE)
})

observeEvent(input$but4, {
   rvar2 <- python.call("func2", FALSE)
})
...
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Did this work for you? The value still doesn't change in the Shiny App. I edited the post above to include the `return`. thanks. – naitbas Jul 01 '17 at 16:21
  • Hmmm...in R after the observeEvents, run: `print(rvar1)` and `print(rvar2)` and check console. Do values change? If so, it might be your render boxes call. – Parfait Jul 01 '17 at 16:41
  • Very cool! The values do change as they should, but it just doesn't show in the box output. Shouldn't the boxes update if the variable changes? – naitbas Jul 01 '17 at 16:52
  • Look at tutorial on [render boxes](https://www.rdocumentation.org/packages/shinydashboard/versions/0.5.3/topics/renderValueBox) with call as: `valueBox("Title", input$count, ... )`. So add title then your rvars. – Parfait Jul 01 '17 at 16:58
  • The output still doesn't change. I tried a bunch of other variations, e.g. with and without icon and subtitle and so on, it doesn't help. – naitbas Jul 01 '17 at 17:23