Search code examples
pythonrshinyinstall.packages

Running a Python script in R Script is giving error


I want to run a Python script in RShiny. I'm doing this by source() function but it is giving me error. I'm very new to R. Do anyone know how can I run task1 in Server.R? My OS is Windows.

server.R:

library(shiny) #library
library(shinydashboard) #library
library(devtools)
shinyServer(function(input, output) {
  observeEvent(input$actionID,{
    source("task1.py")
  })
})

task1.py:

from PIL import Image
print("Mayday! Mayday!")

Error that I'm getting is:

Warning: Error in source: task1.py:6:6: unexpected input
5: 
6: from PIL
        ^
  73: source

Solution

  • That won't work because source() implies that you want the file to be interpreted by R, not by Python.

    You can use system() for any command you want to send to the OS, including running Python against a .py.

    Example:

    system("C:\\Python27\\python.exe my_script.py")
    

    You can specify if R should wait and if any output should be captured, etc.