RShiny: Pass in a FilePath - r

Trying to do something pretty simple here.. I just want to hard code a file path and pass it into "read.csv" but on run it's removing the "\t" from the string.
filePath = toString("C:\test.csv")
scenario_data <<- read.csv(filePath, stringsAsFactors=F)
Error:
Warning in file(file, "rt") :
cannot open file 'C: est.csv': Invalid argument
Warning: Error in file: cannot open the connection
1: runApp
I know in Python I can use r"this is a string" so that this doesn't happen. Is there something similar in RShiny?

Related

Import Excel data into R using openxlsx: Error in file(con, "r") : invalid 'description' argument

I am trying to import the data in my Excel file into R using Openxlsx library:
library(openxlsx)
data <- read.xlsx("datafile.xlsx", sheet = "Sheet1")
However, I get the following error:
Error in file(con, "r") : invalid 'description' argument
In addition: Warning message:
In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file
This error is thrown because your Excel file is open.
Save and close the Excel file and try again, it will work.
There's also another possibility: the XLSX file could be password protected. If you delete the password, then this can fix the error.
I think the best way to solve this problem is to reset the pathway of your data source. Please do not include any characters without English in your pathway.
setwd("C:\\Users\\your path way (where you store datafile.xlsx)")
P.S.
Rstudio2021 seem not friendly to non-English user ☺☺☺

How do I use strings in functions for R ? (file,rt error)

I am trying to load data into a function to use for analysis later on and there seems to be issues inserting a string (i.e. a filename) into my function. Here is what I am working with.
hist_sep<- function(dex_file,etoh_file,sep_parameter) {
dex<-read.csv("dex_file")$sep_parameter
etoh<-read.csv("etoh_file")$sep_parameter
}
The code below outputs this error
hist_sep(RT_3h_Amp_A.csv , RT_3h_Amp_EtOH_A.csv , FL1.A)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
However, if I input the filenames myself (as below) then everything works great. So everything is in the correct directory.
dex<-read.csv("RT_3h_Amp_A.csv")$FL1.A
etoh<-read.csv("RT_3h_Amp_EtOH_A.csv")$FL1.A
Any ideas how I could get around this problem?
Remove the quotes around the filenames in your function (e.g., should be dex<-read.csv(dex_file)$sep_parameter, not dex<-read.csv("dex_file")$sep_parameter). Otherwise, it's trying to download a file CALLED "dex_file".

Shiny validate for read.csv

I am trying to use shiny validate function to capture reading errors and show customized error message when reading uploaded csv files instead of letting shiny forward the default read.csv error message.
Here is the simple code
validate(need(try(sd <- read.csv(file = sdFile[1], stringsAsFactors = FALSE)), "Error reading the file"))
when there is no format issue in the csv file, the code works normally. But when there is an issue with the csv file, the code still returns the default error message (in red font), eg., Error: undefined columns selected, but not the customized message. Any issue here? Thanks!
I think it is actually printing it out, if I do this:
library(shiny)
validate(need(try(sd <- read.csv(file = "mtcars1.csv",
stringsAsFactors = FALSE)),
Error reading the file !!!"))
yielding:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'mtcars1.csv': No such file or directory
Error: Error reading the file !!!
I get this - note your message is the in the last line.
You can suppress the warnings with supressWarnings like this:
library(shiny)
suppressWarnings(
+ validate(need(try(sd <- read.csv(file = "mtcars1.csv",
stringsAsFactors = FALSE)),
"Error reading the file !!!!")))
yielding:
Error in file(file, "rt") : cannot open the connection
Error: Error reading the file !!!!
Or you can supress everything but your message with this (uses tryCatch instead of try):
library(shiny)
suppressWarnings(
validate(need(tryCatch(sd <- read.csv(file = "mtcars1.csv",
stringsAsFactors = FALSE), error=function (e){}),
"Error reading the file !!!!")))
yielding
Error: Error reading the file !!!

error loading csv file for R

when i loading csv file for R, i can see the error
but i don't know why this happening
i wrote following code:
setwd("C:\\Users\\규남\\Desktop\\twitter")
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))
and, this error appear
txt <- readLines(file("test.csv"))
Error in readLines(file("test.csv")) : cannot open the connection
In addition: Warning message:
In readLines(file("test.csv")) :
cannot open file 'test.csv': No such file or directory
why this happening?
file directory is not wrong, and that file in the folder
[enter image description here][1]
please see this
i restart Rstudio, even notebook power
but error appear again
how to i load that csv file?
and why this happening?
here is result useing getwd() function
[1] "C:/Users/규남/Desktop/twitter"
Warning message:
closing unused connection 3 (test.csv)
[1]: http://i.stack.imgur.com/xkFkt.png
When working through these problems I like to use the file.path() function. Look at the documentation, but it makes certain that the separator characters that are used in the string are what R is expecting.
Try:
path <- file.path("C:", "Users", "규남", "Desktop", "twitter")
setwd(path)
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))

"Error in file" instead of "Error in eval" when specifying output file in knit2html

I am making several html reports from one Rmd file called "template.Rmd".
When using the function knit2html(), there is a misleading error message, "cannot open the connection", while in fact the error is due to a code mistake, such as a missing variable. Let me illustrate by a little example below:
Template.Rmd contains this inline R code:
`r missing_variable`
When I don't specify the output file, I get a useful error message
> knit2html("docs/template.Rmd")
Quitting from lines 2-4 (docs/template.Rmd)
Error in eval(expr, envir, enclos) : object 'missing_variable' not found
When I specify the output file, I get a misleading error message
> knit2html("docs/template.Rmd", "docs/template.html")
Warning in file(file, ifelse(append, "a", "w")) :
cannot open file 'docs/template.html': No such file or directory
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Is this a knitr related problem?
I cannot reproduce your problem with the most recent versions of R and knitr on CRAN, but in most cases, you do not need to specify the output argument, and it is strongly not recommended to use an output path that contains a directory name, such as docs/template.html (see the Note section in ?knit). In your case, just switch the working directory to docs and run knit2html(), e.g.
setwd('docs')
knit2html('template.Rmd') # generates template.html

Resources