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
shizar115
Helper I
Helper I

Using gganimate from CRAN via R-script to visualize an animated component

Dear all,

I have been trying to create animated dashboards via Power BI and my preference is R-script since gives me good control.
It seems implementing animated components is not as straight-forward as it would be in shiny server. I created my complete code in R-Studio and am sure the code is correct since I am getting the correct result. I am also sure all required libraries are included in my script:

 

 

 

 

 

 

 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(sumed2, year, weeknumber)
# dataset <- unique(dataset)

# Paste or type your script code here:
library(lubridate)
library(magrittr)
library(dplyr)
library(tidyverse)
library(odbc)
library(DBI)
library(ggplot2)
library(gganimate)
library(transformr)
library(tweenr)
dataset$year<-NULL
dataset$weeknumber<-NULL

ggplot(dataset,aes(x=timed, y=sumed2, group=yeared, colour=yeared)) +
  geom_line() +transition_time(timed) +
  
  geom_segment(aes(xend=max(timed), yend = sumed2), linetype=2, colour='blue') +
  geom_point(size = 3) + 
  geom_text(aes(x = max(timed)+.1, label = sprintf("%5.0f", sumed2)), hjust=0) +
  transition_reveal(timed) + 
  view_follow(fixed_y = TRUE)+
  coord_cartesian(clip = 'off') + 
  labs(title = 'Active in Jan', y = 'Individual Active') +
  enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        plot.margin = margin(1.5, 40, 5.5, 5.5))

 

 

 

 

 

 

 

 

 After running this in Power BI, Windows OS tries to open the output for some reason like this:

shizar115_0-1605897046600.png

and Power BI shows this message:

shizar115_1-1605897081404.pngshizar115_2-1605897094386.png

 

Here is the copied details:

 

 

 

 

 

 

 

Feedback Type: Frown (Error) Timestamp: 2020-11-20T18:21:04.9231924Z Local Time: 2020-11-20T10:21:04.9231924-08:00 Session ID: 9fb6827a-32d2-48f6-aa96-b418c3687ca3 Release: November 2020 Product Version: 2.87.762.0 (20.11) (x64) Error Message: No image was created. The R code didn't result in creation of any visuals. Make sure your R script results in a plot to the R default device. Stack Trace: Microsoft.PowerBI.ExploreServiceCommon.ScriptHandlerException: No image was created. The R code didn't result in creation of any visuals. Make sure your R script results in a plot to the R default device. at Microsoft.PowerBI.Client.Windows.R.RScriptHandler.GenerateVisual(ScriptHandlerOptions options) at Microsoft.PowerBI.ExploreServiceCommon.ScriptVisualCommandFlow.RunInternal(Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor) at Microsoft.PowerBI.ExploreServiceCommon.ScriptVisualCommandFlow.Run(Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor) at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.TransformDataShapeResult(QueryCommand transformCommand, SemanticQueryDataShapeCommand command, Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor) at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.ExecuteDataQuery(IQueryResultDataWriter queryResultDataWriter, EngineDataModel engineDataModel, DataQuery query, Int32 queryId, ServiceErrorStatusCode& serviceErrorStatusCode, CancellationToken cancelToken) at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.ProcessAndWriteSemanticQueryCommands(IQueryResultsWriter queryResultsWriter, IList`1 queries, HashSet`1 pendingQueriesToCancel, EngineDataModel engineDataModel) Invocation Stack Trace: at Microsoft.Mashup.Host.Document.ExceptionExtensions.GetCurrentInvocationStackTrace() at Microsoft.Mashup.Client.UI.Shared.StackTraceInfo..ctor(String exceptionStackTrace, String invocationStackTrace, String exceptionMessage) at Microsoft.PowerBI.Client.Windows.ErrorHostService.GetErrorDetails(ShowErrorDialogArgs args) at Microsoft.PowerBI.Client.Windows.ErrorHostService.<>c__DisplayClass3_1.b__1() at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks() at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at System.Windows.Forms.WindowsFormsSynchronizationContext.Send(SendOrPostCallback d, Object state) at Microsoft.PowerBI.Client.Windows.Services.UIBlockingService.AllowModalDialogs(Action action) at Microsoft.PowerBI.Client.Windows.ErrorHostService.<>c__DisplayClass3_0.<b__0>d.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at Microsoft.PowerBI.Client.Windows.ErrorHostService.<>c__DisplayClass3_0.b__0() at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.<>c__DisplayClass1_0`1.b__0() at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.<>c__DisplayClass0_1.b__0(Object null) at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks() at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) at Microsoft.Mashup.Client.UI.Shared.WindowManager.ShowModal[T](T dialog, Func`1 showModalFunction) at Microsoft.PowerBI.Client.Program.<>c__DisplayClass4_0.b__1() at Microsoft.PowerBI.Client.Windows.IExceptionHandlerExtensions.<>c__DisplayClass3_0.b__0() at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action) at Microsoft.PowerBI.Client.Program.Main(String[] args)

 

 

 

 

 

 

 

 

 

I would appreciate it if anyone can help me with this.
Sample Of my data looks like this:

 

shizar115_0-1605897475824.png

 


Regards,
Shizar

1 ACCEPTED SOLUTION

I'm not sure what is going on.  The visual I see is static, and I suspect it launched your browser when it was returning html.  To see the animated visual on your Power BI report page, I suspect you are going to need to create a custom visual following the steps described here.

https://towardsdatascience.com/custom-html-visuals-in-power-bi-using-r-2b0494894ff

 

Here is the static chart I see with your example data.

mahoneypat_0-1605986978197.png

Attached is the pbix, in case it is useful to you.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

9 REPLIES 9
shizar115
Helper I
Helper I

Hi @mahoneypat ,

I finally got time to go through the instructions you directed me to.
First of all thank you for providing that, I am not sure how you knew that.
Secondly that works only for plotly charts, so not gganimate. when I changed the ggplotly at the end of the code to animate(), the error in Power BI said Power BI does not support gganimate animate elements. There we have it then, Power BI does not support animations made with R. I was not able to do the same with Python since it requires a Pro license.

Issue is not resolved but I guess topic can be closed 🙂

Kind Regards,
Shizar

shizar115
Helper I
Helper I

Hi @mahoneypat ,

Yes absolutely weird. It does work for a normal chart and here I included an image for the same dataset and graph, just no transition:

shizar115_0-1605990774625.png

 

Regards,
Shizar

 

shizar115
Helper I
Helper I

Hi @mahoneypat ,

Can you share the dataset you are using for generating the visual?
Edit: I saw the data set in your .pbix file:

shizar115_1-1605987445416.png

 


Also I ran the .pbix file and this is what I see:

shizar115_0-1605987318884.png

Does it have anything to do with not having a PRO license?


Regards,
Shizar

No pro license needed, as it is all with Power BI Desktop.  Does the R visual work for you with simpler R?

plot(1,1) 

 

If not, it is something with your R configuration.  If so, I'm not sure what's going on.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


shizar115
Helper I
Helper I

Hi Pat,

Thank you for getting back to me.
In R-Studio this is what I get with the same dataset and code:

 

https://gph.is/g/4DeMxdO 

I checked in Power BI and there is no aggregation active (I checked for all three parameters):

summarize.PNG

Reason for those two NULL statements is due to the fact that my dataset actually has those two extra columns and I kept them in the dataset to compare the processed values with raw ones (I used as.numeric() since ggaimate requires numeric values but the raw columns were actually string). So I wanted to clean those up. After removing those NULL statements still issue is there.

 

I wish to note that the dataset itself was generated using an aggregated function with R script. So I added ungroup() at the end and that did nt fix it either.
I paid more attention and saw SIGMA symbols next to field names, and after 20 minutes found that summarization is active by default. Tutorials were not helpful in locating the correct tab for disabling that, finally I found it here:

disable.PNG

After that still the issue persists.

I also tried an explicit call for animation like this:

animate(p, height = 800, width =800,fps=10)


Still no luck..

I'm not sure what is going on.  The visual I see is static, and I suspect it launched your browser when it was returning html.  To see the animated visual on your Power BI report page, I suspect you are going to need to create a custom visual following the steps described here.

https://towardsdatascience.com/custom-html-visuals-in-power-bi-using-r-2b0494894ff

 

Here is the static chart I see with your example data.

mahoneypat_0-1605986978197.png

Attached is the pbix, in case it is useful to you.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Did someone try this solution?
How would a custom visual work in Power BI for animated charts?
I ask this because I don't see how it could work...

shizar115
Helper I
Helper I

Hi @mahoneypat ,

 

Thank you for getting back to me.
In R-Studio this is what I get with the same dataset and code:

https://gph.is/g/4DeMxdO 

 

I checked in Power BI and there is no aggregation active (I checked for all three parameters):

 

summarize.PNG

Reason for those two NULL statements is due to the fact that my dataset actually has those two extra columns and I kept them in the dataset to compare the processed values with raw ones (I used as.numeric() since ggaimate requires numeric values but the raw columns were actually string). So I wanted to clean those up. After removing those NULL statements still issue is there.

  

I wish to note that the dataset itself was generated using an aggregated function with R script. So I added ungroup() at the end and that did nt fix it either.
I paid more attention and saw SIGMA symbols next to field names, and after 20 minutes found that summarization is active by default. Tutorials were not helpful in locating the correct tab for disabling that, finally I found it here:

 

disable.PNG

 

After that still the issue persists.

I also tried an explicit call for animation like this:

animate(p, height = 800, width =800,fps=10)


Still no luck..

 

mahoneypat
Employee
Employee

Your code resulted in a chart for me (once I loaded all the listed packages).  Make sure for each column in the visual that there is no default aggregation applied.  I didn't see any animation, but it looks like you haven't used any gganimate functions yet.  Is that right?  I'm curious if it works in Power BI.  Also, why do you have the <- NULL steps instead of just not including those columns in the R visual?

 

mahoneypat_0-1605974318097.png

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.

Top Solution Authors