Highlight R syntax using knitr and Package Highlight - r

I try to highlight my R code using knit and Highlight pachages with Rstudio, but i have errors when kniting;
There's my code :
opts_chunk$set(fig.width=7, fig.height=5)
opts_knit$set(use.highlight = TRUE)
opts_knit$set(out.format = "html")
opts_chunk$set(highlight = TRUE)
opts_knit$set(...., highlight=FALSE)
render_html()
and when compiling, I have that error
Error in library(package = "parser", character.only = TRUE) :
there is no package called 'parser'
I use R 3.0
Thanks in advance

I think I have fixed this problem in the development version, and you can install it from:
install.packages('knitr', repos = 'http://www.rforge.net/', type = 'source')

Related

Trying to install the Performance Analytics in R

I'm trying to install the package 'Performance Analysis' in the R script below:
tryCatch(
expr = {
#library('PerformanceAnalytics', verbose = FALSE, quietly = TRUE)
library("PerformanceAnalytics",lib.loc=.libPaths(),verbose = FALSE, quietly = TRUE)
},
error = function(e){
print(e)
install.packages('PerformanceAnalytics', repos='https://cran.rstudio.com', verbose = FALSE)
#library('PerformanceAnalytics', verbose = FALSE, quietly = TRUE)
library("PerformanceAnalytics",lib.loc=.libPaths(),verbose = FALSE, quietly = TRUE)
}
)
But I'm having the following error message (Version 1.2.1335 of R Studio). Does anyone know if I've made a mistake somewhere?
Thanks
unable to access index for repository https://cran.rstudio.com/bin/windows/contrib/3.1
Installing package into ‘C:/Users/xxxxx/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
Warning in install.packages :
unable to access index for repository https://cran.rstudio.com/bin/windows/contrib/3.1
Warning in install.packages :
package ‘PerformanceAnalytics’ is not available (as a binary package for R version 3.1.3)
Error in library("PerformanceAnalytics", lib.loc = .libPaths(), verbose = FALSE, :
there is no package called ‘PerformanceAnalytics’

I get Error: invalid version specification ‘0,2’ when I use the function dm_draw() in r

I want to use the function dm_draw() to visualize a dm object but when I ran the command I get the error message " Error: invalid version specification ‘0,2’". I've tried the code included in the vignette "Visualizing dm objects" (https://cran.r-project.org/web/packages/dm/vignettes/tech-dm-draw.html) and I get the same error message when I run the dm_draw() function.
library(dm)
library(dplyr)
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
dm_draw(flights_dm_w_many_keys)
I'm using dm version 0.2.7 and DiagrammeR 1.0.8. R version 4.1.2
I'm looking for a solution to visualize a dm object, it can be also different from dm_draw().
I hope someone can help me to get that done. Sorry for my broken English and thanks for your time, any type of help is appreciated.
You can use this code:
library(dm)
library(dplyr)
library(DiagrammeR)
library(DiagrammeRsvg)
# Use this function
dm_draw_svg = function(dm,...) {
if (!requireNamespace("DiagrammeRsvg", quietly = TRUE)) {
stop(
"Package \"DiagrammeRsvg\" must be installed to use this function.",
call. = FALSE
)
}
dm::dm_draw(dm = dm, ...) %>%
DiagrammeRsvg::export_svg() %>%
htmltools::HTML() %>%
htmltools::html_print()
}
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
# plot
dm_draw_svg(flights_dm_w_many_keys)
Output:

How to get R to read a gdb file?

I am trying to get R to read in a gdb file. First thing I did was to find out its layers, which I did by running:
ogrListLayers("my_data.gdb")
It turns to out my_data has two large layers. I have tried opening both but have had no success. Here is what I have tried so far:
1)
Wont_open <- readOGR(dsn = "D:/my_data.gdb", layer = "layer_1", dropNULLGeometries = F)
I have tried the above with and without the dropNULLGeometries argument and for both layers in my_data. When running this, I get the following error:
Error in readOGR(dsn = "D:/my_data.gdb", :
Unsupported field type: Binary
Wont_open <- st_read(dsn="D:/my_data.gdb", layer = "layer_1")
I have tried the above for both layers in my_data. When I run this, R simply stops working after about 1 hour of having started the process.
3)
read_GDB_Layer <- function(dsn, layerName, overwrite = T){
conversionDir <- tempdir()
gdalUtils:: ogr2ogr(src_datasource_name = dsn, dst_datasource_name = conversionDir, f = "ESRI Shapefile", layer + layerName, verbose = T, overwrite = overwrite)
df <- read.dbf(file.path(conversionDir, paste0(layerName, ".gdbtable")))
return(df)}
Then,
Wont_open <- read_GDB_Layer(dsn = "D:/my_data.gdb", layerName = "layer_1")
I tried this for both layers and changed the .gdbtable argument of the function for .dbf to run it on both layers and it still did not work. I got the following warning messages:
1: In gdal_setInstallation(search_path = NULL, rescan = FALSE, ignore.full_scan = TRUE, :
No GDAL installation found. Please install 'gdal' before continuing:
- www.gdal.org (no HDF4 support!)
- trac.osgeo.org/osgeo4w/ (with HDF4 support RECOMMENDED)
- www.fwtools.maptools.org (with HDF4 support)
2: In gdal_setInstallation(search_path = NULL, rescan = FALSE, ignore.full_scan = TRUE, :
If you think GDAL is installed, please run:
gdal_setInstallation(ignore.full_scan=FALSE)
The st_read() function worked for me, as pointed by #sven-brandt

Cannot Install Plotly in R

Attempting to install plotly in R studio 3.6.1 on a work computer.
Running into this error.
Error in if (any(diff)) { : missing value where TRUE/FALSE needed
Traceback gives
4: tools::checkMD5sums(pkgname, file.path(tmpDir, pkgname))
3: unpackPkgZip(foundpkgs[okp, 2L], foundpkgs[okp, 1L], lib, libs_only,
lock)
2: .install.winbinary(pkgs = bins, lib = lib, contriburl = contrib.url(repos,
type2), method = method, available = av2, destdir = destdir,
dependencies = NULL, libs_only = libs_only, quiet = quiet,
...)
1: install.packages("plotly")
Is the checksum not passing? Why would this be?
Thanks

Issue when loading LDA function in R

I'm using the text-mining tm library for R.
I'm running on R version 3.3.1
I have this code:
lda <- LDA(docterm,k = 3,method = 'Gibbs')
lda.topics <- as.matrix(topics(lda))
lda.terms <- as.matrix(terms(lda,5))
topic.terms <- c()
topic.terms[1] <- paste(c(lda.terms[,1],'\n'),collapse = '\n')
topic.terms[2] <- paste(c(lda.terms[,2],'\n'),collapse = '\n')
topic.terms[3] <- paste(c(lda.terms[,3],'\n'),collapse = '\n')
tw.df <- tw.df %>%
mutate(topico = topic.terms[lda.topics])
But every time I try to run it with source file.r
It throws me this:
Error in eval(expr, envir, enclos) : could not find function "LDA"
I don't get it, the tm package is installed.
Has anybody encountered this kind of behaviour before?
Any ideas on how to solve it?
Thanks in advance!
Try installing and using the package 'topicmodels'.
install.packages('topicmodels')
It should work.

Resources