/
python-installation.Rmd
138 lines (96 loc) · 3.15 KB
/
python-installation.Rmd
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
title: "R Notebook"
output: html_notebook
---
### Installation von Python und der für TensorFlow benötigten Pakete (nur einmalig nötig) ###
```{r}
# Install the reticulate package if you haven't already
if (!requireNamespace("reticulate", quietly = TRUE)) {
install.packages("reticulate")
}
# Load the reticulate package
library(reticulate)
# Custom function to check if a Conda environment exists
condaenv_exists <- function(env_name) {
conda_envs <- conda_list()
return(env_name %in% conda_envs$name)
}
# Check if conda is installed, if not, install miniconda
conda_envs <- conda_list()
if (length(conda_envs) == 0) {
install_miniconda()
}
# Create a specific Python environment if it doesn't exist
if (!condaenv_exists("r-reticulate")) {
conda_create("r-reticulate", python_version = "3.8")
}
# Get the list of installed packages in the created environment
conda_envs <- conda_list()
r_reticulate_env <- conda_envs[conda_envs$name == "r-reticulate", ]
installed_packages <- r_reticulate_env$packages
# Install required packages in the created environment
required_packages <- c("pandas", "numpy", "tensorflow", "h5py")
for (pkg in required_packages) {
if (!(pkg %in% installed_packages)) {
conda_install("r-reticulate", pkg)
}
}
# Use the created Python environment
use_condaenv("r-reticulate", required = TRUE)
```
### Potentielle Alterbative für Windows ###
#### Nur ausführen, wenn die Variante oben fehl schlägt. ####
```{r}
# Install the reticulate package if you haven't already
if (!requireNamespace("reticulate", quietly = TRUE)) {
install.packages("reticulate")
}
# Load the reticulate package
library(reticulate)
# Custom function to check if a Conda environment exists
condaenv_exists <- function(env_name, condaenvs_dir = NULL) {
conda_envs <- conda_list(condaenvs_dir)
return(env_name %in% conda_envs$name)
}
# Check if conda is installed, if not, install miniconda
conda_envs <- conda_list()
if (length(conda_envs) == 0) {
install_miniconda()
}
# Set custom path for the conda environment
custom_path <- "C:/conda_envs" # Change this path as needed
# Create a specific Python environment if it doesn't exist
if (!condaenv_exists("r-reticulate", condaenvs_dir = custom_path)) {
conda_create("r-reticulate", python_version = "3.8", envdir = custom_path)
}
# Get the list of installed packages in the created environment
conda_envs <- conda_list(custom_path)
r_reticulate_env <- conda_envs[conda_envs$name == "r-reticulate", ]
installed_packages <- r_reticulate_env$packages
# Install required packages in the created environment
required_packages <- c("pandas", "numpy", "tensorflow", "h5py")
for (pkg in required_packages) {
if (!(pkg %in% installed_packages)) {
conda_install("r-reticulate", pkg, envdir = custom_path)
}
}
# Use the created Python environment
use_condaenv("r-reticulate", condaenvs_dir = custom_path, required = TRUE)
```
```{python}
import sys
import tensorflow
print("Python Version: " + sys.version+"\nTensorFlow Version: "+tensorflow.__version__)
```
```{r}
# Import Libraries
library(reticulate)
# Importing Data
data <- mtcars
```
```{python}
mpg = r.data['mpg']
```
```{r}
table(py$mpg)
```