59
Web + Stata Web + Stata Alexander Zlotnik Technical University of Madrid, ETSIT, DIE Ramon y Cajal University Hospital Stata is a registered trademark of StataCorp LP, College Station, TX, USA.

Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Web + Stata Web + Stata

Alexander ZlotnikTechnical University of Madrid, ETSIT, DIETechnical University of Madrid, ETSIT, DIERamon y Cajal University Hospital

Stata is a registered trademark of StataCorp LP, College Station, TX, USA.

Page 2: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

User-contributed programs

ssc install <program>

findit <program>

(runs both search and net search)

net from http://www.website.com/net from http://www.website.com/

manually copy program files to

C:\ado\plus\<subdir>\

Page 3: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Sometimes this is Sometimes this is not enough

Page 4: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Sometimes your program…

… requires complex interactions with

external software packagesexternal software packages

(ex: WinBUGS, MATLAB, Maxima, AnyLogic)

… uses proprietary data sources

(ex: real-time currency exchange rates)(ex: real-time currency exchange rates)

… uses proprietary source code

Page 5: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Sometimes your audience…

… does not have the version of Stata your

program requiresprogram requires

… does not have Stata at all

… does not have a PC, but may … does not have a PC, but may

have a smartphone with a web browser

Page 6: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

What if…?

Your programPrivate

data sources

Stata / Mata

Externalprograms

Stata / Mata

Your server

Page 7: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

What if…?

Web interfaceYour program

Stata / Mata

Private data sources

Externalprograms

Access from any device

Stata / Mata

Your server

Page 8: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

What if…?

Web interfaceYour program

Stata / Mata

Private data sources

Externalprograms

Access from any device

Stata / Mata

Security: client isolation

Page 9: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 1:

Translate Stata / Mata program into

Your program

Translate Stata / Mata program into

a general-purpose programming language used

in web applications.

Ex: Java, C / C++, C#, ASP.net + VB.net, Python,

Ruby, etc

Page 10: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 1:

Translate Stata / Mata program into

Your program

Translate Stata / Mata program into

a general-purpose programming language used

in web applications.

Ex: Java, C / C++, C#, ASP.net + VB.net, Python,

Ruby, etc

- Few numerical libraries

- May not have the same functions

- Functions may not be implemented

in the same way

-- subtle errors

-- numerical precision issues

-- performance issues

Page 11: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 2:

Translate Stata / Mata program into

Your program

Translate Stata / Mata program into

R & RShiny or SAS Stored Process Web

Application

Page 12: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 2:

Translate Stata / Mata program into

Your program

Translate Stata / Mata program into

R & RShiny or SAS Stored Process Web

Application

- Still requires a laborious translation

in most cases

- Again, functions may not be implemented - Again, functions may not be implemented

in the same way

- RShiny is a nice alternative but the free

version only supports one concurrent session

Page 13: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 3:

Use a slightly modified version of your

Your program

Use a slightly modified version of your

existing Stata program in a web application.

Stata is a registered trademark of StataCorp LP, College Station, TX, USA,

and the Stata logo is used with the permission of StataCorp.

Page 14: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

How?

Option 3:

Use a slightly modified version of your

Your program

Use a slightly modified version of your

existing Stata program in a web application.

-- In this presentation, we will see how to build a

web application using your Stata program, with

minimal modifications based on Stata/IC, Stata/SE

or Stata/MP.

-- Very similar techniques can be used with

Numerics for Stata.

Stata is a registered trademark of StataCorp LP, College Station, TX, USA,

and the Stata logo is used with the permission of StataCorp.

Page 15: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Technologies

Program core: Stata + Mata

Web application language: PHP

Web server: Apache

Operating system: Windows

Page 16: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Technologies

Program core: Stata + Mata

Web application language: PHP

Web server: Apache

Operating system: Windows

Well-known

Easy to use

Page 17: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Technologies

Program core: Stata + Mata

Web application language: PHP

Web server: Apache Open source

Operating system: Windows

Well-known

Easy to use

Page 18: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Web application language

PHP implementation example

Other languages may also be used:

- Java (servlets, JSPs)

- Python

- ASP / ASP.net + C# / VB.net- ASP / ASP.net + C# / VB.net

- C/C++, Perl (CGI interface)

-et cetera

Page 19: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Web server

Apache implementation example

Other web servers, application containers and

application servers may also be used:

- Tomcat

- JBoss- JBoss

- Oracle WebLogic

- IBM WebSphere

- Magic xpa

-et cetera

Page 20: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Operating system

It should be possible to do this on any

operating system that supports Stataoperating system that supports Stata

(i.e. Windows, Unix/Linux, Mac OS X).

Page 21: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

General idea

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Mata

Web server /

Application serverStata IC / SE / MP

Operating system

Page 22: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Mata

Web server /

Application serverStata IC / SE / MP

Operating system

Stata command(s)

Page 23: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Matatext files

images

data files

log files

Web server /

Application serverStata IC / SE / MP

Operating system

Page 24: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Simplified example

Page 25: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Web interface

(HTML / JS)

Calling Stata

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Mata

Web server /

Application serverStata IC / SE / MP

Operating system

Stata command(s)

Page 26: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Page 27: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

<html><head> Web interface </head><head> Web interface </head><body> <form action=“call_stata.php" method="post">

Stata command(s):<br><br><textarea name="stata_commands" ><br><br><input type="submit" value="Send command(s) to Stata" >

</form></body></html>

Page 28: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Mata

Web server /

Application serverStata IC / SE / MP

Operating system

Stata command(s)

Page 29: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

call_stata.phpcall_stata.phpcall_stata.phpcall_stata.php

<?php<?php<?php<?php............

$stata_commands = $_POST[“stata_commands”];

write_stata_do_file($stata_commands);

execute_stata_do_file();

............>>>>

Page 30: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Our web application will execute:

<<<<path_to_Statapath_to_Statapath_to_Statapath_to_Stata>/>/>/>/Stata.exeStata.exeStata.exeStata.exe /q /e do “/q /e do “/q /e do “/q /e do “commands.docommands.docommands.docommands.do””””

We’ll previously write our commands here

$stata_commands

Page 31: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Our web application will execute:

<<<<path_to_Statapath_to_Statapath_to_Statapath_to_Stata>/>/>/>/Stata.exeStata.exeStata.exeStata.exe /q /e do “/q /e do “/q /e do “/q /e do “commands.docommands.docommands.docommands.do””””

cdcdcdcd <<<<path_to_temp_folderpath_to_temp_folderpath_to_temp_folderpath_to_temp_folder>>>>

We’ll previously write our commands here

Example:

$stata_commands

cdcdcdcd <<<<path_to_temp_folderpath_to_temp_folderpath_to_temp_folderpath_to_temp_folder>>>>sysusesysusesysusesysuse autoautoautoautohistogram pricehistogram pricehistogram pricehistogram price

Page 32: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Our web application will execute:

<<<<path_to_Statapath_to_Statapath_to_Statapath_to_Stata>/>/>/>/Stata.exeStata.exeStata.exeStata.exe /q /e do “/q /e do “/q /e do “/q /e do “commands.docommands.docommands.docommands.do””””

(Stata User’s Guide, section [B.5])(Stata User’s Guide, section [B.5])

Page 33: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Mata

Web server /

Application serverStata IC / SE / MP

Operating system

Stata command(s)

Page 34: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Ex: PHP

-- synchronous execution-- synchronous execution

-- asynchronous execution

shell_exec(............);

pclose(popen(............,"r"));

Page 35: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Problem: modern versions of Stata will not

work if called directly from a web server work if called directly from a web server

(SYSTEM user).

stata.exe /e /q …

Web application

Stata IC / SE / MP

PHP: shell_exec()

Page 36: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Calling Stata

Problem: modern versions of Stata will not

work if called directly from a web server work if called directly from a web server

(SYSTEM user).

Solution: wrapper + user impersonation

Wrapper library

Stata IC / SE / MPstata.exe /e /q …

Web application

PHP: shell_exec()

Page 37: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

Web interface

(HTML / JS)

Web application

(PHP / Java /

ASP.net + C# / etc…)

Program written in

Stata / Matatext files

images

data files

log files

Web server /

Application serverStata IC / SE / MP

Operating system

Page 38: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

Our web application will execute:

cdcdcdcd <<<<path_to_temp_folderpath_to_temp_folderpath_to_temp_folderpath_to_temp_folder>>>>

<<<<path_to_Statapath_to_Statapath_to_Statapath_to_Stata>/>/>/>/Stata.exeStata.exeStata.exeStata.exe /q /e do “/q /e do “/q /e do “/q /e do “commands.docommands.docommands.docommands.do””””

We’ll previously write our commands here

Example:cdcdcdcd <<<<path_to_temp_folderpath_to_temp_folderpath_to_temp_folderpath_to_temp_folder>>>>sysusesysusesysusesysuse autoautoautoautohistogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)graph export graph01.png, replacegraph export graph01.png, replacegraph export graph01.png, replacegraph export graph01.png, replace

Page 39: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

Our web application will execute:

cdcdcdcd <<<<path_to_path_to_path_to_path_to_webwebwebweb_folder_folder_folder_folder>/>/>/>/imgimgimgimg////

<<<<path_to_Statapath_to_Statapath_to_Statapath_to_Stata>/>/>/>/Stata.exeStata.exeStata.exeStata.exe /q /e do “/q /e do “/q /e do “/q /e do “commands.docommands.docommands.docommands.do””””

We’ll previously write our commands here

Example:cdcdcdcd <<<<path_to_path_to_path_to_path_to_webwebwebweb_folder_folder_folder_folder>/>/>/>/imgimgimgimg////sysusesysusesysusesysuse autoautoautoautohistogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)histogram price, normal saving(graph01, replace)graph export graph01.png, replacegraph export graph01.png, replacegraph export graph01.png, replacegraph export graph01.png, replace

Now our web application will be able to display<<<<path_to_path_to_path_to_path_to_webwebwebweb_folder_folder_folder_folder>/img/>/img/>/img/>/img/graph01.pnggraph01.pnggraph01.pnggraph01.png

Page 40: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

call_stata.phpcall_stata.phpcall_stata.phpcall_stata.php

<?php<?php<?php<?php............

$stata_commands = $_POST[“stata_commands”];

write_stata_do_file($stata_commands);

execute_stata_do_file();

display_resultsdisplay_resultsdisplay_resultsdisplay_results(); //display graph01.(); //display graph01.(); //display graph01.(); //display graph01.pngpngpngpng

............?>?>?>?>

Page 41: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

call_stata.phpcall_stata.phpcall_stata.phpcall_stata.php

<?<?<?<?phpphpphpphp............

function display_results() {echo “<html>”;echo “ <head>Result</head>”;echo “ <body>”;

>”;echo “ <img src=img/graph01.pnggraph01.pnggraph01.pnggraph01.png>”;

echo “ </body>”;echo “</html>”;}............?>

Page 42: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Getting a response from Stata

Page 43: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

SQL injection attack:

'; DROP TABLE users;

Page 44: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

Prevent “Stata injection” attacks:

--Limited, sanitized inputs,

Ideally, no free text fields on the web interface

--Avoid or restrict shell(), xshell(), winexec()--Avoid or restrict shell(), xshell(), winexec()

in your Stata program

Page 45: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

Bad practice Better practice

Page 46: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

Prevent “Stata injection” attacks:

--Limited, sanitized inputs,

Ideally, no free text fields on the web interface

--Avoid or restrict shell(), xshell(), winexec()--Avoid or restrict shell(), xshell(), winexec()

in your Stata program

Page 47: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

Bad practice Better practice

Page 48: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Basic security

It’s even better to avoid dynamic shell() commands

if Stata is executed

Bad practice Better practice

if Stata is executed through a web interface

Page 49: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Implementation Implementation example

Page 50: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Web interface for –nomolog–

A general-purpose nomogram generator for

predictive logistic regression modelspredictive logistic regression models

Zlotnik A, Abraira V. Stata Journal. 2015. Volume 15, Number 2

URL: http://www.zlotnik.net/stata/nomograms

Page 51: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture
Page 52: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture
Page 53: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

In the web implementation, we must add a tab for loading the

dataset and executing the logistic regression command.

Page 54: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture
Page 55: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Stata web services

Web servicePrivate

resources

XML

Your program

Stata / Mata

Externalprograms

XMLStata / Mata

Page 56: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Stata web services

Web service

XML

Desktop

applications

Web

applications

XML

Mobile

applications

WSDLSOAP

native iOS appsnative Android apps

Page 57: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Service-oriented architecture

Web service

XML

Enterprise

Service Bus

XML

Other

web services

WSDLSOAP

Page 58: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Credits

Page 59: Web + StatafmStata web services Web service XML Desktop applications Web applications Mobile applications WSDL SOAP native iOSapps native Android apps Service-oriented architecture

Credits

Special thanks to all the people who made and released

these design resources for free:these design resources for free:

◎Presentation template by SlidesCarnival

◎Photographs by Unsplash & Death to the Stock Photo

(license)