Εκμάθηση: Δημιουργία μιας απεικόνισης R Power BITutorial: Create an R-powered Power BI visual
Αυτή η εκμάθηση περιγράφει πώς μπορείτε να δημιουργήσετε μια απεικόνιση βάσει R για το Power BI.This tutorial describes how to create an R-powered visual for Power BI.
Σε αυτή την εκμάθηση, θα μάθετε πώς μπορείτε να κάνετε τα εξής:In this tutorial, you learn how to:
- Δημιουργία μιας απεικόνισης RCreate an R-powered visual
- Επεξεργασία της δέσμης ενεργειών R στο Power BI DesktopEdit the R script in Power BI Desktop
- Προσθήκη βιβλιοθηκών στην απεικόνισηAdd libraries to the visual
- Προσθήκη στατικής ιδιότηταςAdd a static property
ΠροαπαιτούμεναPrerequisites
- Λογαριασμός Power BI Pro.A Power BI Pro account. Εγγραφείτε για μια δωρεάν δοκιμαστική έκδοση πριν ξεκινήσετε.Sign up for a free trial before you begin.
- Ο μηχανισμός R.The R engine. Μπορείτε να κάνετε λήψη της δωρεάν, από άλλες τοποθεσίες, συμπεριλαμβανομένης της σελίδας λήψης του Revolution Open και του Αποθετηρίου CRAN.You can download it free from many locations, including the Revolution Open download page and the CRAN Repository. Για περισσότερες πληροφορίες, ανατρέξτε στο θέμα Δημιουργία απεικονίσεων του Power BI με χρήση της R .For more information, see Create Power BI visuals using R.
- Power BI Desktop.Power BI Desktop.
- Έκδοση 4 ή μια νεότερη έκδοση του Windows PowerShell για χρήστες των Windows Ή το Τερματικό για χρήστες του OSX.Windows PowerShell version 4 or later for Windows users OR the Terminal for OSX users.
Γρήγορα αποτελέσματαGetting started
Προετοιμάστε το δείγμα δεδομένων για την απεικόνιση.Prepare sample data for the visual. Μπορείτε να αποθηκεύσετε αυτές τις τιμές σε μια βάση δεδομένων του Excel ή σε ένα αρχείο .csv και να την εισαγάγετε στο Power BI Desktop.You can save these values to an Excel database or .csv file and import it into Power BI Desktop.
MonthNoMonthNo Σύνολο μονάδωνTotal Units 11 23032303 22 23192319 33 17321732 44 16151615 55 14271427 66 22532253 77 11471147 88 15151515 99 25162516 1010 31313131 1111 31703170 1212 27622762 Για να δημιουργήσετε μια απεικόνιση, να ανοίξετε το PowerShell ή το τερματικό και να εκτελέσετε την ακόλουθη εντολή:To create a visual, open PowerShell or Terminal, and run the following command:
pbiviz new rVisualSample -t rvisual
Αυτή η εντολή δημιουργεί μια νέα δομή φακέλου που βασίζεται στο πρότυπο
rvisual
.This command creates a new folder structure based on thervisual
template. Αυτό το πρότυπο περιλαμβάνει μια βασική, έτοιμη προς εκτέλεση απεικόνιση R, η οποία εκτελεί την ακόλουθη δέσμη ενεργειών R:This template includes a basic, ready-to-run R-powered visual that runs the following R script:plot(Values)
Το πλαίσιο δεδομένων
Values
θα περιέχει στήλες στον ρόλο δεδομένωνValues
.TheValues
data frame will contain columns inValues
data role.Αντιστοιχίστε δεδομένα στην απεικόνιση προγραμματιστή, προσθέτοντας τις τιμές ΑρΜήνα και Συνολικές μονάδες στις Τιμές για την απεικόνιση.Assign data to the developer visual by adding MonthNo and Total units to Values for the visual.
Επεξεργασία της δέσμης ενεργειών REditing the R Script
Όταν χρησιμοποιείτε το pbiviz
για να δημιουργήσετε την απεικόνιση R σύμφωνα με το πρότυπο rvisual
, δημιουργεί ένα αρχείο με την ονομασία script.r στον ριζικό φάκελο της απεικόνισης.When you use pbiviz
to create the R-powered visual based on the rvisual
template, it creates a file called script.r in the root folder of the visual. Αυτό το αρχείο περιέχει τη δέσμη ενεργειών R που εκτελείται για τη δημιουργία της εικόνας για έναν χρήστη.This file holds the R script that runs to generate the image for a user. Μπορείτε να δημιουργήσετε μια δέσμη ενεργειών R στο Power BI Desktop.You can create your R script in Power BI Desktop.
Στο Power BI Desktop, επιλέξτε Απεικόνιση δέσμης ενεργειών R:In Power BI Desktop, select R script visual:
Επικολλήστε αυτόν τον κώδικα R στο Πρόγραμμα επεξεργασίας δέσμης ενεργειών R:Paste this R code into the R script editor:
x <- dataset[,1] # get the first column from dataset y <- dataset[,2] # get the second column from dataset columnNames = colnames(dataset) # get column names plot(x, y, type="n", xlab=columnNames[1], ylab=columnNames[2]) # draw empty plot with axis and labels only lines(x, y, col="green") # draw line plot
Επιλέξτε το εικονίδιο Εκτέλεση δέσμης ενεργειών, για να δείτε το αποτέλεσμα.Select the Run script icon to see the result.
Όταν η δέσμη ενεργειών R είναι έτοιμη, αντιγράψτε την στο αρχείο
script.r
στο έργο απεικόνισης που δημιουργήσατε σε ένα από τα προηγούμενα βήματα.When your R script is ready, copy it to thescript.r
file in your visual project created at one of the previous steps.Αλλάξτε το
name
τουdataRoles
στο capabilities.json σεdataRoles
.Change thename
ofdataRoles
in capabilities.json todataRoles
. Το Power BI διαβιβάζει τα δεδομένα ως το αντικείμενο πλαισίου δεδομένωνdataset
για την απεικόνιση της δέσμης ενεργειών R, αλλά η απεικόνιση R λαμβάνει το όνομα του πλαισίου δεδομένων σύμφωνα με τα ονόματαdataRoles
.Power BI passes data as thedataset
data frame object for the R script visual, but the R visual gets the data frame name according todataRoles
names.{ "dataRoles": [ { "displayName": "Values", "kind": "GroupingOrMeasure", "name": "dataRoles" } ], "dataViewMappings": [ { "scriptResult": { "dataInput": { "table": { "rows": { "select": [ { "for": { "in": "dataset" } } ], "dataReductionAlgorithm": { "top": {} } } } }, ... } } ], }
Προσθέστε τον παρακάτω κώδικα για να υποστηρίξετε την αλλαγή μεγέθους της εικόνας στο αρχείο src/visual.ts.Add the following code to support resizing the image in the src/visual.ts file.
public onResizing(finalViewport: IViewport): void { this.imageDiv.style.height = finalViewport.height + "px"; this.imageDiv.style.width = finalViewport.width + "px"; this.imageElement.style.height = finalViewport.height + "px"; this.imageElement.style.width = finalViewport.width + "px"; }
Προσθήκη βιβλιοθηκών στο πακέτο απεικονίσεωνAdd libraries to visual package
Αυτή η διαδικασία επιτρέπει στην απεικόνισή σας να χρησιμοποιήσει το πακέτο corrplot
.This procedure allows your visual to use the corrplot
package.
Προσθέστε την εξάρτηση βιβλιοθήκης για την απεικόνισή σας στο
dependencies.json
.Add the library dependency for your visual todependencies.json
. Ακολουθεί ένα παράδειγμα του περιεχομένου του αρχείου:Here is an example of the file content:{ "cranPackages": [ { "name": "corrplot", "displayName": "corrplot", "url": "https://cran.r-project.org/web/packages/corrplot/" } ] }
Το πακέτο
corrplot
είναι μια γραφική απεικόνιση μιας μήτρας συσχέτισης.Thecorrplot
package is a graphical display of a correlation matrix. Για περισσότερες πληροφορίες σχετικά με τοcorrplot
, ανατρέξτε στο θέμα Εισαγωγή στο πακέτο corrplot.For more information aboutcorrplot
, see An Introduction to corrplot Package.Αφού κάνετε αυτές τις αλλαγές, ξεκινήστε να χρησιμοποιείτε το πακέτο στο αρχείο
script.r
.After you make these changes, start using the package in yourscript.r
file.library(corrplot) corr <- cor(dataset) corrplot(corr, method="circle", order = "hclust")
Το αποτέλεσμα της χρήσης του πακέτου corrplot
μοιάζει με το παρακάτω παράδειγμα:The result of using corrplot
package looks like this example:
Προσθήκη στατικής ιδιότητας στο παράθυρο ιδιοτήτωνAdding a static property to the property pane
Δώστε τη δυνατότητα στους χρήστες να αλλάζουν τις ρυθμίσεις περιβάλλοντος εργασίας χρήστη.Enable users to change UI settings. Για να το κάνετε αυτό, προσθέστε ιδιότητες στο παράθυρο ιδιοτήτων που αλλάζουν τη συμπεριφορά που βασίζεται στην είσοδο χρήστη της δέσμης ενεργειών R.To do this, add properties to the property pane that change the user-input based behavior of the R script.
Μπορείτε να ρυθμίσετε τις παραμέτρους του corrplot
χρησιμοποιώντας το όρισμα method
για τη συνάρτηση corrplot
.You can configure corrplot
by using the method
argument for the corrplot
function. Η προεπιλεγμένη δέσμη ενεργειών χρησιμοποιεί έναν κύκλο.The default script uses a circle. Τροποποιήστε την απεικόνιση για να επιτρέψετε στον χρήστη να επιλέξει μεταξύ πολλών επιλογών.Modify your visual to let the user choose between several options.
Καθορίστε το αντικείμενο και την ιδιότητα στο αρχείο capabilities.json.Define the object and property in the capabilities.json file. Στη συνέχεια, χρησιμοποιήστε αυτό το όνομα αντικειμένου στη μέθοδο απαρίθμησης για να λάβετε αυτές τις τιμές από το παράθυρο ιδιοτήτων.Then use this object name in the enumeration method to get those values from the property pane.
{ "settings": { "displayName": "Visual Settings", "description": "Settings to control the look and feel of the visual", "properties": { "method": { "displayName": "Data Look", "description": "Control the look and feel of the data points in the visual", "type": { "enumeration": [ { "displayName": "Circle", "value": "circle" }, { "displayName": "Square", "value": "square" }, { "displayName": "Ellipse", "value": "ellipse" }, { "displayName": "Number", "value": "number" }, { "displayName": "Shade", "value": "shade" }, { "displayName": "Color", "value": "color" }, { "displayName": "Pie", "value": "pie" } ] } } } }
Ανοίξτε το αρχείο src/settings.ts.Open the src/settings.ts file. Δημιουργήστε μια κλάση
CorrPlotSettings
με την δημόσια ιδιότηταmethod
.Create aCorrPlotSettings
class with the public propertymethod
. Ο τύπος είναιstring
και η προεπιλεγμένη τιμή είναιcircle
.The type isstring
and the default value iscircle
. Προσθέστε την ιδιότηταsettings
στην κλάσηVisualSettings
με την προεπιλεγμένη τιμή:Add thesettings
property to theVisualSettings
class with the default value:"use strict"; import { dataViewObjectsParser } from "powerbi-visuals-utils-dataviewutils"; import DataViewObjectsParser = dataViewObjectsParser.DataViewObjectsParser; export class VisualSettings extends DataViewObjectsParser { public rcv_script: rcv_scriptSettings = new rcv_scriptSettings(); public settings: CorrPlotSettings = new CorrPlotSettings(); } export class CorrPlotSettings { public method: string = "circle"; } export class rcv_scriptSettings { public provider; public source; }
Μετά από αυτά τα βήματα, μπορείτε να αλλάξετε την ιδιότητα της απεικόνισης.After these steps, you can change the property of the visual.
Τέλος, η δέσμη ενεργειών R πρέπει να αρχίζει με μια ιδιότητα.Finally, the R script must start with a property. Εάν ο χρήστης δεν αλλάξει την ιδιότητα, η απεικόνιση δεν θα έχει καμία τιμή για αυτήν την ιδιότητα.If the user doesn't change the property, the visual doesn't get any value for this property.
Για τις μεταβλητές χρόνου εκτέλεσης R για τις ιδιότητες, η σύμβαση ονομασίας
<objectname>_<propertyname>
, σε αυτή την περίπτωση,settings_method
.For R runtime variables for the properties, the naming convention is<objectname>_<propertyname>
, in this case,settings_method
.Αλλάξτε τη δέσμη ενεργειών R στην απεικόνιση, ώστε να συμφωνεί με τον ακόλουθο κώδικα:Change the R script in your visual to match the following code:
library(corrplot) corr <- cor(dataset) if (!exists("settings_method")) { settings_method = "circle"; } corrplot(corr, method=settings_method, order = "hclust")
Η τελική σας απεικόνιση μοιάζει με το ακόλουθο παράδειγμα:Your final visual looks like the following example:
Επόμενα βήματαNext steps
Για να μάθετε περισσότερα σχετικά με τις απεικονίσεις R, ανατρέξτε στο θέμα Χρήση απεικονίσεων R Power BI στο Power BI.To learn more about R-powered visuals, see Use R-powered Power BI visuals in Power BI.
Για περισσότερες πληροφορίες σχετικά με τις απεικονίσεις R στο Power BI Desktop, ανατρέξτε στο θέμα Δημιουργία απεικονίσεων του Power BI με χρήση της R.For more information about R-powered visuals in Power BI Desktop, see Create Power BI visuals using R.