Re: [shinyapps-users] using rvest on shiny webpage

74 views
Skip to first unread message

Tareef Kawaf

unread,
Jul 22, 2016, 3:04:00 AM7/22/16
to Yoni Sidi, shinyapps.io Users, shiny-...@googlegroups.com
This sounds like more of a shiny question in general so moving to the shiny-discuss list.  That being said, given that the shiny application can send information directly to the server, why wouldn't your shiny application react to those events instead of having an external caller take an action.

On Fri, Jul 22, 2016 at 2:45 AM, Yoni Sidi <yon...@gmail.com> wrote:
when running a shiny app can one use rvest (or something similar)  on the app itself? i am looking to use it to track user activity of a d3js during the run and update another part of the app (ie reactive)

--
You received this message because you are subscribed to the Google Groups "shinyapps.io Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shinyapps-use...@googlegroups.com.
To post to this group, send email to shinyap...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shinyapps-users/edef4f9c-2e7f-46bd-a0af-7c881503c9da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yoni

unread,
Jul 22, 2016, 10:39:03 AM7/22/16
to Tareef Kawaf, shiny-...@googlegroups.com
does the shiny server store the changes in the js that is not a natural shiny library (ie d3)? can it react to changes in a nonshiny library object?
--
Yoni

You do not learn to swim from books and lectures on the theory of buoyancy (Box 1990)

Tareef Kawaf

unread,
Jul 22, 2016, 10:45:33 AM7/22/16
to Yoni, shiny-...@googlegroups.com
The browser has the state and you can have the browser send information to shiny.  I am not on a computer right now but I believe there are some articles on the shiny developer center which talk about how to send messages from the browser to shiny.  

Yoni

unread,
Jul 22, 2016, 11:07:55 AM7/22/16
to Tareef Kawaf, shiny-...@googlegroups.com
is this the basic idea? or has this been changed with newer versions of shiny?


Tareef Kawaf

unread,
Jul 22, 2016, 12:40:47 PM7/22/16
to Yoni, shiny-...@googlegroups.com
Nothing has changed to my knowledge.  Have you tried the technique of sending data to shiny and having shiny react to the sent data and it didn't work?

Yoni

unread,
Jul 22, 2016, 12:53:00 PM7/22/16
to Tareef Kawaf, shiny-...@googlegroups.com

i know how to import data, but i need to capture it in this case from d3. as for this example of filtering the datatable through selection in the d3 tree.

#global.r
library(shiny)
library(ggplot2)
library(shinyAce)
library(dplyr)
#From ggtree https://github.com/cpsievert/shiny_apps/tree/master/ggtree
# recursive approach! http://stackoverflow.com/questions/12818864/how-to-write-to-json-with-children-from-r
makeList <- function(x) {
  idx <- is.na(x[,2])
  if (ncol(x) > 2 && sum(idx) != nrow(x)){
    listSplit <- split(x[-1], x[1], drop=T)
    lapply(names(listSplit), function(y){list(name = y, children = makeList(listSplit[[y]]))})
  } else {
    nms <- x[,1]
    lapply(seq_along(nms), function(y){list(name = nms[y], value = x[,"value"][y])})
  }
}

# thanks Jeroen http://stackoverflow.com/questions/19734412/flatten-nested-list-into-1-deep-list
renquote <- function(l) if (is.list(l)) lapply(l, renquote) else enquote(l)

df2tree <- function(m) {
  list(name = "ggplot", children = makeList(m))
}

m=mtcars%>%select(cyl,vs,am,gear,carb,mpg)%>%mutate(mpg=as.character(mpg))%>%rename(value=mpg)

#server.r
shinyServer(function(input, output, session) {
  output$d3 <- reactive({
    return(list(root = df2tree(m), layout = input$d3layout))
  })

  output$table <- renderDataTable(
    m
  )
})

#ui
shinyUI(fluidPage(
  tags$head(
    tags$script(type="text/javascript", src = "d3.v3.js"),
    tags$script(type="text/javascript", src ="d3.tip.js"),
    tags$script(type="text/javascript", src ="ggtree.js"),
    tags$link(rel = 'stylesheet', type = 'text/css', href = 'ggtree.css')
  ),

  fluidRow(
    column(width = 6,
           selectInput("d3layout", "Choose a layout:", 
                       choices = c("Radial" = "radial",
                                   "Collapsed" = "collapse",
                                   "Cartesian" = "cartesian"),selected = "collapse"),
           HTML("<div id=\"d3\" class=\"d3plot\"><svg /></div>")
    ),
    column(width = 6,dataTableOutput('table'))
  )
))

Yoni

unread,
Jul 22, 2016, 12:57:58 PM7/22/16
to Tareef Kawaf, shiny-...@googlegroups.com
sorry i didnt attach the www subdir
d3.tip.js
d3.v3.js
ggtree.css
ggtree.js

Yoni

unread,
Jul 25, 2016, 8:50:38 AM7/25/16
to Tareef Kawaf, shiny-...@googlegroups.com
Hi

I have gotten further in my problem and now I just have one obstacle. The object I need is created as a local variable in a function of the d3 code.

```
function update(source) {
// Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
  links = tree.links(nodes);
  // console.log(nodes);
//...
}
```

how can i get the nodes object back into shiny (as a list)?

Reply all
Reply to author
Forward
0 new messages