Analysis of deviance table model output in HTML - html

I am trying to export the output of an 'Analysis of deviance table' in HTML format, so that it can be inserted into a word document.
I created a GLM model as follows:
newmod <- glm(cbind(Recaptured, predated) ~ Morph * Plant * Site, data =
survival, family = binomial)
Running the following code gives me the output that I would like to export to HTML:
anova(newmod,test="Chisq")
I have tried the following code to create a HTML table using stargazer, however it doesn't seem to be working:
anova_mod<-anova(newmod,test="Chisq")
stargazer(newmod, type="html", out = "anova_output.htm")
Is there a simple way of doing this in r? I have managed to successfully export the summary statistics, but what I really need is the Analysis of deviance table.

I believe you are looking for:
print(xtable(anova_mod), type = "html")
as indicated by this answer: Exporting R tables to HTML
Here is my full code for reproducing something similar to your question:
plant.df = PlantGrowth
plant.df$group = factor(plant.df$group,labels = c("Control", "Treatment 1", "Treatment 2"))
newmod = lm(weight ~ group, data = plant.df)
anova_mod=anova(newmod)
anova_mod
install.packages("xtable")
require(xtable)
print(xtable(anova_mod), type = "html")
You can then paste the output to an html vizualizer such as: https://htmledit.squarefree.com/ to see the resulting table.
Instead of printing it, you can write it to a file. I have not personally tested this part, but the second answer in this question should work for you: Save html result to a txt or html file
Note: You can also reference all parts of the anova_mod separately by adding a $ after it like anova_mod$Df.

Related

Sending Redshift Query Result in Email with Python and Airflow

I am making a daily DAG that will run a query in Redshift and will send the result table in an email.
I currently manage to take the query results and put it into a DataFrame. My problem is using send_email (from airflow package) and choosing the parameters for it to show the data.
The output of the email is unfortunately not a table, just the results being all over the place as text.
Here is what I wrote for send_email :
html_content = f"""
<html><body><p>Hello,</p>
<p>Found {num_of_late_dags} Late DAGs:</p>
{late_dags}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""
send_email(to=v_email_recipients_daily_report,
cc=cc,
bcc=bcc,
subject=f'Daily DAG Load Report',
html_content=html_content,
mime_subtype='alternative') ```
Update:
I used pd.to_html() after html_content in .format(to_html()) but that didnt work.
I used to_html() when creating the data as a DataFrame it worked. Not sure why, they should do the same.
So it looked like this: df = pd.DataFrame(data, columns=['X',Y']).to_html()
Then putting df inside the HTML content worked.

Best way to display MySql data in Tkinter GUI

Just wondering if there is a better way to display MySql data to users of my app.
Basically I store look-up data then put it in a pop-up window for viewing:
for row in all_reinforcement_data:
r_total = ("Total number of reinforcement entries", mycursor.rowcount)
r_id = ("\n\nId", row[0])
messagebox.showinfo("Reinforcement Data Results", r_total + r_id)
Which doesn't look too polished but gives me what I want:
Is there any other ways of showing the user the data. In some form they could copy and paste from, ideally an excell spreadsheet or something similar.
In a messagebox I don't believe you could do it. You could attempt to do it in a normal window with an entry that you could only copy out of, similarly to this question.
For example, you could do this to show the rows in a simple window:
from tkinter import *
row_info = Tk()
row_info.title("Reinforcement Data Results")
title = Label(text="Total number of reinforcement entries:")
title.pack()
data = Entry(row_info, borderwidth=0, justify='center')
data.insert(END, mycursor.row_count)
data.pack()
data.configure(state="readonly")
close = Button(row_info, text="Ok", command=row_info.destroy)
close.pack()
row_info.mainloop()

How to parse Table from Wikipedia using htmltab package?

All,
I am trying to parse 1 table located here https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population. And I would like to use htmltab package to achieve this task. Currently my code looks like following. However I am getting below Error. I tried passing "Rank", "% of world population " in which function, but still received an error. I am not sure, what could be wrong ?
Please Note: I am new to R and Webscraping, if you could provide explanation of the code, that will be great help.
url3 <- "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population"
list_of_countries<- htmltab(doc = url3, which = "//th[text() = 'Country(or dependent territory)']/ancestor::table")
Error: Couldn't find the table. Try passing (a different) information to the which argument.
This is an XPath problem not an R problem. If you inspect the HTML of that table the relevant header is
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">
Country<br><small>(or dependent territory)</small>
</th>
So text() on this is just "Country".
For example this could work (this is not the only option, you will just have to try out various xpath selectors to see).
htmltab(doc = url3, which = "//th[text() = 'Country']/ancestor::table")
Alternatively it's the first table on the page, so you could try which=1 instead.
(NB in Chrome you can do $x("//th[text() = 'Country']") and so on in the developer console to try these things out, and no doubt in other browsers also)

R Scraping html webpage using XML

I am trying to scrape this webpage using the following code.
library(XML)
url <- html("http://www.gallop.co.za/")
doc <- htmlParse(url)
lat <- xpathSApply(doc,path="//p[#id=Racecards]",fun = xmlGetAttr , name = 'Racecards')
I looked at the webpage and the table i want to scrape is the racecard table, primarily to get the links to where the racecard data is.
I used selector gadget which returns the xml path as:
//*[(#id = "Racecards")]
However, when i use the R code, it returns a zero list. It feels like i'm getting the xml path wrong somehow, what is the correct way to return the table but also return the links within the table?
It seems that the data are transported through json and use js to insert into html. So you can't get the data from html. You can get it directly from json.
library(RCurl)
library(jsonlite)
p <- getURL("http://www.gallop.co.za/cache/horses.json")
fromJSON(p)

How to code Regular Expression with an IF ELSE function

I am trying to build a scraper to extract key metrics from a website. One of the metrics is to find the Model number of the products on the website. I am using Outwit as the base program but I'm now stuck when it comes to some exceptions in the sites source code.
Here is an example of the source code:
var zx_description = "Test Dress<br/><br/>Model: Nice01j<br/>
Where the information I am looking to extract is: Nice01j
The issue is that for some products the word Modell is spelled Model and also that the end of the actual model name/number does not always end with a row break but in some cases the code might look like this:
var zx_description = "Test Dress<br/><br/>Model: Nice01j";
I have managed to create the RegEx before the Modell number as below:
/var zx_description[\s\S]+?Modell:/
So now Im looking to alter it so that it also takes in consideration that the spelling might be Model with just one "l".
Also the second part is to create a RegEx for the capturing of te info after the actual Model name which in should be something like:
IF: < br comes before "; then < br ELSE ";
Is this possible to state in a Regular Expression and if so how would I do that?
Based on your use of [\s\S] it looks to me like you need to run through a regular expression tutorial. For your question, specifically focus on optional items and capturing groups.
http://www.regular-expressions.info/tutorial.html