-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathserver.R
93 lines (73 loc) · 2.7 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Import R packages needed for the app here:
library(shiny)
library(DT)
library(RColorBrewer)
# Define any Python packages needed for the app here:
PYTHON_DEPENDENCIES = c('pip', 'numpy')
# Begin app server
shinyServer(function(input, output) {
# ------------------ App virtualenv setup (Do not edit) ------------------- #
virtualenv_dir = Sys.getenv('VIRTUALENV_NAME')
python_path = Sys.getenv('PYTHON_PATH')
# Create virtual env and install dependencies
reticulate::virtualenv_create(envname = virtualenv_dir, python = python_path)
reticulate::virtualenv_install(virtualenv_dir, packages = PYTHON_DEPENDENCIES, ignore_installed=TRUE)
reticulate::use_virtualenv(virtualenv_dir, required = T)
# ------------------ App server logic (Edit anything below) --------------- #
plot_cols <- brewer.pal(11, 'Spectral')
# Import python functions to R
reticulate::source_python('python_functions.py')
# Generate the requested distribution
d <- reactive({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm)
return(dist(input$n))
})
# Generate a plot of the data
output$plot <- renderPlot({
dist <- input$dist
n <- input$n
return(hist(d(),
main = paste0('Distribution plot: ', dist, '(n = ', n, ')'),
xlab = '',
col = plot_cols))
})
# Test that the Python functions have been imported
output$message <- renderText({
return(test_string_function(input$str))
})
# Test that numpy function can be used
output$xy <- renderText({
z = test_numpy_function(input$x, input$y)
return(paste0('x + y = ', z))
})
# Display info about the system running the code
output$sysinfo <- DT::renderDataTable({
s = Sys.info()
df = data.frame(Info_Field = names(s),
Current_System_Setting = as.character(s))
return(datatable(df, rownames = F, selection = 'none',
style = 'bootstrap', filter = 'none', options = list(dom = 't')))
})
# Display system path to python
output$which_python <- renderText({
paste0('which python: ', Sys.which('python'))
})
# Display Python version
output$python_version <- renderText({
rr = reticulate::py_discover_config(use_environment = 'python35_env')
paste0('Python version: ', rr$version)
})
# Display RETICULATE_PYTHON
output$ret_env_var <- renderText({
paste0('RETICULATE_PYTHON: ', Sys.getenv('RETICULATE_PYTHON'))
})
# Display virtualenv root
output$venv_root <- renderText({
paste0('virtualenv root: ', reticulate::virtualenv_root())
})
})