Power BI has a feature that lets you create visuals from R scripts. When you add data (columns) to the Values field, it automatically creates a data frame from those columns, which is calls "dataset"
It even shows the code it runs:
dataset <- data.frame(Col1, Col2, Col3, etc.)
My question is, how could I go about viewing the data in this dataframe?
I've tried running code like:
g <- xtabs(dataset)
g
print(g)
but it just returns the error: "No image was created. The code didn't result in creation of any visuals. Make sure your R script results in a plot to the R default device."
On the PowerBI website it says: 'Only plots that are plotted to the R default display device are displayed correctly on the canvas'. In simpler terms it means that if an object is printed to the console, it will not be displayed in PowerBI.
The tableHTML package let's you create HTML tables that will be displayed in the R default display.
library (tableHTML)
g <- tableHTML(dataset, rownames = FALSE)
print(g)
Note: you need to make sure tableHTML is installed in the library of R that is used by PowerBI. You can see the path for R used by PowerBI in the Global.options under 'R scripting'. Use the path that is displayed there in the code snipped below (this needs to be run from R/RStudio rather that PowerBI):
install.packages('tableHTML','/path/to/R/R-x.x.x/library)
You need to use a function that turns the table into a visual. If you install the gridExtra package in R, you should be able to do this in PowerBI:
g <- xtabs(dataset)
gridExtra::grid.table(dataset)
Bear in mind, the grid.table() requires a lot of detailed programming to control the image size, margins, font size, etc.
If you're just doing something simple like a crosstab, that's something you should be able to calculate as a Measure in PowerBI, and then use the built in table or matrix visuals.
Related
I am starting to write a book using bookdown and trying to find the best workflow. First of all I am trying to make frequency tables, crosstables and model summaries, using bookdown with pdf format mainly, but I am pretty sure my advisor will love I send word documents for revision, so also getting a word output would be awesome. Word output can be achieved producing first the .html file and then opening with Word. So .html and .pdf output at the same time are desiderables.
Tables are the main problem because seems impossible to find a productive way to produce frequency, crosstable and summarymodels without too excessive pain in both formats at the same time. Using knitr has an added value since it and kableExtra provide a set of customizations.
So far I've tried:
1) sjmisc / sjPlot with the frq / sjt.frq (deprecated) function
but only provides html objects as it is said in the comments of this
post, also has the lack of labels for cross reference in the
document.
2) stargazer package but doesn't have frequency tables available and
.pdf and .html output at the same time seems impossible to achieve.
However summary models are pretty good!
3) descr package with the freq function used inside kable:
f <- descr::freq(iris$Species, plot = F)
kableExtra::kable(data.frame(f), caption = "Foo")
However crosstab output cant be coerced to data.frame while using crosstab function.
Any ideas?
Thanks in advance!
https://haozhu233.github.io/kableExtra/bookdown/index.html
https://github.com/haozhu233/kableExtra/tree/master/docs/bookdown_example
Check out this sample document (I'm still working on this document recently so you may expect some change). Note that you might need the latest dev version of kableExtra and bookdown for some of the formatting features mentioned there.
I'm looking for a better way to view output of the data frame in the console.
The computer that I'm using has high security restrictions, so installing many of the more popular packages such as tidyr and tibble is not possible.
What I want is for the ouput to be more compact and not wrap in the console.
Is there a way to use base R to improve the console output for data frames?
You could edit your data.frame without changing it. It will open a new window for you to see. There is an editor parameter which allows to choose an editor of your choise.
Or you could page through the data:
page(mtcars, method = "print")
I want to pass data from Spotfire to R and then display the plot constructed by R.
What is the best way to do this?
I’ve figured out the trick of putting images into Spotfire. It’s not hard if you follow these directions, but it’s done in a way very different from how you guess you would do it in Spotfire, and that’s why it took me awhile to figure out.
Here’s an overview of how to do it. You create a DocumentProperty which is a binary object, you write some Spotfire code that gives a value to that Document Property, and you display that binary object using a Spotfire Property Control of the “Label” type.
The confusing parts are that you DON’T use the Spotfire “Insert Image” tool at all, and that you DON’T use the filename generated inside the R code in Spotfire at all. Once you get used to the idea that the two most obvious ways you think you would approach the problem in Spotfire are entirely useless and wrong, you can make some progress.
I’ll leave out the spiderplot specifics because the code’s pretty long.
Here’s what you do.
1) Create a document Property in Spotfire of type “Binary”, e.g., “imageThatGoesBackToSpotfire”
2) You write some R code that generates an image and writes it to a file:
# get a temporary directory name on the local machine. You wouldn’t need to do this is you were just
# going to run it on your own, but you need to do it if you intend to let anybody else run it on their own machine.
tempfilebase = tempfile()
# take the tempfilebase and prepend it to a filename.
myFilename<-“someFileName.jpg”
myFullFilename <- paste(tempfilebase,myFilename,sep="")
#open a jpeg
jpeg(filename=myFullFileName)
# generate the image, however you normally would in R
plot(input)
# close the file
dev.off
# open a connection to that file.
myConnection<-file(myFullFileName,open=”rb”)
imageThatGoesBackToSpotfire<- data.frame(r=readBin(myConnection, what="raw", n=(file.info(myFullFileName)$size)))
close(myConnection)
3) Run your R script, above. Select some columns that are the “input” to the plot, and make the R script return outputs to the “imageThatGoesBackToSpotfire” DocumentProperties.
4) Create a text area in Spotfire.
5) Insert a Property Control into the text area of type “label”. (Click on the icon that’s circled in the picture below). This opens a dialog,
You need to register a data function with inputs and outputs, and the specific PNG data needs to be returned as a binary label.
Some details: http://spotfire.tibco.com/tips/2014/02/25/dynamically-displaying-images-in-a-text-area/
I'm new with R in QGIS, I could write a simple script, and I want to obtain the table resulting, the table which R uses to create the plot graphics.
How can I do that?
This is the script:
##Point pattern analysis=group
##Layer=vector
##Titulo=string
##showplots
library("maptools")
library("spatstat")
K <- Kest(as.ppp(Layer))
plot(K, main=Titulo)
Can anyone help me?
The QGIS processing module runs each R script in a separate R session. If you want to save anything created then you need to save it to a file in your script, for example:
save(K,file="K.RData")
Then in another R session you can do:
load("K.RData")
library(spatstat)
and now K is restored.
You might want to pass the save file name as another parameter to your processing script, or you may want to not do this further work in QGIS...
If you want to save this as a DBF file then there's a problem caused by the fact that K is a special kind of data frame - use write.dbf(as.data.frame(K),"/path/to/K.dbf") to convert it to a plain data frame for writing. This will lose some of the information such as labels and names of the various components but you can't store irregular data in a DBF.
I wonder how to redirect R graphs to MS Word? Like sink() redirect the R output to any file but not the graphs. I tried R2Wd but sometimes it doesn't work properly. Any comment and help will be highly appreciated. Thanks
To answer your direct question, the best way to get the results of R scripts and plots into word is probably via some form of Sweave. Look up odfweave to send R output to a LibreOffice file that can then be converted to word, or even opened directly in Word if you have the right plugin.
To create plots that can be editable (i.e you can alter the look of plots, move the legend etc) I would recommend saving the plot to an svg format (scalable vector graphic) that you can then edit using the excellent free vector graphics app inkscape.
For instance, if I create my ggplot2 graph as an object
library(ggplot2)
dataframe<-data.frame(fac=factor(c(1:4)),data1=rnorm(400,100,sd=15))
dataframe$data2<-dataframe$data1*c(0.25,0.5,0.75,1)
testplot<-qplot(x=fac, y=data2,data=dataframe, colour=fac, geom=c("boxplot", "jitter"))
You can use the Cairo package, which allows creation of svg files, I can then edit these in Inkscape.
library(Cairo)
Cairo(600,600,file="testplot.svg",type="svg",bg="transparent",pointsize=8, units="px",dpi=400)
testplot
dev.off()
Cairo(1200,1200,file="testplot12200.png",type="png",bg="transparent",pointsize=12, units="px",dpi=200)
testplot
dev.off()
For more info read this previous question that has more good answers Create Editable plots from R
Also, you can follow this advice from Hadley, and save the actual ggplot2 object, then load it later and modify it
save(testplot, file = "test-plot.rdata")
# Time passes and you start a new R session
load("test-plot.rdata")
testplot + opts(legend.position = "none")
testplot + geom_point()
To get sink() like behavior with MSword look at the wdTxtStart function in the TeachingDemos package. This uses R2wd internally, so you will see similar functionality, this just sends everything you do to the word document.
Graphs are not sent automatically since you may be adding to them, but once you know you are finished with the graph you can use wdtxtPlot to send the current graph to the word document.
If you know what you want to do ahead of time then sweave or something similar is probably the better approach (as has already been mentioned). The group that created Rexcel are also working on Sword that does sweave like things within MSword.