Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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
Super User

I don't believe R visuals support cross filter?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@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!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Anonymous

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.