Not able to run selected lines in REPL R in sublime text - r

Followed these instructions to set up REPL for sublime text
http://www.kevjohnson.org/using-r-in-sublime-text-3/
R console is running. But I am unable to push text to console using the shortcuts
Ctrl+Shift+,,l
I must be doing something wrong here, not able to figure it out on my own. Any help appreciated.
I get the following error:
Cannot find REPL for 'regexp'
Edit: Adding sample code
library("e1071")
data(iris)
m <- naiveBayes(Species ~ ., data = iris)
m
table(predict(m, iris), iris[,5])

REPL checks scopes to know what console to run, and returns an error if no console is associated to a language. It might have been that your file was not in the R syntax.
To change the syntax : Command Panl (ctrl+shift+p), Set Syntax: R

Related

How to stop R from stopping execution on error?

I'm having an issue where if I execute several lines of code at once and one of them has an error, the lines below don't get executed.
For example if I have:
table(data$AGE)
table(dataREGION)
table(date$SEXE)
I get the table for the first line, and then
Error in table(dataREGION) : object 'dataREGION' not found
>
And the last line does not execute.
Does anyone know why it does that and how to fix it?
(I work with R 4.2.2 and RStudio 2022.12.0+353 "Elsbeth Geranium" Release)
Thanks!
Have a nice day,
Cassandra
Fixed: In Global Options > Console, under "Execution" uncheck the "Discard pending console input on error"
It seems like you want to use try().
try(table(data$AGE), silent = F, outFile = T)
try(table(dataREGION)) # also works without any params
try(table(date$SEXE))
You can also use tryCatch() if you want more control but it doesn't seem necessary for your purpose.
__
As for why your dataREGION doesn't exectue:
Hazarding a guess it might be because you forgot the $ between data and REGION

Issue trying to Knit Rmd File to Word when RWeka is Used

I'm trying to Knit an Rmd file to Word, and keep encountering the following error message:
Error Message from RStudio
My line 28 is as follows:
NN <- make_Weka_filter("weka/filters/unsupervised/attribute/NumericToNominal")
I use NN later to apply the filter function to my testing and training data (I'm attempting a classification model for the Federalist papers)
trainfed <- NN(data=trainfed, control= Weka_control(R="1-3"), na.action = NULL)
testfed <- NN(data=testfed, control= Weka_control(R="1,3"), na.action = NULL)
When I run all of this either in blocks in my instance of RStudio, or just line by line - I have no issues. I can view the output in window, in line, and even in the viewer pane.
However, as soon as I start to try to Knit to Word I get the error mentioned above.
Is it possible to Knit to Word with RWeka package in use? I've seen many of these filed, but never really one with an answer to the error in finding the function on whatever instance Rmarkdown Knits from...
Can anybody advise? Thank you!

collect output of system rscript command as r object

I am using RStudio in Windows to develop and run a pipeline for multivariate analysis that involve big dataset (90 by ~ 60000 matrices). With matrices of such a size, I got "protection from stack overflow" pretty often. One way of avoid this problem, while still using RStudio as opposite to the regular Rgui, is to run my script(s) using the following syntax
system("Rscript --max-ppsize=500000, my_script.s").
However, running this command results in the script running succesfully, but I cannot get the desired output. If I run the previous command with the following option
opt-< system("Rscript --max-ppsize=500000, my_script.s", internal = TRUE)
I got the standard output to the terminal as output (as a character vector), but not the desired output.
Consider this toy examples:
save the following code in my_script.R
print("first call")
rnorm(15)
print("second call")
rnorm(20)
and run the following code from the console
a <- system("Rscript my_script.R", intern = TRUE)
a
As you can see, the output is a character vector of length 9 with the standard output to the console.
If you modify my_script.R as follow
print("first call")
i_want_this <- rnorm(20)
and then run it again
a <- system("Rscript my_script.R", intern = TRUE)
a
now the only thing stored in a is the output of the print command.
My question is: is there a way to collect the i_want_this variable as an r object (in this case a numeric vector of length 20) ?
A similar question has been asked here, without a satisfying answer.

Error in R. Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :

I am encountering an error in R that I cannot seem to figure out. I am creating an R markdown document where I read in an a csv table using this code.
iati <- read.csv(file="/filepath/IATI_NGOS.csv",head=TRUE,sep=",")
and then using ggplot2 I create a plot using the following code.
figure_one <- ggplot(iati, aes(iati$reporting.org))+
geom_bar(fill="blue")+
ylab("Total Activities")+
xlab("NGO Reporting Organizations in IATI")+
ggtitle("Total Number of Activities compared to each NGO Reporting Organization in IATI")+
coord_flip()
When I try to call figure_one in the R markdown I get the following error:
Quitting from lines 44-55 (NGO_IATI.Rmd)
Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :
input string 1 is invalid UTF-8
Calls: <Anonymous> ... paste -> comment_out -> line_prompt -> paste -> gsub
In addition: Warning message:
In grep("\n", message) : input string 1 is invalid in this locale
Execution halted
When I run this code in a regular R script I have absolutely no issues. I have search for some answers but can't figure it out.
Thanks!
I ended solving my issue by just doing a fresh install of R and Rstudio on my local machine. I think the recent update to Yosemite on my local environment created a lot of issues with the TeX plugin I had installed for R markdown.
I get the same question when I knit my rmarkdown document and find **encoding is the cause.
When you use functions like read.csv, fread or read_csv, you will read the column name.
If column names are in other languages, like Chinese, the problem will easily happen.
Or you rmarkdown works on Windows, but the encoding bug happens on Mac, a different environment.
The temporal solution is to rename the column name in English and resave the data files.
Here is the pseudocode in R to show my idea.
library(data.table)
library(tidyverse)
fread('yourfile.csv',encoding = 'UTF-8') %>%
purrr::set_names(c('x1','x2','x3')) %>%
write_excel_csv('yourfile_2.csv')
Here the new file yourfile_2.csv is fine to rmarkdown knit without encoding problems happening.

Error when running (working) R script from command prompt

I am trying to run an R script from the Windows command prompt (the reason is that later on I would like to run the script by using VBA).
After having set up the R environment variable (see the end of the post), the following lines of code saved in R_code.R work perfectly:
library('xlsx')
x <- cbind(rnorm(10),rnorm(10))
write.xlsx(x, 'C:/Temp/output.xlsx')
(in order to run the script and get the resulting xlsx output, I simply type the following command in the Windows command prompt: Rscript C:\Temp\R_code.R).
Now, given that this "toy example" is working as expected, I tried to move to my main goal, which is indeed very similar (to run a simple R script from the command line), but for some reason I cannot succeed.
Again I have to use a specific R package (-copula-, used to sample some correlated random variables) and export the R output into a csv file.
The following script (R_code2.R) works perfectly in R:
library('copula')
par_1 <- list(mean=0, sd=1)
par_2 <- list(mean=0, sd=1)
myCop.norm <- ellipCopula(family='normal', dim=2, dispstr='un', param=c(0.2))
myMvd <- mvdc(myCop.norm,margins=c('norm','norm'),paramMargins=list(par_1,par_2))
x <- rMvdc(10, myMvd)
write.table(x, 'C:/Temp/R_output.csv', row.names=FALSE, col.names=FALSE, sep=',')
Unfortunately, when I try to run the same script from the command prompt as before (Rscript C:\Temp\R_code2.R) I get the following error:
Error in FUN(c("norm", "norm"))[[1L]], ...) :
cannot find the function "existsFunction"
Calls: mvdc -> mvdcCheckM -> mvd.has.marF -> vapply -> FUN
Do you have any idea idea on how to proceed to fix the problem?
Any help is highly appreciated, S.
Setting up the R environment variable (Windows)
For those of you that want to replicate the code, in order to set up the environment variable you have to:
Right click on Computer -> Properties -> Advanced System Settings -> Environment variables
Double click on 'PATH' and add ; followed by the path to your Rscript.exe folder. In my case it is ;C:\Program Files\R\R-3.1.1\bin\x64.
This is a tricky one that has bitten me before. According to the documentation (?Rscript),
Rscript omits the methods package as it takes about 60% of the startup time.
So your better solution IMHO is to add library(methods) near the top of your script.
For those interested, I solved the problem by simply typing the following in the command prompt:
R CMD BATCH C:\Temp\R_code2.R
It is still not clear to me why the previous command does not work. Anyway, once again searching into the R documentation (see here) proves to be an excellent choice!

Resources