Using RStudio on our Linux server, I am attempting to display a file dialog in R that allows the user to see hidden files (files that begin with a period {.*} that are visible with ls -a). I have tried the following file.choose() and rstudioapi::selectFile() with different options, but the hidden files remain hidden in the file dialog in each case:
myHiddenFile1a <- file.choose()
myHiddenFile1b <- file.choose(new = TRUE)
myHiddenFile2a <- rstudioapi::selectFile()
myHiddenFile2b <- rstudioapi::selectFile(existing = FALSE)
myHiddenFile2c <- rstudioapi::selectFile(filter = ".*")
Related
I'm fetching data from a server using an API, my R-script has 4 code sections.
Section I
new_export_url<-KoboconnectR::kobo_export_create(url=serverURL, uname=username, pwd=password,
assetid=id, type= "csv", all="false", lang="_default",
hierarchy="false", include_grp="true",grp_sep="/")
Section II
Raw.CEP <- httr::GET(new_export_url, httr::authenticate(user =username, password =password))
Raw.Data <- read.csv(text = httr::content(Raw.CEP, type = "text", encoding = "UTF-8"), sep = ";")
When I run the code separately, i.e. Section I first then Section II later, the data successfully downloads to the RStudio workspace. However, When I click the button run to run the whole script at once, the data download times out (?) and I get the following error in the console.
What could be the issue?
I was wondering if it is possible to add a link of a local directory or file to rmarkdown. My goal is to have a png in each page, and a clickable link on some of the pages that take me to the file that plots are generated from (special file, so no csv, or tables).I don't want to open the file, but just open the folder that has that file, so user can easily find it and open it with appropriate software.
Something like this:
{r, results='asis'}
#Generate some plots
for (i1 in 1:10)
{
png(paste0("~/Downloads/tmp-png/",i1,".png"), width=800, height=800)
par(mfrow=c(3,3))
for (j1 in 1:9)
plot(1:40)
dev.off()
}
some.list <- c("2.png","5.png","7.png")
files <- list.files(path = "~/Downloads/tmp-png", pattern = "png",
full.names = TRUE)
for (f in files) {
cat(paste0("![image_label](", f, ")\n"))
if ( f %in% some.list)
#Add a local hyperlink to the folder
#
}
You didn't say what the link should be, but you should be able to generate one using something like your image inclusion. E.g. if the link you want for file f is to folder temp/foo.wjx, then just include
folder <- "temp/foo.wjx"
cat(paste0("[folder_label](", folder, ")\n"))
after the if.
This needs to be a folder on the web server with a path relative to the path to the PDF file making the link. Whether the PDF viewer follows the link probably depends on which viewer you are using.
The folder_label could be an image link instead of a text label.
I am working on a project which is developed by our team. We share the codes in a repository. Every team member is using his/her own machine with his/her own working directories. That is why we use relative paths in our projects. Usually we use something like
setwd("MyUser/MyProject/MyWD/myCodesDir") # local
...
MyReportingPath <- "../ReportsDir" # in repository
Now I try to render a markdown report to this directory:
rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
output_file = paste0(MyReportingPath, "/ReportingHTML.html"))
This doesn't work. It only works if I type in the full path of the output file ("/home/User/..../ReportingHTML.html")
This is one of the issues I would like to clarify: is there any possibility to use relative paths in any way for Markdown?
Second issue is that if I type in an non-existing directory in the output_file, pandoc throws me an error instead of creating this directory with my output file. Is there any possibility to do a dynamic output directory creation? (except for doing system(paste0("mkdir ", reportPath), intern = T) before rendering)
P.S. It is important for me to render the markdown document in a separate R function, where I create the whole environment which is inherited by my Markdown document.
Trivial issue - since you're using paste0 you need to provide the / delimiter between your output directory and output file.
You wrote:
rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
output_file = paste0(MyReportingPath, "ReportingHTML.html"))
Instead, try:
rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
output_file = paste0(MyReportingPath, "/", "ReportingHTML.html"))
More broadly:
For your first issue (settting the path for the input file) - I also suggest using here::here(). If you need to navigate up from your working directory you can break down the path as follows:
parent_dir <- paste(head(unlist(strsplit(here::here(), "/", fixed = TRUE)), -1), collapse = "/")
grandparent_dir <- paste(head(unlist(strsplit(here::here(), "/", fixed = TRUE)), -2), collapse = "/")
However - it might be easier to set the working directory to a higher level, then build up your code and results directories, for example:
project_dir <- here::here()
codefile <- paste(project_dir, "code", "myreport.Rmd", sep = "/")
outfile <- paste(project_dir, "results", "myreport.html", sep = "/")
rmarkdown::render(input = codefile,
output_file = outfile))
For your second issue (creating the directory for output) - using dir.create("MyReportingPath", recursive = TRUE) will create the output directory and any intermediate levels. You will get a warning if the directory exists which can be suppressed using showWarnings = FALSE.
I just ran into this myself, and as it turns out, there's one additional complication here: You really do need to use an absolute path for the output if your input file being rendered isn't in your current working directory. You also need to use absolute paths for anything else during rendering, for example images like ![](path.png).
This looks to be because rmarkdown::render temporarily sets your working directory to the directory containing the input file, so it interprets a relative directory as relative to that path, not your initial working directory. (EDIT: There's currently an open issue for this on github.)
For example if you have this setup:
subdir/test.Rmd
outdir
And you do this:
rmarkdown::render(
input = "subdir/test.Rmd",
output_file = "outdir/out.html")
You get the error:
Error: The directory 'outdir' does not not exist.
Execution halted
Instead, you could do:
output_path <- file.path(normalizePath("."), "outdir/out.html")
rmarkdown::render(
input = "subdir/test.Rmd",
output_file = output_path)
...and that should work.
You might think you could just use normalizePath("outdir/out.html"), but you can't, because that function only works when the path already exists. You also might think you could do this:
rmarkdown::render(
input = "subdir/test.Rmd",
output_file = file.path(normalizePath("."), "outdir/out.html"))
but you can't, because R only gets around to interpreting the value of output_file once the working directory has already been changed.
I have the following data frame which can be downloaded from here. The column image_path has jpg files in base64 format. I want to extract the image and store it in a local folder. I tried using the code given here and here.
While the second one perfectly opens the image in the browser, I couldn't figure out how to save the file locally. I tried the following code:
library(shiny)
for (i in 1:length(df)){
file <- paste(df$id[i])
png(paste0(~images/file, '.png'))
tags$img(src = df$image_path[i])
dev.off()
}
The following just runs but doesn't create any image files and no errors are shown. When I tried running tags$img(src = df$image_path[1]) to see if it generates the image, it doesn't. I understand tags$img is a function within shiny and works when I pass it inside ui (as suggested by #daatali), but not sure how do I save the files locally.
What I want is to run a for loop from inside a server environment of shiny and save the images locally as jpg using id numbers as filename, which can be rendered with various other details captured in the survey.
I have never worked with images and please bear with me if this is completely novice.
This creates your images from the base64 strings and saves the files to your current working directory, subfolder "/images/". This article describes pretty well how to save files locally in Shiny.
library(shiny)
library(base64enc)
filepath <- "images/"
dir.create(file.path(filepath), showWarnings = FALSE)
df <- read.csv("imagefiletest.csv", header=T, stringsAsFactors = F)
for (i in 1:nrow(df)){
if(df[i,"image_path"] == "NULL"){
next
}
testObj <- strsplit(df[i,"image_path"],",")[[1]][2]
inconn <- testObj
outconn <- file(paste0(filepath,"image_id",df[i,"id"],".png"),"wb")
base64decode(what=inconn, output=outconn)
close(outconn)
}
Is there a way to make R detect the path of an input file in R script when running in R studio, automatically?
I have the following code
input.data <- read.xlsx("C:/Users/haha/Desktop/haha/input.xlsx", "input", header=F, rowIndex=NULL, startRow=1, endRow=21, colIndex=c(1:2))
If i were to share the script containing this code, the user will have to change the directory path for the input file before running the code.
I'd like to figure out a way to do this automatically such that the user is able to run the script without needing to change the directory's path.
You can get the directory of the script (basedir) automatically in this way and then use the directory to access the input file:
args <- commandArgs(trailingOnly = FALSE)
basedir <- dirname(sub("--file=", "", args[grep("--file=", args)]))
input.data <- read.xlsx(paste0(basedir, "input.xlsx"), "input", header=F, rowIndex=NULL, startRow=1, endRow=21, colIndex=c(1:2)))
You could use file.choose() to prompt the user to navigate the file.
It would look like this input.data <- read.xlsx(file.choose(), "input", header=F, rowIndex=NULL, startRow=1, endRow=21, colIndex=c(1:2))