Browser/viewer to see an R dataset in Azure Notebooks? - r

I understand that the command line can be used to view data, e.g. head(), tail(), etc.
However, I'd like to view the whole dataset. Is this possible within Jupyter Azure Notebooks?

(Untested) if you have a DataFrame object, calling the name should print the entire dataset.
For example, if your dataframe is named dta, just have
dta
at the end of a cell, and run that cell.

Related

In R, is there any way to automatically run an output after it has been printed?

Sorry if this sounds a bit messy, I'm new to using R. I have a data frame named "AB13" ( a postcode) and I am using a list of all postcodes which I imported as a .csv file. Is there any way I can print one of the columns and run it through the console automatically. Say print the 10th element of the list, which is AB13. Then it will display AB13 ( the data frame)?
I have tried using print, cat and multiple other print functions but I haven't been able to get anywhere.
You can do
get(your_list[[10]])

How to create a table in a Power BI R Visual

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.

tibble table display issue when using R in jupyter notebook

I just started using R in Jupyter notebook. There seems to be some issue displaying tibble table.
for example,
mtcars
Everything is normal.
If mtcars is converted to tibble,
car<-as_data_frame(mtcars)
car
The table displayed is totally screwed....
Anyone knows why? Do I need to set some options in jupyter notebook?
A follow-up question: How to control the number of rows for table output (not using head())? Is there any notebook options I can set? Is there any way that display the whole table with some page number button like those in R notebook?
Are you sure that you are using the latest version of Jupyter Notebook? I get exactly the same printout for the base data frame and the tibble.
As for the number of rows to show, I don't know of any notebook setting. If you don't want to use head(), you can always do:
mtcars[seq.int(15),]
with whatever number of lines you want instead of 15.

Command to use with easy way the insert of R dataframe

I have a dataframe loaded successfully in R.
I would like to give the data of df to someone else to use them with quick and easy way without need to load again the file into a df.
Which is the command to give the whole data of df (not the str())
You can save the file into a .RData using save or save.image, depending on your needs. First one will save specific objects while the latter will dump the whole workspace to a file. This method has the advantage of working on probably any R object.
Another option is as #user1945827 mentioned, using dput which will produce a string that is parseable into another R session. This will not work for complex (like S4) objects.

R: Capture output of regression in string [duplicate]

I have multiple regressions in an R script and want to append the regression summaries to a single text file output. I know I can use the following code to do this for one regression summary, but how would I do this for multiple?
rpt1 <- summary(fit)
capture.output(rpt1, file = "results.txt")
I would prefer not to have to use this multiple times in the same script (for rpt1, rpt2, etc.), and thus have separate text files for each result. I'm sure this is easy, but I'm still learning the R ropes. Any ideas?
You can store the result as a list and then use the capture.output
fit1<-lm(mpg~cyl,data=mtcars)
fit2<-lm(mpg~cyl+disp,data=mtcars)
myresult<-list(fit1,fit2)
capture.output(myresult, file = "results.txt")
If you want multiple output sent to a file then look at the sink function, it will redirect all output to a file until you call sink again. The capture.output function actually uses sink.
You might also be interested in the txtStart function (and friends) in the TeachingDemos package which will also include the commands interspersed with the output and gives a few more options for output formatting.
Eventually you will probably want to investigate the knitr package for ways of running a set of commands in a batch and nicely capturing all the output together nicely formatted (and documented).

Resources