Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 7918
License: MIT
%md # R jupyter kernel in sage worksheet

R jupyter kernel in sage worksheet

%auto ir = jupyter("ir") %default_mode ir
[removed]
R.version.string
Out [2]:
'R version 3.2.2 (2015-08-14)'
ax <- c(0,5,8,8) ay <- c(2,3,5,3) df <- data.frame(ax,ay) df
Out [3]:
R.version.string
Out [4]:
'R version 3.2.2 (2015-08-14)'
# Motor Trend sample data # https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html str(mtcars)
'data.frame':	32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
# model car mpg as linear function of wt (1000 lbs) and number of cylinders model <- lm(mpg ~ wt + cyl, data = mtcars) summary(model)
Out [6]: Call: lm(formula = mpg ~ wt + cyl, data = mtcars) Residuals: Min 1Q Median 3Q Max -4.2893 -1.5512 -0.4684 1.5743 6.1004 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 39.6863 1.7150 23.141 < 2e-16 *** wt -3.1910 0.7569 -4.216 0.000222 *** cyl -1.5078 0.4147 -3.636 0.001064 ** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.568 on 29 degrees of freedom Multiple R-squared: 0.8302, Adjusted R-squared: 0.8185 F-statistic: 70.91 on 2 and 29 DF, p-value: 6.809e-12
# scatterplots plot(mtcars[c('mpg','wt','cyl')])
# r error message model2 <- lm(mpg ~ badcol, data = mtcars)
Error in eval(expr, envir, enclos): object 'badcol' not found
Traceback:
1. lm(mpg ~ badcol, data = mtcars)
2. eval(mf, parent.frame())
3. eval(expr, envir, enclos)
4. stats::model.frame(formula = mpg ~ badcol, data = mtcars, drop.unused.levels = TRUE)
5. model.frame.default(formula = mpg ~ badcol, data = mtcars, drop.unused.levels = TRUE)
6. eval(predvars, data, env)
7. eval(expr, envir, enclos)
# autocomplete with r jupyter kernel # put cursor at the $ and press TAB # you should see something like the # image in the next cell model$
%sage salvus.file('r-completion.png')