spencer spencer - 6 days ago 5
R Question

R,shiny dynamic data manipulation and selecting rows according to a condition

I have a csv file in mongodb server. I read it in R shiny using the following snippet of code,

library(shiny)
shinyServer(

function(input,output,session)
{
dataf <-eventReactive(input$load, {
con <- mongo(collection = "recent", url = "mongodb:some url")
mydata <- con$find()
})
output$variables = renderUI({
selectInput('variables', 'Choose the device', outVar())
})
outVar <- reactive({
devices <- dataf()
ds <- devices$`Source`
return(unique(ds))
})
}


And dataf contains something like,

serial no source target r_no
1 A B 4
2 B C 9
3 A E 3
4 B A 6
5 A F 7
6 C G 2


Now,I want to select only the rows that correspond to a particular user input that is selected by user in a drop down list button on ui.R.

Suppose user selects 'A' then I have the entire rows that have only A as the Source in it. Like,

serial no source target r_no
1 A B 4
3 A E 3
5 A F 7


My ui.R is as follows:

library(shiny)
library(mongolite)

shinyUI(fluidPage(

titlePanel(title=h3()),
sidebarLayout(
sidebarPanel(
uiOutput("variables"),
br(), width = 3),
mainPanel(uiOutput("tb"))
)
)
)


How do I accomplish this in R shiny? Please let me know. Thanks.

Answer

You were pretty close in outVar,

In server.R add inside shinyServer function:

outVar <- reactive({ 

  mongo_results <- dataf()

  output <- mongo_results[which(mongo_results[,'source'] == input$variables),]

  return(output) 

})
Comments