i am very new with iframes. I am trying to implement a shiny code so that i can count the number pages I visit when implement an iframe from an online newspaper. So, i want to count how many time the iframe changes when clicking.
So far I have:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(width = 12,
HTML('
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
iframe{
width: 100%;
height: 600px;
border: 2px solid #ccc;
}
</style>
</head>
<body>
<script> var i =-1; </script>
<iframe src="https://elpais.com/" onLoad="console.log(++i)"></iframe>
</body>'),
textOutput("view")
)
)
)
)
server <- function(input, output) {
output$view <- renderText( paste0( "User ID is: " ) )
}
runApp(list(ui = ui, server = server), launch.browser = TRUE)
Every time I click a link, I can see the value of variable "i" in the Google chrome console. But I actually want to have acces to this value in my R code. I have seen that you can acces js variables with shiny using shiny.oninputchange. Nevertheless I am very confused on how this should be implemented.
Thank you in advance if you can aswer my questions!
I want to count the number of times I click on the iframe and the iframe page refreshes to a new one (for example, a diferent news item). I hope this answer helps!
Sorry If the question was a little unclear. I finally solved it like this. Every time the iframe embedded in my shiny app refreshes I have a count that adds one to its value. This way I can count the number of pages I visit while navigating in my I frame. I do that by adding one to the count every time the page in the Iframe refreshes.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(width = 12,
HTML('<input type="text" id="count" name="count" style="display: none;"> '),
HTML('
<style>
iframe{
width: 100%;
height: 600px;
border: 2px solid #ccc;
}
</style>
</head>
<body>
<script>
var countStr = "count";
var changeStr = "change";
var countInput = document.getElementById(countStr);
countInput.value = -1;
</script>
<iframe src="https://elpais.com/" onLoad="var event = new Event(changeStr);countInput.value++;countInput.dispatchEvent(event)"></iframe>
</body>'),
#includeScript("example/get_user_id.js"),
textOutput("view"),
actionButton("submit", "Submit", class = "btn-primary"),
)
)
)
)
server <- function(input, output) {
count <- reactive({ input$count })
output$view <- renderText( paste0( "User ID is: ",count()) )
}
runApp(list(ui = ui, server = server), launch.browser = TRUE)