R Shiny: 'error cannot open the connection' - r

so I have a Shiny app where I'm trying to read in a user-identified input file.
Towards that end, my ui.R has the line fileInput("predictor2", label = "Predictor Values") and I try to read the file using the line predictor <- read.delim("input$predictor2") in my server.R file.
However, I get a message saying Error: Cannot open the connection. If I don't try to read in the file and use another matrix of values, the code works fine. Any advice for how to fix this problem or more detail that would be useful?

You code is looking for a file with the literal name input$predictor2 which presumably does not exist. You first need to remove the quotes from around it, then add which column of the return actually has the path to the data, e.g.:
read.delim(input$predictor2$datapath)
See the help for fileInput for an example that checks to make sure something has been uploaded first.

Related

Debugging script, issue in reading in files at start

I am trying to debug a .R script, going through line by line to see where the bug is.
I am at the part where I input the datasets but am having trouble understanding what the code below is doing.
gtfile =grep("gt=",commandArgs())
if (length(gtfile)==0) { stop ("specify genotype file (gt=filename)\nRun script without arguments to see all options\n") }
gtfile=sub("gt=","", commandArgs()[gtfile])
Should I just be able to read in my gt file name below:
gt=read.table("~/clean.tab")
If I read the gt file in like above, I should then modify the mode to expect the gt object, like below?
gtfile =grep("gt",commandArgs())
if (length(gtfile)==0) { stop ("specify genotype file (gt=filename)\nRun script without arguments to see all options\n") }
gtfile=sub("gt","", commandArgs()[gtfile])
I get the following error, which makes me think it is not reading in the file gt as I hoped.
Error: specify genotype file (gt=filename)
Run script without arguments to see all options
Any advice on what the first block of code is doing would be appreciated.
I have modified the code block and read in the file.
I was expecting the gt file to be read in, then run the code block to grep the needed parts within the file.

R Markdown script and R

I am calling one R Markdown script from another R script.Below you can see command
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
The script is executed without any problem but is not open automatically.
So can anybody help me how to solve this problem and open this script automatically after running of this command ?
First, your syntax probably isn't doing what you intended. Writing
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
will create a new variable named file and use it as the first parameter to rmarkdown::render. A more likely way to get what you want is to write it as
outfile <- rmarkdown::render(paste(path1,"/Dashboard.Rmd",sep=""),
params=list(args = myarg))
This removes the assignment from the first argument, and saves the
result (which is the name of the file that was produced).
Now, on to your question: You need to follow that line with
rstudioapi::viewer(outfile)
to view it in RStudio, or
browseURL(outfile)
elsewhere, because rmarkdown::render doesn't automatically call a previewer.

Loading a .mat file that has dots in the variable name

I am trying to load a .mat file using the 'load' function. The file seems to load correctly, but some of the variables are not accessible. My best guess is that this is because the name of the variables (which was set by someone else in R and then exported to a .mat file) contains a dot (see pic)
As an example, one of the variables is a vector called 'final.product'. If I try to access the first element, I would get this
>>final.product(1)
Undefined variable "final" or class "final.product".
This works fine with the variables whose name do not contain any dots.
I have tried to modify the name from the Workspace (i.e. by clicking once on the name and removing the dot), but I am prompted with an error message saying
All the other things I have tried in the Command Window give me errors related to the fact that my variables don't exist or that the name is invalid, for example
>> T = from.product;
Undefined variable "from" or class "from.product".
Or
>>T = load('C:\Users\Loup\Downloads\5. Source-Intermediates-Products.mat', 'final.product');
T = T.('final.product');
Error using load
Invalid field name: 'final.product'.
So I guess I have two questions:
1) can someone confirm that my issue is caused by having dots in the variable names?
2) is there a way to rename a variable that does not require being able to read them?
Thanks a lot!
I did not manage to find a good answer to what I wanted to do. In case someone has the same issue, I ended up having to get the file in R and change the name there.
Cheers

Calling Skim from inside R

I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.

Unexpected input error in Shiny, but unable to locate the source of error

I am getting the unexpected input error in UI.R, as follows:
ERROR: C:\Users\myApp/ui.R:1:2: unexpected input
1: ï»
However, when I try to locate the error at line 1, there is absolutely nothing of the form ï».
To resolve this error, I tried saving my UI.R file as a text file and changing the encoding to UTF-8, but this still does not remove the strange character. I also tried removing the first couple of lines and re-writing the code, but it still gives the same error!
How can I remove this character? Should I use another text editor?
I am using base R, not R Studio. And I had copy-pasted my code form my GitHub account, if that info is required...
Code from my file can be viewed here.
Many thanks.
I have this same issue in the year '19, and took me a while to run into this question from the year '14.
Not Shiny, but a regular R project with its .Rprofile.
The solution that worked for me is:
Open your file in Notepad++. From the Encoding menu, select Convert to UTF-8 (without BOM), save the file, replace the old file with this new file. And all is fixed.

Resources