I want to run a R script to run a simulation and cache the results for the Rmarkdown document. I am using Rstudio and try to create an HTML report by using knit HTML Here's a simple example.
```{r test_global_env,cache=TRUE}
print(getwd())
source("./test_script.R")
```
```{r test_global_env_2}
print(a)
```
and test_script.R is as follows
a<-1
When I change the cache option for the chunk to FALSE, print(a) works. If I set it to TRUE, it works the first time, the second time, I get object 'a' not found error.
A similar question is Can knit2pdf use the global environment? , but I could not figure out if it applies to my situation. Here is the sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
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
[6] methods base
other attached packages:
[1] knitr_1.2 igraph_0.6.5-2 lubridate_1.3.0
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.4.3 formatR_0.8
[4] memoise_0.1 plyr_1.8 stringr_0.6.2
[7] tools_3.0.0
Related
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).
When I run a complete chunk of code, inline output appears as expected. However if I run the same chunk one line at a time it breaks if I use more than one plot call. So:
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
works fine in an R script. If I insert it into a code chunk in a brand new R markdown file, like this:
---
title: "wtf"
author: "Drew Tyre"
date: "December 22, 2016"
output: html_document
---
```{r}
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
```
This chunk runs if I run the entire chunk using control-shift-enter, the play button, or by selecting all the lines and using control-enter. However, if I run the lines one at a time using control-enter, it breaks on the call to lines(x,y):
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
It goes away if I set the options to chunk output in console. I guess it could be the intended behavior, but then maybe control-enter should do the same thing as control-shift-enter when using inline output?
I'm using RStudio 1.0.44 and
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
[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
[6] methods base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.2 yaml_2.1.14
[4] knitr_1.15.1
I can also reproduce this behavior on this machine:
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
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
loaded via a namespace (and not attached):
[1] tools_3.2.3 knitr_1.12.3
Kevin Ushey over at the RStudio community forum had this answer:
This is unfortunately a consequence of the Notebook execution model. The graphics device state is reset after each execution, so code execution that is done line-by-line (e.g. to fill a plot) will not work as expected.
There are some workarounds for this:
Prefer executing a whole chunk at a time, using e.g. Cmd + Shift + Enter,
Wrap your plot generating code in brackets {},
Define a function that generates the entire plot,
Disable inline chunk output for that document. (Do this by clicking the Gear icon in the source toolbar and choosing 'Chunk Output in Console')
As a reproducible example, we can use the ui.R and server.R files from:
https://github.com/bcaffo/courses/tree/master/09_DevelopingDataProducts/shiny/inputApp
When I run runApp() in RStudio the internal RStudio viewer displays an interface like this:
which is good, but when I click "Open in Browser" the Chrome and Firefox versions looks like this:
What gives?
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
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
[6] methods base
other attached packages:
[1] shiny_0.10.2.1
loaded via a namespace (and not attached):
[1] digest_0.6.4 htmltools_0.2.6 httpuv_1.3.2
[4] mime_0.2 R6_2.0.1 Rcpp_0.11.3
[7] RJSONIO_1.3-0 tools_3.1.2 xtable_1.7-4
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.
I'm having an issue with R Studio crashing when I run the Crosstable function. First, I'm installing the following packages:
install.packages ("gmodels"); install.packages("MASS")
library (gmodels); library (MASS)
Then, I run the following code on my data frame:
CrossTable(dat$PopSize, dat$PCIncChange, fisher = TRUE, chisq = TRUE)
expected = TRUE, sresid = TRUE, format="SPSS")
R produces the table, but then does not allow me to execute any other code in the console. It also doesn't allow me to save my file or quit the program (I've had to use control alt delete every time).
-- Edit: Adding session info from comments --
sessionInfo():
R version 2.15.2 (2012-10-26)
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] splines stats graphics grDevices utils datasets methods base –
other attached packages: [1] MASS_7.3-23 gmodels_2.15.3 Hmisc_3.10-1 survival_2.36-14 stringr_0.6.2
loaded via a namespace (and not attached): [1] cluster_1.14.3 gdata_2.12.0 grid_2.15.2 gtools_2.7.0 lattice_0.20-10 tools_2.15.2
Any ideas?