Extract chart data from website - R Studio - web-scraping

I want to extract all chart data from the following website: https://www.kayak.com.au/flight-trends. Id like to know how to extract via R Studio using the "inspect" option on Chrome. Your help is much appreciated.

Related

DT: is there a way to display dataframe using r in google colab as interactive table?

Are you aware of a "nice" way to display the dataframe in colab using R, preferably as an interactive table?
I am trying the code below (which I typically use in rstudio) but it is not working in colab.
library(datasets)
data(iris)
library(DT)
DT::datatable(iris) #this typically displays a beautiful interactive html table, but not working with colab
#these display a simple table
View(iris)
fix(iris)
I did some search and looks like there is a nice way to display interactive table with filters in colab under python https://colab.research.google.com/notebooks/data_table.ipynb#scrollTo=jcQEX_3vHOUz
but I could not find anything similar for R. Do you have any suggestions?
Thank you

How to embed a plotly graph on a wordpress website?

I am trying to embed a plotly graph I created in R on a wordpress website. It seems a lot more difficult than it should. Perhaps I am missing something obvious. Here is what I tried:
solution 1: saved the graph as an html using htmlwidgets::saveWidget(as_widget(Basic_Spending_Graph), file = "Basic_Spending_Graph.html"). Then use that html to either embed the whole file or html source code into the website. There are numerous problems with this approach. Firstly if I embed file it embeds a link to the file where you can open the page rather than fully embedding the graph within the page. Secondly the file is 3 Mbs, which over time could put a strain on website speed which is using shared hosting.
solution 2: export plotly graphs from R to chart studio which allows you to host graph on their server and generate an html embedding snippet. This seems like a great solution, but I am struggling to find an easy way to export from R to chart studio, since I already spent quite a lot of time creating the graphs in R. Apparently there is a way to export the charts to chart studio, but nobody seems to explain how?
I may be missing something very obvious considering plotly was designed with web in the mind! Any advice would be greatly appreciated. What is the best way of getting plotly charts on the webpage?
Thanks!
I can't help you with solution 1, but re: solution 2, while this is maddening to find, there is actually a pretty simple solution.
First, you need to register with chart studio online and generate your api key. For whatever reason, to generate your api key you need to view your "settings." (see here: https://chart-studio.plotly.com/settings/api)
Once you've got your api key, you can run the following code from R and it will upload your plotly chart to your profile.
#first we register to upload to plotly
key <- readLines("path/to/your/api/key")
Sys.setenv("plotly_username"="<your username>")
Sys.setenv("plotly_api_key"=key)
#now we post to plolty
plotly_POST(
x = last_plot(),
file = "<whatever you want to name your plot>",
fileopt = "overwrite",
sharing = c("public"),
world_readable=TRUE
)
#note that evidently, plotly_POST is depreciated, though it worked for me as of 11/2020
#use instead the call below, with the same arguments
api_create(
x = last_plot(),
file = "<whatever you want to name your plot>",
fileopt = "overwrite",
sharing = c("public"),
world_readable=TRUE
)

R - Better way to view data frame outputs on console

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")

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.

R: Scheduling code in a webpage

I'm trying to build a webpage displaying a few graphs. I want to use R to build the graphs, and I want these graphs to automatically update themselves periodically. For example, a webpage showing a graph of the stock price of a particular company over time, which refreshes every day with new data.
what is the best way to approach this? Using Rook to run the R-scripts? can I use it along Markdown, for example, to make the html webpage? Or do you suggest something else?
You can make your plots in a R file and write your webpage in markdown in which you call your R objects and plots. Alternatively you can also run the R code directly in the markdown file. With the knit2html function of the knitr package you can create the html page with the desired output. You can find basic examples on the knitr webpage.
You can schedule these file(s) on your own computer or on a server to update the data of the html output every day. If you have a machine that runs on Windows you can use the Windows Task Manager to run the batch file.
EDIT:
Here you can find a worked out example.

Resources