Object "mydata" not found error in rmd - r

when I run the following code in R Studio, it works (produce the graph). But, when I knit the document to HTML it gives an error as: object "mydata" not found. How can I fix it?
mydata <- read.csv("impt5.csv")
get("mydata")
xyplot(gdppc ~ human + unemp +inv+indust | class, data = mydata,
auto.key = list(x = .85, y = .035, corner = c(0, 0)),
layout = c(4,1), main = "Per capita gdp by class")

set the working directory to the folder containing your CSV file.
You might want to create a R markdown file instead (.rmd), this is a easy to create an HTML document,or for any other format you want.
Please look into the R markdown help document.
Hope this helps.

More precisely, use:
mydata <- read.csv(paste0(getwd(),"impt5.csv"))
paste0() is the paste() function with sep=" "

Related

Task Schedular Giving Error while scheduling R Script for saving PDF

I am scheduling R script which contains ggsave for saving pdf.
my code is running but on the line of ggsave("plot.pdf), it is skipping code. But instead of saving pdf if i use png format then it is fine. but only for pdf it is giving problem.
Below is my sample code.
library(ggplot2)
library(data.table)
a <- data.frame(a = c(1:5))
p <- ggplot(data.frame(x = 1:5, y = 1:5), aes(x, y)) + geom_point()
fwrite(a,"abc1.csv")
ggsave("plot.pdf")
Does ggsave(p, "plot.pdf", device = "pdf") work? You may not have been specifying the plot to be saved or perhaps it doesn't know to export as pdf from only the file path that you gave?
EDIT: It should be ggsave("plot.pdf", p, device = "pdf") so that the arguments are in the correct order.

Why I get different results when running code in R Markdown with knitr than in R script

I have an R code that I've written which is in a normal .r file, now I want to make a markdown html report so I'm basically running the same code, but in chunks with text in between.
I have the weirdest problem where some code works as it worked in the regular r file, but some code produces different results entirely. For example:
mydata_complete_obs %>% select(-(prom_id:end_a)) %>% select(qualified, everything()) %>%
cor(use = "complete.obs", method = "spearman") %>%
corrplot(type = "lower", method = "circle", diag = F, insig = "pch", addCoef.col = "grey",
p.mat = res1$p, title = "Spearman Correlations")
The above code which produces a corrplot does work and produces the same graph as I get in the .r file, but a simple summary() function gives me different things - the correct output being produced in the .r file and in the markdown report I get all zeroes (min, 1st quartile, median, mean, etc. - all 0!). This is the chunk for the summary():
```{r hists, echo = FALSE, warning = FALSE, message = FALSE, error =
FALSE, results="markup"}
summary(mydata_complete_obs)
```
What can be wrong? I'm loading all the libraries and read in the data from an .rds file in the first chunk, and then later use mydata_complete_obs to produce graphs and summaries. If I understand correctly I don't need to load the data for each chunk separately, because I thought that was the problem.
Ok, I have solved the problem. I had to change the query in the database so that the format of the columns will be INT instead of BIGINT (that's in Impala), then when I read it in R, everything worked and the strange behaviour disappeared.

R chart convert to html format without other files

My sample dataset:
library(plotly)
library(htmlwidgets)
d <-
data.frame(
"name" = c("bily", "mary", "ken"),
"fruit" = c("cherry", "apple", "apple"),
"count" = c(1, 10, 30)
)
gg <- ggplot(data = d,
aes(x = fruit,
y = count,
group = name)) +
geom_line(aes(color = name)) +
geom_point(aes(color = name))
plotly <- ggplotly(gg)
save graph:
d <- # please put your directory where you want to save the graph
"xxxx/graph.html"
I have searched these three functions that can output the HTML format but they will also make other files and I cannot solve it.
#htmlwidgets::saveWidget(as_widget(plotly), d)
#htmltools::browsable(plotly)
htmltools::save_html(plotly, d)
I am trying to convert my graph, which uses from ggplot function to ggplotly function, to HTML format but I face a problem. When I save the graph, the function will also make other files such as plotlyjs, jquery, and crosstalk. I hope only the HTML file can be made because it is too hard to put the files on the website if each graph has different files.
These are the files when I run the code. My expected result is that only the HTML file is made.
The other files are saved in the directory of the lib but I hope they are not provided and I can run the HTML file. Is it possible to do? or Are there any other methods?
Just add selfcontained = TRUE to your last line:
htmlwidgets::saveWidget(as_widget(plotly), selfcontained = TRUE, file = "xxxx/graph.html")

Multiple timeseries zoo objects in one panel; blogdown serve_site() doesn't load plot

I've several zoo objects generated through a loop. I'd like to plot all objects in one panel. I suppose it can be done by first merging zoo objects to a matrix-like zoo object and and supply plot.type = "multiple" and screens = ncol(merged-zoo-object) arguments in plot.zoo(), but I can't figure out how to merge.
library(zoo)
for (i in 1:3) {
value <- rnorm(n = 12, mean = i)
index <- seq(as.Date("2000/1/1"), by = "month", length.out = 12)
ts <- zoo(x = value, order.by = index)
plot.zoo(ts)
}
UPDATE
I've managed to create the plot (answered) and I want to create a blogpost with blogdown.
The problem you had with blogdown is that you were using an absolute local path /home/rsl/r-plots/sample.png. In general, it is a bad idea to use absolute paths, since they are not portable. In this specific case, when you publish your post to a web server, the meaning of /home/rsl/r-plots/sample.png will change. It indicates the file /home/rsl/r-plots/sample.png under the root directory of your website. For example, if your website is http://example.com, the file path means http://example.com/home/rsl/r-plots/sample.png, which is definitely not what you actually mean. The web server knows nothing about your local files on your computer, and certainly cannot find any files on your local disk, so the plot won't load on the web page.
In short, remove this:
ggsave(filename = "sample.png", path = "~/r-plots")
When you author a document using knitr, or any packages based on knitr, such as rmarkdown, bookdown, and blogdown, there is no need to manually save plots using ggsave() or R graphical devices. R plots will be automatically save behind the scenes.
This sort of works, but code could've been cleaner.
require(zoo)
require(ggfortify)
merged.zoo <- zoo()
for (i in 1:3) {
value <- rnorm(n = 12, mean = i)
index <- seq(as.Date("2000/1/1"), by = "month", length.out = 12)
ts <- zoo(x = value, order.by = index)
merged.zoo <- merge.zoo(merged.zoo, ts)
}
autoplot.zoo(object = merged.zoo, geom = "line")
ggsave(filename = "sample.png", path = "~/r-plots")
I now create a new post with blogdown::new_post(title = "title") and add the below text in *title.rmd file which is created by new_post command.
---
title: title
author: ~
date: '2017-10-05'
slug: title
categories: []
tags: []
---
![I want to see this plot](/home/rsl/r-plots/sample.png)
I expect to see the plot in the post named title when serve_site() is executed followed by build_site() with default settings. But plot doesn't load.

Why can't the pdf file created by gage (a R packge) be opened

I am trying to use Gage package implemented in R to analyze my RNA-seq data. I followed the tutorial and got my data.kegg.p file and I used the following script to generate the heatmap for the top gene set
for (gs in rownames(data.kegg.p$greater)[1]) {
outname = gsub(" |:|/", "_", substr(gs, 10, 100))
geneData(genes = kegg.gs[[gs]], exprs = essData, ref = 1,
samp = 2, outname = outname, txt = T, heatmap = T,
Colv = F, Rowv = F, dendrogram = "none", limit = 3, scatterplot = T)
}
I did get a pdf file named "NOD-like_receptor_signaling_pathway.geneData.heatmap.pdf", but when I open this file with acrobat reader or photoshop, it gives the error information that this file has been disrupted and cannot be recovered. Could anyone help check this file (https://www.dropbox.com/s/wrsml6n1pbrztnm/NOD-like_receptor_signaling_pathway.geneData.heatmap.pdf?dl=0) to see whether it is really disrupted and is it possible to find a way to recover it?
I also attached the R workspace file (https://www.dropbox.com/s/6n5m9x5hyk38ff1/A549.RData?dl=0). The object "a4" is the data with the format ready for gage analysis. It contains the data of the reference sample (nc) the treated sample (a549). It can be accepted by gage for analysis but generate the heatmap pdf file which cannot be opened (above). Would you mind helping me check whether these data can be properly used to generated the correct gage result?
Best regards.
I'm running into a similar problem myself. Not 100% sure but I think this problem occurs when there is no heatmap to plot. In my case, I was doing as.group comparison with ref and sample selections. I think the software treats this circumstance as a sample n of 1 and can't really show a differential heatmap. When I tried using 1ongroup setting, I was able to visualize the pdf file.

Resources