-
Notifications
You must be signed in to change notification settings - Fork 20
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
Problem with chromote package #154
Comments
I think the problem is the use of You should do something like this: library(shiny)
library(chromote)
ui <- fluidPage(
textOutput("result")
)
server <- function(session, input, output) {
driver <- ChromoteSession$new()
p <- driver$Page$loadEventFired(wait_ = FALSE)
driver$Page$navigate("https://google.com", wait_ = FALSE)
output$result <- renderText({
p$then(function(value) {
# scrape Google title
driver$Runtime$evaluate('document.querySelector("title").innerText')$result$value
})
})
}
shinyApp(ui, server) To properly navigate to a page and wait for it to load without blocking the R process, see this section of the README: The example above also makes uses of Promises in Shiny. See here for more information: |
Thanks for your feedback. Does it work on shinyapps.io?
Best Regards,
SaleForecast
|
@saleforecast1 Your app currently does work on shinyapps.io. Maybe your example doesn't completely reproduce your issue or I don't understand what you mean by "work" or "two users can not use it concurrently". But if I open https://sale4cast.shinyapps.io/findGoogleTitle/ in two different tabs or browsers, they both eventually (after about 7 seconds) show me the word "Google". |
This question was also cross-posted to https://forum.posit.co/t/problem-with-chromote-package/186346 |
If I open this link in two different tabs or browsers simultaneously, I
expect it to display the output concurrently. However, in my app, I notice
that the second instance starts running only after the first one has
completed.
|
Have you tried the code that I provided? The problem is that your |
Oh, in that case, what Winston said is exactly right:
If you put Here's a simple diagram outlining the interaction. sequenceDiagram
User 1->>+Shiny: Opens app
User 2-->Shiny: Opens app
Shiny-->>-User 1: responds after 7s
activate Shiny
Note over Shiny: starts user 2 request
Shiny-->>-User 2: responds after 7+s
To fix it please follow Winston's guidance:
|
@wch can you say please why this code doesn't return the title? It returns an error "TypeError: Cannot read properties of null (reading 'innerText')\n at :1:32" |
It sounds like the I think the problem is that clicking on the search button causes another page load, and when you grab the I believe that you'll have to wait for another library(shiny)
library(chromote)
ui <- fluidPage(
textOutput("result")
)
server <- function(session, input, output) {
driver <- ChromoteSession$new()
p <- driver$Page$loadEventFired(wait_ = FALSE)
driver$Page$navigate("https://google.com", wait_ = FALSE)
p$then(function(value){
googleSearchText <- "4 star hotel in barcelona"
p2 <- driver$Page$loadEventFired(wait_ = FALSE)
driver$Runtime$evaluate(paste0('document.querySelector("textarea").value = "', googleSearchText,'"'))
driver$Runtime$evaluate('document.querySelector("input[aria-label=\'Google Search\']").click()')
p2
})$then(function(value){
v <- driver$Runtime$evaluate('document.querySelector("title").innerText')
print(v)
})
}
shinyApp(ui, server) Note that One other thing I want to mention: the code you started with uses a mix of sync and async programming, and calls to synchronous Chromote functions inside of asynchronous functions. It works in this case but might do unexpected things for more complicated code. It's probably best to stick to just async code for complex tasks, but that will require a good understanding of how these promises work. |
Thanks for you response @wch. I really appreciate your answer and it works great. However, I still face an error when I run this app from multiple devices by shinyapps.io. Error: Code:
can you please help me for sort out the problem? |
I don't know for sure, but my guess would be that there's not enough time between the two |
Thanks for your response @wch. can you please say, how to ensure enough time between two |
Dear community,
I have created a shiny app to scrape the "google title" from the google page ("https://google.com"). To scrape this, I have used R chromote package. The app works fine while running on the desktop. However, once It is hosted on shinyapps.io server, two users can not use it concurrently. The code for the app is in below,
Output:
Question: How can two users access the app concurrently via shinyapps.io?.
Best Regards,
SaleForecast
The text was updated successfully, but these errors were encountered: