cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jcastro296
Frequent Visitor

Error in Custom Visual in R because not interacting with my powerbi's filters

I have an error in the custom visula in R because this graphic does not interact or update with the filters of my power BI, can someone help me to solve this problem. In the following image is the graph that I made and the code of R. Thank you very much.

contagio.PNG

The code is in R

 

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("reshape2")
libraryRequireInstall("deSolve")
libraryRequireInstall("gridExtra")


####################################################

################### Actual code ####################
simulate <- function(theta,init.state,times) {
  ode <-function(t,x,params){
    
    S<-x[1]
    E<-x[2]
    I<-x[3]
    R<-x[4]
    A<-x[5]
    H<-x[6]
    
    
    with(as.list(params),{
      
      N=S+E+I+R+A+H
      
      dS <- -b*S*(I+A)/N 
      dE <- b*S*(I+A)/N - E/De 
      dI <- r*E/De - I/Di
      dA <- (1-r)*E/De - A/Di 
      dH <- a*I/Dq + a*A/Dq - H/Dh
      dR <- (1-a)*(I+A)/Di + H/Dh
      dx <- c(dS,dE,dI,dR,dA,dH) 
      list(dx)
    })
  }
  
  traj <- as.data.frame(lsoda(init.state, times, ode, theta))
  
  return(traj)
}



init.state <- c(S=sum(50000000),E=346,I=80,R=0,A=0,H=0)
theta <- c(b=1.75,De=5.2,Di=2.3,Dq=10,Dh=30,r=0.19,a=0.2)
times <- rep(1:100)
x <- simulate(theta,init.state,times)
x2 <- x[,-c(5,2)]


### FIGURES
# melt the data to a long format
df2 <- melt(data = x2, id.vars = "time")

# plot, using the aesthetics argument 'colour'
g<-ggplot(data = df2, aes(x = time, y = value, colour=variable)) + geom_line()+
  labs(colour = "Poblaciones")+ 
  xlab("Tiempo") +
  ylab("Pob")

####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

 

 

4 REPLIES 4
Greg_Deckler
Super User IV
Super User IV

I don't believe R visuals support cross filter?


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@Greg_Deckler @dm-p That means that the custom graphics in R are static, do not vary with the filters?

Well, this one does in fact cross filter (see attached) so perhaps it has to do with your R Script and whether or not it is using the default data frame.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @jcastro296

All custom visuals by default will take cross-filtered data from slicers or other visuals into their dataView. You can verify this by clicking the ellipsis (...) on the visual container and choosing 'Show as a table' both before and after applying a slicer or cross-filter and see the differences.

I don't do my custom visual development in R, nor am I currently set up for it in my dev environment but I've reviewed your code and a number of tutorials online and I think the issue is that your visual isn't actually binding to any data from your data model and just using static data.

The default data frame passed in from Power BI to your visual has a default name of Values, and you'll need to work with that object to make the data in your visual respond to changes in filter context within your report.

This name is defined in capabilities.json in your project, e.g.:

...
  "dataRoles": [
    {
      "displayName": "Values",
      "kind": "GroupingOrMeasure",
      "name": "Values"                   <---- This one right here
    }
  ],
...

So, provided you haven't renamed this if you reference the Values data frame in your code, that should be all there is to it.

Further reading:

Good luck!

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors