Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Enable non-R code exercises #213

Closed
lukaskawerau opened this issue Feb 8, 2019 · 8 comments
Closed

Feature Request: Enable non-R code exercises #213

lukaskawerau opened this issue Feb 8, 2019 · 8 comments
Labels
effort: high 4+ days of work priority: low Will be fixed eventually type: enhancement Adds a new, backwards-compatible feature

Comments

@lukaskawerau
Copy link

lukaskawerau commented Feb 8, 2019

In normal Rmd documents you can specify SQL-only chunks, based on a database connection via DBI:

```{r}
library(DBI)
mydata = dbConnect(RPostgres::Postgres(), 
               dbname = "some_database",
               host = 'localhost',
               port = 5432,
               user = 'postgres',
               password = 'mysecretpassword')
```
```{sql, connection=mydata}
CREATE TABLE table_name (
  PersonID int,
  LastName varchar(255),
  FirstName varchar(255)
);
```

However, this does not work with learnr. I would like to be able to do the same in an exercise so that students can directly submit SQL statements:

```{sql, connection=mydata, exercise = TRUE}

```

Where students then would have to enter SQL code into the exercise.

@schloerke
Copy link
Collaborator

I'm going to leave this open as it's a good idea.

It's going to be a while as the correct infrastructure is not there.

@schloerke schloerke added effort: high 4+ days of work priority: low Will be fixed eventually type: enhancement Adds a new, backwards-compatible feature labels Apr 10, 2019
@coatless
Copy link
Contributor

@schloerke let's generalize to all of R Markdown's supported engines?

Outside of SQL, it would be useful to have Python.

@schloerke schloerke changed the title Feature Request: Enable SQL code chunks Feature Request: Enable non-R code chunks Aug 8, 2019
@schloerke schloerke changed the title Feature Request: Enable non-R code chunks Feature Request: Enable non-R code exercises Aug 8, 2019
@cassws
Copy link
Contributor

cassws commented Oct 7, 2019

Hi, @coatless @schloerke should I assume that Python isn't working with learnr at this time? Is there any workaround with e.g. reticulate? If not, I would be interested in getting involved with adding this functionality as it would be extremely helpful for a work project (slash helpful in general!)

@schloerke
Copy link
Collaborator

schloerke commented Oct 7, 2019

@zoews

should I assume that Python isn't working with learnr at this time?

Correct

Is there any workaround with e.g. reticulate?

I would like to use knitr's engine to process the information. R Markdown already has support for python.

This would allow for all existing languages that can produce output to work as expected. (Persistent connections, like shiny applications, will not be possible. Only code that evaluates and returns is possible in the current execution model.)

Recipe

evaluate_exercise is where the execution of the user exercise happens.

I'm guessing that (as of writing this comment)

  • the knitr::spin call will have to have a different function call when working with different languages.
  • the code pertaining to last_value and learnr_output_handler will need to not be executed
  • the checker functions (pre and post) might need an extra argument of language.

Debugging will be hard with a temp file being created each time. I recommend changing this line to a local working path folder.


@zoews For implementation ideas/discussion/troubleshooting, please open a PR. 😄

@braunsb
Copy link
Contributor

braunsb commented Dec 4, 2019

Just leaving my vote: If we had this feature, we would be using learnr all over the organization!

@TonyFly3000
Copy link

TonyFly3000 commented Mar 13, 2020

would love to have following Feature

{sql, connection=mydata, exercise = TRUE}

@JosiahParry
Copy link

Just want to throw in my vote for this as well :)

@schloerke
Copy link
Collaborator

@nischalshrestha has re-written how exercises are calculated in #390 (just merged to master). Thank you @zoews for getting the conversation started and showing what is possible.

Currently, no UI changes have been merged into master from #310. Working on this in #397

If you could test this locally, that would be great! If you run into any issues, please comment on #397, as I will be making followup adjustments there. Thank you!

Example polyglot tutorial
---
title: "Multi-language exercises"
author: "Zoë Wilkinson Saldaña"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include = FALSE}
library(learnr)

# altered from https://datacarpentry.org/R-ecology-lesson/05-r-and-databases.html
if (!file.exists("portal_mammals.sqlite")) {
  download.file(url = "https://ndownloader.figshare.com/files/2292171",
                destfile = "portal_mammals.sqlite", mode = "wb")
}
mammals <- DBI::dbConnect(RSQLite::SQLite(), "portal_mammals.sqlite")
```

### Python or R?

The following two code exercises run language agnostic code.  It will finally evaluate to either 5 or 10.

```{r r-not-python, exercise=TRUE, exercise.lines = 5}
# if running in R, this should return 10
# if running in Python, this should return FALSE and 5
x = 5
x <-10
print(x)
```

```{python python-not-r, exercise=TRUE, exercise.lines = 5}
# if running in R, this should return 10
# if running in Python, this should return FALSE and 5
x = 5
x <-10
print(x)
```

### Bash

Execute exercises in bash

```{bash bash-not-r, exercise = TRUE}
echo "hello world"
date
```

### SQL

Use a SQL connection defined in a setup chunk.

```{sql sql-not-r, exercise = TRUE, connection="mammals"}
SELECT *
FROM `surveys`
LIMIT 10
```

Screen Shot 2020-07-14 at 11 24 13 AM


Closing as fixed in #390

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: high 4+ days of work priority: low Will be fixed eventually type: enhancement Adds a new, backwards-compatible feature
Projects
None yet
Development

No branches or pull requests

7 participants