Java script loop with Rstudio loop

Hello,

I'm trying to print data frame using javascript. In order to do that I'm trying to make double loop - combaining js loop and r loop. Since giving to JS RStudio variable is pretty easy and I don't know how to give to R javascript variable I came up with solution like this:

---
output: html_document
---
```{r echo = FALSE}
knitr::opts_chunk$set(echo = FALSE)```

```{r} 
Week <- strftime(Sys.Date(), format = "%V")
x <- 1:52
df <- as.data.frame(matrix(x, nrow = 1, ncol = 52))```
<html>
<head>
   <meta charset = "UTF-8">
  <!-- Java Script -->
<script>
function Start() 
{
	function Create_Row()
	{
    ```{r}
    x <- 1:52 ```
 
    var table = '';
    var i;  
    table += '<tr>';

    for (i = 1; i < `r Week`; i++)
    {
      ```{r}
      j <- x[1]
      ```
      table += '<td> `r df[1, j]` </td>';
      ```{r}
      x <- x[-1]
      ```
    }
  table += '</tr>'; 
	
	return table;
  }

table =  Create_Row();
document.getElementById("FirstRow").insertRow().innerHTML += table;
}	
</script>
</head>

<body onload = "Start();"> 

<table border = 1>
<tbody id = "FirstRow"></tbody>
</table>

</body>
</html>

Unfortunately it looks like R chunks runs only ones in javascript loop:

for (i = 1; i < `r Week`; i++)
    {
      ```{r}
      j <- x[1]
      ```
      table += '<td> `r df[1, j]` </td>';
      ```{r}
      x <- x[-1]
      ```
    }

instead of running with every loop step (Could be seen by changing j <- x[1] to j <- x[2] and comparing results).

I will be grateful for your help in solving my problem.

You are trying to mix a lot of syntax that is not made to be used together. Using inline R and R chunks from knitr in a R for loop won't work like that. You could use a for loop in an R chunk to output some text in the Rmarkdown, this text containing any Rmarkdown syntax. See https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html

But here, you are trying to create html code with R. You can have a look at the htmltools package to generate your html code from a chunk.

If you prefere to use HTML directly, you can be insterested by the use of the .Rhtml format :grimacing:

Know also that you can use js code chunk in Rmarkdown to insert js script in your script directly

```{js}
script here
```

May I ask why trying to do that instead of using R package that already does that for you ?
You can also have a look at how they do that to get inspiration