I intend to make a Javascript variable from an iframe available to be readable in Shiny. Following these answers:
-Display HTML file in Shiny App
-How do I print an input from the R shiny UI to the console?
I was able to make the following code:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = 'demo'),
dashboardSidebar(
sidebarMenu(
menuItem('Banco de Dados',
tabName = 'banco_de_dados',
icon = icon('th'))
)
),
dashboardBody(
tabItems(
tabItem(tabName = 'banco_de_dados',
h2('Banco de Dados'),
textOutput('view'),
htmlOutput('map')
)
)
)
)
server <- function(input, output, session) {
output$map <- shiny::renderUI({
tags$iframe(seamless = 'seamless',
src = 'foo.html',
width = 800,
height = 800)
})
clickCounter <- reactive({ input$clickCounter })
output$view <- renderText(paste0('Click counter is: ', clickCounter()))
}
shinyApp(ui, server)
And the foo.html
in my www
folder:
<html>
<script type="text/javascript">
let clickCounter = 0;
</script>
<body>
TEST
<button id="myButton" onclick="clickCounter += 1;console.log(clickCounter);">Click here</button>
</body>
</html>
The big problem right now is that I cannot capture input$sclickCounter
value. And as you can see, I can change the variable value in the iframe and display it in my browser's console:
How can I fix this and make my code behave as I expect?