RMarkdown error with melt() function - r

I'm trying to create a file word starting from a program that on console perfectly works.
The problem is that when it reaches a function that uses melt() it generates an error
cannot find melt function
and stops compiling the code.
The package reshape is installed, and I tried to rerun the code without using markdown and it doesn't generate any error.
Is possible that RMarkdown doesn't support melt() function?

Installing a package is not enough — you need to load it. It’s possible that it works in your console because it was still loaded from before, or because you installed it in the current session.
To use the package, you need to add the following into your (R Markdown) code, before using any of its functions:
library(reshape)
Alternatively, you can just prefix all its functions with reshape::. So, instead of calling melt, you’d call reshape::melt. That’s less commonly done but there’s nothing wrong with it.

Load the package inside R markdown. Loading the package outside R markdown is not enough. The loading of package should be inside R chunk in R markdown.
```{r}
library(reshape)
melt(iris)
```

I had the exact same problem and finally solved it. It was caused by setting eval=FALSE inside the code chunk that loaded the libraries in my Markdown document, including reshape2. When I removed that option, it finally worked:
{r setoptions, ***eval=FALSE***}
library(ggplot2)
library(plyr)
library(dplyr)
library(reshape2)
library(knitr)

Related

How to use a function from an external package in the example part of an R file

I am working on an R package and in the #examples part of my R file I call on ggplot from the ggplot2 package in my example code. I am not using any part of ggplot2 in my actual package code and so I wasn't sure how to call the ggplot2 functions.
Originally I thought I would just need to use ggplot2::ggplot() but the R CMD Check complained. So I added it to the Imports list in the DESCRIPTION file but then R complained that it shouldn't be in there since I am not using it in my code functions. I then thought to add it to using #import ggplot2 ggplot in my .R file but it seemed to complain about that as well.
Can anyone tell me how I should reference external functions in only the example part of my .R file? I tried reading through the R help pages on CRAN as well as Googling for an answer but I guess I wasn't able to find what I needed.
Packages used for examples only should be listed in 'Suggests:' instead of 'Imports:' in the DESCRIPTION file

Determining if there are unused packages in an R script [duplicate]

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.
Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.
Update 2020-04-13
I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.
I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:
Functions matched from package data.table: data.table
**No exported functions matched from iotools**
Have you considered using packrat?
packrat::clean() would remove unused packages, for example.
I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.
My approach always is to close my R script or IDE (i.e. RStudio) and then start it again.
After this I run my function without loading any dependecies/packages beforehand.
This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

R markdown, hiding the library output

I am working on a rmarkdown presentation. I am trying to show the usage of cast function. However, since the reshape package is necessary, to run the cast function, I need to load reshape library as below.
{r package_options, echo=TRUE}
library(reshape)
cast(datam, isim~Ay, value="Sirano")
However, after knitting the codes, I face with the output ;
I just need to see the name of the library on the screen which is library(reshape) , and also I want it to be used to run cast function, but I dont want to see the package outputs as shown in the picture.
Would someone help about that?
If you want to hide all these messages you have to put :
```{r,warning=FALSE,message=FALSE}
library(reshape)
```

How can I tell which packages I am not using in my R script?

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.
Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.
Update 2020-04-13
I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.
I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:
Functions matched from package data.table: data.table
**No exported functions matched from iotools**
Have you considered using packrat?
packrat::clean() would remove unused packages, for example.
I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.
My approach always is to close my R script or IDE (i.e. RStudio) and then start it again.
After this I run my function without loading any dependecies/packages beforehand.
This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

Access function from current package when using R CMD check with vignette

I'm putting together a package; for simplicity with one function and one vignette illustrating its use.
I was able to run R CMD check packagename with no difficulties before I tried adding the vignette.
The package has a functionfoo.R in the R directory of packagename (it makes a plot with base graphics).
The vignette, in the vignettes directory, (an .Rnw file) calls function foo like this:
<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
foo(df0)
#
I'm tying to play 'by the rules' but running R CMD check packagename as usual gives:
When sourcing 'foo.R':
Error: could not find function "foo"
Execution halted
I've tried adding the following to the .Rnw file, which didn't help:
\begin{document}
\VignetteDepends{packagename}
I've also tried this with no success:
<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
source("foo.R")
foo(df0)
#
Note that the NAMESPACE file already contains the following:
export(foo)
Questions:
Do I need to add a specific source() command in the .Rnw file every time I call a function from the package? If so, how do I specify the path (i.e. where is R CMD check starting from when checking the vignette?)
Or should I take the easy way out by adding the following to the DESCRIPTION file:
BuildVignettes: False
(As I'm able to build a .pdf from the existing .Rnw file).
I'm trying to follow the advice in Writing R extensions.
Your vignette needs to have library("mypkg") at the top so that your own functions, like foo, can be found. I believe this is because the vignette building runs in a clean environment so it doesn't know about your package or any other for that matter unless you bring it up.
If you have such a line already, put a minimal example of your vignette into your question, and include your sessionInfo() as we may need that to figure it out.

Resources