如何在R中重新获取Python变量?

如何在R中重新获取Python变量?,python,r,shiny,Python,R,Shiny,我脑子里有这个项目的想法,我正在尝试使用Shiny和Python接口。为了看看它是否可以工作,我制作了这个简单的测试应用程序: 用户界面: 我的问题是R变量rvar1和rvar2不会从0更新为1。如何从Python脚本中将这些变量更新为相应的blink1和blink2值?甚至可以使用rPython包吗?若否,有何建议? 谢谢大家! 在Python中仍然需要return,只需将Python.call()分配给RVAR即可: #按钮 ObserveeEvent(输入$but1{ rvar1尝试在Py

我脑子里有这个项目的想法,我正在尝试使用Shiny和Python接口。为了看看它是否可以工作,我制作了这个简单的测试应用程序:

用户界面:

我的问题是R变量rvar1和rvar2不会从0更新为1。如何从Python脚本中将这些变量更新为相应的blink1和blink2值?甚至可以使用rPython包吗?若否,有何建议?


谢谢大家!

在Python中仍然需要
return
,只需将
Python.call()
分配给RVAR即可:

#按钮
ObserveeEvent(输入$but1{

rvar1尝试在Python函数中添加
return
行。谢谢,但它不起作用。真的吗?按原样,
print
不会从函数中返回任何值。请演示如何添加return。请参见。另外,您是否按了
TRUE
value按钮?以及
Python.call
删除赋值并传递t/F。如果我在Shiny中按下按钮,我可以看到Python中变量的变化,这就是我打印的原因。删除
Python.call中的赋值也没有帮助。这对你有用吗?Shiny应用程序中的值仍然没有变化。我编辑了上面的帖子,将
return
包含在R中。谢谢事件,运行:
print(rvar1)
print(rvar2)
并检查控制台。值是否更改?如果更改,可能是渲染框调用。非常酷!值确实会更改,但不会显示在框输出中。如果变量更改,框不应该更新吗?请参阅教程,调用为:
valueBox(“Title”),输入$count,…)
。因此,先添加标题,然后再添加RVAR。输出仍然不变。我尝试了许多其他变体,例如,有无图标和字幕等,都没有帮助。
# 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 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")
  })
}
#!/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
# 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)
})
...