Why is R holding on to permission to modify excel files? - r

I am reading in some excel data into R in a loop because I have a bunch of files. This is something that I do regularly. I am having some trouble though and I'm not sure even how to make reproducible example. So i have all the files that I want in one directory and I am reading them in using this set of commands:
## Because I want warnings to stop the loop at a certain file so I can diagnose
options(warn=2)
xl_load_in <- c()
for (x in list.files(pattern="*\\.xls")) {
cat(x, "\n ")
u <- read_excel(x,col_types=c("text","text","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","text"))
xl_load_in <- tbl_df(rbind(xl_load_in, u))
}
options(warn=0)
So this is both a tool to read in files but also, using the warn toggle, I can diagnose which files aren't reading in correctly.
Now the problem: From a fresh reboot and I can rename and save the excel files in the directory. If, however, I run the above R code, I lose the permission to save or rename the excel file where the loop broke (There are small issues with the excel files that have to rectified manually). For example if I try to rename the file upon which the loop broke I get this windows error:
The action can't be completed because the file is open in RStudio R
Session.
If I reboot, I can again modify the file in question. But before the reboot I can't save or rename the file. Can anyone explain this behaviour where R or Rstudio seems to be taking over my permissions?
Update If I close RStudio, the permissions are returned. If I do the same sequence of commands above using RGUI for Windows, the problem in replicated indicating the issue is not an RStudio problem.
sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_1.1.0 readxl_0.1.1 lubridate_1.6.0 dplyr_0.5.0 purrr_0.2.2 readr_1.0.0 tidyr_0.6.0 tibble_1.2
[9] ggplot2_2.2.0 tidyverse_1.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 assertthat_0.1 R6_2.2.0 grid_3.3.2 plyr_1.8.4 DBI_0.5-1 gtable_0.2.0
[8] magrittr_1.5 scales_0.4.1 stringi_1.1.2 lazyeval_0.2.0 tools_3.3.2 munsell_0.4.3 colorspace_1.3-0

Related

Error: 'glmnet_softmax' is not an exported object from 'namespace:glmnet'

I am trying to use this tutorial https://github.com/wvictor14/planet#infer-ethnicity and get the following error message, even when using the test data provided:
pl_infer_ethnicity(pl_betas)
[1] "1860 of 1860 predictors present."
Loading required package: Matrix
Error: 'glmnet_softmax' is not an exported object from 'namespace:glmnet'
I've tried re-installing individual packages and running in a new version or R and get the same error. I believe this is related to other errors posted with a recent update in glmnet. Any tips on how to resolve?
sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Matrix_1.2-18 planet_0.1.0
loaded via a namespace (and not attached):
[1] lattice_0.20-38 codetools_0.2-16 glmnet_3.0-2 foreach_1.4.7
[5] crayon_1.3.4 grid_3.6.2 magrittr_1.5 pillar_1.4.2
[9] rlang_0.4.2 remotes_2.1.0 iterators_1.0.12 tools_3.6.2
[13] compiler_3.6.2 pkgconfig_2.0.3 shape_1.4.4 tibble_2.1.3
I don't know why it would have worked in the first place; the NEWS file for glmnet doesn't say anything one way or the other about glmnet_softmax (e.g., it does not say "glmnet_softmax is no longer exported" or anything like that ...)
In any case, this is a non-exported function from the glmnet package. It is referred to here in the tutorial code.
If you can change that line of code to refer to glmnet:::glmnet_softmax (i.e., three colons rather than two), that should suffice (::: allows you to access a non-exported function).

R looks in the wrong place for html dependency

I am trying to knit a flexdashboard to an html file. My flexdashboard includes leaflet maps. The knit.md file compiles correctly. At the last stage, converting the knit.md to an html file, I get an error. Apparently leaflet is looking for html dependencies in the R 3.3.2 folder, rather than the R 3.4.2 folder. I originally built this flexdashboard months ago on a different machine on which I had R 3.3.2 (although I was using 3.4.1 I believe to build this). So the problem appears to be that leaflet still looks in the old R-3.3.2 folder for its dependencies even though I've updated and reinstalled leaflet multiple times, trying to fix this problem.
Does anyone know how to tell leaflet to look in the correct place for its html dependencies?
output file: myflexdashboard.knit.md
Error: path for html_dependency not found: C:/R/R-3.3.2/library/leaflet/htmlwidgets/lib/leaflet-providers
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.2 backports_1.1.1 magrittr_1.5 rprojroot_1.2 htmltools_0.3.6
[6] tools_3.4.2 flexdashboard_0.5.1 yaml_2.1.15 Rcpp_0.12.14 stringi_1.1.6
[11] rmarkdown_1.8 knitr_1.17 jsonlite_1.5 stringr_1.2.0 digest_0.6.12
[16] packrat_0.4.8-1 evaluate_0.10.1

R Notebooks not evaluating chunks

I'm trying out the new R notebooks feature in Rstudio, but the chunks are not evaluating. When I preview the minimal example featured in rstudio (when you start a new file) the window says explicitly;
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
However there is no "run" button in the preview and the code does not evaluate. This is what I get basically (even if I place eval = TRUE in the chunk options).
I have been all over but I can still not get the chunks to evaluate. When using a normal rmarkdown document, it evaluates with no problem. Do I have to place a specific htmlwidget or something?
Here are my specs:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tse.sql_0.1.0 memisc_0.99.7-1 MASS_7.3-45 lattice_0.20-34 foreign_0.8-67 magrittr_1.5 ggplot2_2.2.0
[8] inegiR_1.2.0 dplyr_0.5.0 tse.utils_0.1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 knitr_1.14 munsell_0.4.3 colorspace_1.2-7 R6_2.2.0 stringr_1.1.0 plyr_1.8.4
[8] tools_3.3.2 grid_3.3.2 gtable_0.2.0 DBI_0.5-1 htmltools_0.3.5 lazyeval_0.2.0 yaml_2.1.13
[15] assertthat_0.1 digest_0.6.10 tibble_1.2 formatR_1.4 base64enc_0.1-3 evaluate_0.10 rmarkdown_1.1
[22] stringi_1.1.2 scales_0.4.1 XML_3.98-1.4 jsonlite_1.1 zoo_1.7-13
Using Rstudio 1.0.44 and these options:
As a feature (or bug), chunks in Notebooks do not run when you click 'Preview' but rather as you evaluate each chunk (in the course of writing the Notebook itself.
If you Run all code in the Notebook before Publishing, using the 'Run' button in the top right of the Source Pane, RStudio can then find the objects you want to embed.
Alternatively, you can click the green play (right arrow) in the top right of each code chunk in the Notebook to run each code chunk.
Run the code chunk using CTRL+SHIFT+Enter. If you now Preview the R Notebook, the code output will be available. The code must be executed in the editor before it gets reflected in the Preview.

cryptic error message in testthat

I am running testthat in Rstudio (Windows 0.98.1056), and I get the following error. It seems to have appeared after I updated Rstudio and the installed R packages, but I suspect the two may not be directly related. The error occurs also on a very simple test file. Has anyone encountered this?
file: tests/test-all.R
library(testthat)
library(matrixUtils)
test_package("matrixUtils")
file: tests/testthat/matrixUtils-test.R
context("testa")
test_that("subsetting by one dimension", {
a <-1
expect_equal(a, 1)
})
Error message:
==> Sourcing R files in 'tests' directory
Error: '\.' is an unrecognized escape in character string starting "'\." Execution halted
Exited with status 1.
sessionInfo() R version 3.1.1 (2014-07-10) Platform: x86_64-w64-mingw32/x64 (64-bit)
locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages: [1] grid stats graphics grDevices utils datasets methods base
other attached packages: [1] magrittr_1.1.0 matrixUtils_0.2 testthat_0.8.1 dplyr_0.2.0.99 reshape2_1.4 ggplot2_1.0.0 gridExtra_0.9.1 [8] zoo_1.7-11
loaded via a namespace (and not attached): [1] assertthat_0.1 colorspace_1.2-4 digest_0.6.4 gtable_0.1.2 lattice_0.20-29 MASS_7.3-34 munsell_0.4.2 [8] parallel_3.1.1 plyr_1.8.1 proto_0.3-10 Rcpp_0.11.2 scales_0.2.4 stringr_0.6.2 tools_3.1.1
>
It's hard to check your error without the package. Some ideas:
This was a similar error reported in #187 on github/hadley/testthat.
Update testthat package. Latest is 0.9.1.
Issue was closed but looks hard to reproduce. #hadley suggests a fix
check that devtools is enabled under project options in RStudio
renkun-ken added library(devtools) as a last resort in his Rprofile.
I start RStudio without .Rprofile which runs library(devtools). Test from Test package does not work, then I run devtools::test() as you suggest, and it works.
If this does not work. Check your paths and use file.path() to create paths using strings in your package. This error seems to crop up with paths misspecified.
Hope this helps gappy and you get the "woot" happy test messages soon!
M

Exporting Arabic Text from R

I'm trying to export a data frame with Arabic text in R.
When R imports Arabic text it converts it to UTF-8 codes. Like this:
<U+0627><U+0644><U+0641><U+0631><U+0639> <U+0627><U+0644><U+062A><U+0634><U+0631><U+064A><U+0639><U+064A><U+060C> <U+0627><U+0644><U+0641><U+0631><U+0639> <U+0627><U+0644><U+062A><U+0646><U+0641><U+064A><U+0630><U+064A><U+060C><U+0627><U+0644><U+0641><U+0631><U+0639> <U+0627><U+0644><U+0642><U+0636><U+0627><U+0626><U+064A>. <U+0627><U+0644><U+062D><U+0643><U+0648><U+0645><U+0629> <U+0627><U+0644><U+0641><U+062F><U+0631><U+0627><U+0644><U+064A>
Unfortunately, I can't get it to turn back into readable Arabic when exporting. Below is code I'm using...
write.csv(my.data,"data.csv", fileEncoding='UTF-8')
Anybody have a solution?
Also, here is my session info.
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-2 dichromat_2.0-0 digest_0.6.3 grid_3.0.1 gtable_0.1.2
[6] labeling_0.2 MASS_7.3-27 munsell_0.4.2 plyr_1.8 proto_0.3-10
[11] RColorBrewer_1.0-5 reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 tools_3.0.1
This code worked with me so I am sharing it:
Sys.setlocale("LC_CTYPE", "arabic" )
write.csv(group$message, file = 'posts.txt', fileEncoding = "UTF-8")
If you save the file as csv it will not work. You have to save it as txt.
You'll have to install and use locales. It's difficult and sometimes doesn't work.
There's some solutions and code offered here: Writing data isn't preserving encoding
Keep in mind that you actually HAVE to install language packs for your operating system and for some Windows versions there aren't any available separately at all.

Resources