export output into text file and open in automatically in r? - r

I export my output file into a text file. Using two type of function.
sink()
write.table()
My list content is exported using sink() and data.frame content is exported using write.table().
Is it possible to open the text file automatically after created?
please give example .
I create text file into two way..
write.table(x, file ="F:\\frequent itemset.txt",row.names=FALSE,sep="=")
here x is data frame..And
sink("F:\\Large itemset.txt")
print(mylist)
sink()
print(mylist)
mylist is list data structure
Below code is used to open a data.frame in .csv file...
Is there any simpler way other than this?
myView <- function(dframe) {
# RStudio does not have a good viewer for large data frames. This
# function writes a dataframe to a temporary .csv and then opens it,
# presumably in excel (if that is the file association).
csvName <- paste0(tempdir(), "\\myView-", substitute(dframe),
format(Sys.time(), "%H%M%S"), ".csv")
write.csv(dframe, file = csvName)
shell.exec(csvName)
}

This is how package RMark opens a notepad on windows every time you call print on mark object. On non-Windows OS, you have to use another editor, obviously.
system(paste(shQuote("notepad"), "test.txt", sep = " "))
EDIT
Here is a self contained example of how to make up mock data, save it to a table and open it using a notepad.exe.
mydf <- data.frame(x = runif(10), y = runif(10))
filename <- "test.csv"
write.table(mydf, file = filename, sep = ",", row.names = FALSE)
system(paste(shQuote("notepad"), filename, sep = " "), wait = FALSE, invisible = FALSE)

If your question in 'only' about viewing data frames, you may have a look at the gvisTable function in package googleVis.
"The gvisTable function reads a data.frame and creates text output referring to the Google Visualisation API, which can be included into a web page, or as a stand-alone page. The actual chart is rendered by the web browser."
There are loads of nice tutorials on googleVis, e.g. the vignette. Here is just a very simple example.
library(googleVis)
gt <- gvisTable(iris)
plot(gt)
gt <- gvisTable(iris, options = list(page = 'enable', height = 300))
plot(gt)

Related

An R Loop to wrap text in multiple saved excel files

I have hundreds of excel files with a single column and a single sheet containing text. I am trying to write a loop that will 'Wrap Text' and align the single column in all of the files, preferably without reading the files into R.
I already set the style object as follows:
style <-
openxlsx::createStyle(
halign = "left",
valign = "center",
wrapText = T
)
I have tried both a for loop and lapply but both only performs the openxlsx::addStyle to one file out of the 100s. Doesn't have to be openxlsx, it can be xlConnect or or any other package for xlsx files, even VBA is welcomed, if I can call it from R.
Please help.
Thanks in advance.
This will probably be pretty slow and will most likely require reading the files into R, so I'm not sure how much this helps 😅.
Libraries
library(openxlsx)
Find files
First you need a list of all the excel files you have:
xlsx_paths <- list.files(path = "./folder_with_yr_excels", pattern = "xlsx$")
This will create a vector of all the .xlsx files you have in the folder.
Write function
Then we can write a function to do what you want to a single file:
text_wrapper <- function(xlsx_path){
#this links the file to R using the openxlsx package
n3 <- openxlsx::loadWorkbook(file = xlsx_path)
# this creates the style that you wanted:
style <-
openxlsx::createStyle(
halign = "left",
valign = "center",
wrapText = TRUE
)
# this adds the style to the excel file we just linked with R
openxlsx::addStyle(n3, sheet = 1, cols = 1:400, rows= 1:400, style, gridExpand = TRUE)
#this removes the .xlsx part from the path name
xlsx_path2 <- sub(pattern = ".xlsx",
replacement = "",
x= xlsx_path)
# This is the naming standard I'll use:
#"original_file_name" -> "original_file_name_reformatted.xlsx"
new_path <- paste(xlsx_path2, "_reformatted", ".xlsx", sep = "")
# this saves the reformated excel file
saveWorkbook(n3, file = new_path, overwrite = TRUE)
}
Notes
For other people coming across this post, here's a more in depth description of the openxlsx R package and some of the formatting things that can be done with it: https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf
An annoying thing about this package is that you have to specify how many rows and columns you want to apply the style to, which become annoying when you don't know how many rows and columns you have. The not great workaround is to specify a large number of columns (in this case I did 400)
openxlsx::addStyle(n3, sheet = 1, cols = 1:400, rows= 1:400, style, gridExpand = TRUE)
As of the time of posting, it sounds like there's not a better solution: https://github.com/awalker89/openxlsx/issues/439
Apply function to files
Anyways, the final step is to apply the function we wrote to all the excel files we found.
lapply(paste("./folder_with_yr_excels",xlsx_paths,sep = ""), text_wrapper)
Since that was done inside of a function we don't have to go back and delete intermediate data file. Yay!
Notes
The paste("./folder_with_yr_excels",xlsx_paths,sep = "") step adds the folder name back to the path name. There's an option in list.files() to keep the whole file path intact, but I like to keep track of which folder I'm dealing with by pasting the folder name back on at the end.

how to modify multiple cells in a loop in R without losing formatting?

Disclaimer: R noob here!
On a high level, I am trying to convert pdf to xls ;) The pdf is well formatted, no surprises expected. At one point I am trying to modify multiple cells using xlsx package in a loop. I've got a variable list of 3 - 5 elements and want to change the content of 7th column in .xls file, starting with 14th row. The list is coming from a PDF file (src.pdf below).
Here's the code:
library(xlsx)
library(pdftools)
library(stringr)
library(tabulizer)
library(tidyverse)
# for example data, separately download src.xls from https://file-examples-com.github.io/uploads/2017/02/file_example_XLS_100.xls, i. e. using wget -O src.xls https://file-examples-com.github.io/uploads/2017/02/file_example_XLS_100.xls
src <- xlsx::loadWorkbook(file = "src.xls")
sheets <- getSheets(src)
rows <- getRows(sheets$List1)
cc <- getCells(rows)
pdf_path <- "src.pdf"
# dest <- extract_tables("src.pdf", output="data.frame", area = list(c(163, 315, 217, 459)), guess = FALSE, header = FALSE)
dest <- extract_tables("https://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf", output="data.frame", area=list(c(195,103,376,515)), guess = FALSE, header = FALSE)
#use as
#dest[[c(1,1)]][1]
#dest[[c(1,1)]][2]
#...
row = 0
for (i in 1:length(dest[[1]]$V1))
{
row = i+13
setCellValue(paste0("cc$`",row,".7`"), value = dest[[c(1,1)]][i])
}
This returns
Error in .jcall(cell, "V", "setCellValue", value) :
RcallMethod: cannot determine object class
Any ideas how to use setCellValue in a loop? I am open to using different modules as well, as long as they keep the formatting of the source .xls.
Thank you!

how can I import several .txt files into r using loops for text analysis

I'm working on a bibliometric analysis in r that requires me to work with data obtained from the Web of Science database. I import my data into r using the following code:
file1 <- "data1.txt"
data1 <- convert2df(file = file1, dbsource = "isi", format = "plaintext")
I have about 35 text files that I need to repeat this code for. I would like to do so using loops, something I don't have much experience with. I tried something like this but it did not work:
list_of_items <- c("file1", "file2")
dataset <- vector(mode = "numeric", length = length(list_of_items))
for (i in list_of_items){
dataset[i] <- convert2df(file = list_of_items[i], dbsource = "isi", format = "plaintext")
print(dataset)}
I get the following error:
Error in file(con, "r") : invalid 'description' argument
I'm not very familiar with using loops but I need to finish this work. Any help would be appreciated!
R wants to open file1, but you only have file1.txt. The filenames in the list are incorrect.
I once had that problem as well, maybe the solution works for you too.
maybe put all text files in a folder and read the folder, this might be easier.
FOLDER_PATH <- #your path here just paste it from the explorer bar (Windows). Beware to replace`\` with `\\`
file_paths <- list.files(path = FOLDER_PATH,
full.names = TRUE # so you do not change working dir
) # please put only txt in the folder or read the doc on list.files
# using lapply you do not need a for loop, but this is optional
dataset <- lapply(file_paths, convert2df, dbsource = "isi", format = "plaintext")

Is there an R script that write rmd files with the content from excel spreadsheets?

Solution in comments!
I'd like to have a template for the rmd file and have the template filled in with content, both R code chunks and regular text, according to the content of the spreadsheet.
Content to be used in code chunk
Text to be pasted after code chunk
Parametrized code 1
Description of event 1
Parametrized code 2
Description of event 2
Rmd output:
"""{r, echo = FALSE, comment = NA}
set.seed(rand_seed)
parametrized code 1
"""
Description of event 1
Thanks for your help!
I don't know if such a tool is available as of yet. However, you may be interested in taking a look at this topic https://bookdown.org/yihui/rmarkdown/parameterized-reports.html, from the R Markdown Definitive Guide.
EDIT:
Well, I came up with a simple solution. I'm not sure of how big your needs are for this logic, but for only a handful of chunks, maybe this script could help you. I tested it and it works.
instructions_set <- data.frame(
code_chunks <- c(
"a <- 50; print(a)",
"hist(iris$Sepal.Length)"
),
text_chunks <- c(
"I've just set the variable a to 50 and printed it.",
"This is a histogram of the variable Species in the Iris dataset."
)
)
file <- apply(instructions_set, MARGIN = 1, function(x) {
x[1] <- paste0("```{r}\n", x[1], "\n```")
return(
paste(x[1], x[2], "", sep = "\n")
)
})
readr::write_file(purrr::reduce(file, paste0), "test_file.Rmd")
I've found a solution using the knitrdata package. You can use the create_chunk and insert_chunk functions to convert strings to code chunks for rmarkdown.
My next step would be to load the strings into a data frame and iterate these two functions over the content of the data frame to create and insert multiple code chunks to the rmd file.
For the text part of the Rmd file, I'd use the writeLines function.
Here's a sample of the code chunk creation code I've put together:
library(knitrdata)
library(svDialogs)
#choose the blank template Rmd.file
input_file <- dlg_open(message = "Please select the blank Rmd file to use a template!" )$res
output_file <- paste(as.character(dlg_input( message = "Please name your output file!")$res), ".Rmd", sep = "")
#select line at which to insert the code chunk - I've chosen then last line of the file
insert_at <- length(readLines(input_file))
#takes string input and formats it as a chunk then returns it as a character vector
input_chunk <- create_chunk(text = "print('this is a test')", chunk_label = "Testing label creation", chunk_type = "r")
#inserts properly formatted chunk at the specified line of lines read from the target Rmd file
rmd_text <- insert_chunk(input_chunk, line = insert_at , rmd.file = input_file )
#since insert_chunk returns a character vector, this line writes it to a new Rmd file
writeLines(rmd_text,output_file)

decode base64 to a raster

it is a rather untypical scenario, I am using R Custom visual in PowerBI to plot a raster and the only way to pass data is by using a dataframe.
this what I have done so far,
generate a raster in R
save it to file using SaveRDS
encoded the file as a base64 and save it as a csv.
now using this code I manage to read the csv, load it to a dataframe combine al the rows
my question is how to decode it back to a raster Object ?
here is a reproducible example
# Input load. Please do not change #
`dataset` = read.csv('https://raw.githubusercontent.com/djouallah/keplergl/master/raster.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
library(caTools)
library(readr)
dataset$Value <- as.character(dataset$Value)
dataset <- dataset[order(dataset$Index),]
z <- paste(dataset$Value)
Raster <- base64decode(z,"raw")
here is the result
it turned out the solution is very easy, saveRDS has an option to save with ascii = TRUE
saveRDS(background,'test.rds',ascii = TRUE,compress = FALSE)
now I just read it as humain readbale format (which is easy to load to PowerBI) and it works
fil <- 'https://raw.githubusercontent.com/djouallah/keplergl/master/test.rds'
cony <- gzcon(url(fil))
XXX <- readRDS(cony,refhook = NULL)
plotRGB(XXX)

Resources