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

Show one line of code but result from another line without adding any comment #2129

Open
cderv opened this issue May 20, 2022 · 4 comments
Open
Assignees
Labels
feature Feature requests

Comments

@cderv
Copy link
Collaborator

cderv commented May 20, 2022

Tested from quarto-dev/quarto-cli#863 (comment)

mtcars
rmarkdown::paged_table(mtcars)

Can I show 'mtcars' in the code chunk after the render but not 'rmarkdown::paged_table(mtcars)' and in addition to that have only the result of the paged_table() shown?

At first I tried

```{r, echo = -2, eval= 2}
mtcars
rmarkdown::paged_table(mtcars)
```

but it will add a ## prefix, without option to opt-out

knitr/R/block.R

Lines 235 to 240 in ca398c2

if (is.numeric(ev <- options$eval)) {
# group source code into syntactically complete expressions
if (isFALSE(options$tidy)) code = sapply(xfun::split_source(code), one_string)
iss = seq_along(code)
code = comment_out(code, '##', setdiff(iss, iss[ev]), newline = FALSE)
}

## mtcars

I would have thought this could be equivalent of this

```{r, eval= FALSE}
mtcars
```

```{r, echo = FALSE}
rmarkdown::paged_table(mtcars)
```

I already encountered this in demo slide when trying to show an example I think. Would it be interesting to add chunk option to allow this ?

Maybe the longer form in two chunks is enough... 🤔

@yihui
Copy link
Owner

yihui commented May 23, 2022

I remember @jaredlander also requested this feature eight or nine years ago at a Strata conf (time flies), i.e., evaluate and echo different lines of code in the same chunk. I didn't do it because I thought it would be a little tricky to implement, and also felt like "cheating" :) Since this was brought up again, I think we can reconsider implementing it. Basically, after evaluate()ing the code, we remove the ## from the source code expressions with indices setdiff(iss, iss[ev]); or we use a special comment like #~ / #* etc to mask the code instead of ##, then simply remove these comment masks (this will be a little easier implementation-wise).

@cderv Do you want to try it, or you would rather me do it? I won't have time until a few weeks later.

@cderv
Copy link
Collaborator Author

cderv commented May 23, 2022

I'll give it a try following your advice.

@cderv cderv self-assigned this May 23, 2022
@cderv cderv added the feature Feature requests label May 23, 2022
@cderv cderv moved this from Todo to Backlog in R Markdown Team Projects May 23, 2022
@atusy
Copy link
Collaborator

atusy commented Jul 25, 2022

I would rather suggest use the render chunk option.
It has finer control and is less tricky.

Things like {r, echo=-2, eval= 2} is not a good idea because author has to correct the values when line number changes.

```{r, render = function(x, ...) knitr::knit_print(rmarkdown::paged_table(x), ...)}
mtcars
```

@yihui
Copy link
Owner

yihui commented Jul 26, 2022

For this particular case, yes, the problem can be solved by a custom render function. For general cases, we still need a more general solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

3 participants