Issue in printing code in R taken from python - r

I am trying to print the python string in R with the below code, but not able to . Can anyone please help
py_run_string("print(Hello)")

You are missing quotation marks around Hello
reticulate::py_run_string("print('Hello')")

Related

Problems with unicode in R

I am having problems with printing unicode characters in R. Initially the problems started with me trying to plot some custom labels with ggplot, but I have found out that this problem runs deeper.
For example, the letter đ is represented with unicode code U+0100. This means that if I type
"\u0100"
in the console, I should get đ printed as output, right? However, this is not what happens. Instead, as the output I get:
<U+0100>
I don't understand why this is happening since my encoding is set to UTF-8. Does anyone have any ideas on how to solve this?
I tried using the following function:
stri_escape_unicode("<U+0100>")
but I just get the same output as before. Any help would be appreciated! I am using Macbook Pro from 2013.

Trying to display regression outputs in Rmarkdown in Latex

Hi all!
I'm currently working on a paper for my university. The task is to run some regressions on a dataset and compile all results in an rmarkdown file. It should be noted that this is my first time working with R, so if the following question is really stupid, I apologize in advance. ;-)
My problem relates to the display of the regression outputs in Rmarkdown. I want to have them in Latex but somehow it won't work.
I tried to replicate this code https://www.r-statistics.com/tag/stargazer/ but when I enter it I get the following output (this happens both in the console and in Rmarkdown):
When I manually enter Latex in the Rmarkdown document with the $ notation, for instance to include formulas, it works just fine so I'm a bit puzzled.
I feel like I'm missing something very obvious but I can't quite put my finger on it. In any event, any help would be greatly appreciated!

Using str_extract_all to extract only first two words in R?

I am stuck with a problem which should be simple. Likely a regex issue. I am a newbie. Please consider a string vector such as the one:
species_location<-c('Homo_sapiens_Lausanne_Switzerland', 'Solenopsis_invicta_California_US', 'Rattus_novaborensis_Copenhagen_Denmark', 'Candida_albicans_Crotch_Home')
I wanted to end up with a new vector for species that looks like:
c(Homo_sapiens, Solenopsis_invicta, Rattus_novaborensis, Candida_albicans)
Currently I am using the following function:
str_extract_all(species_location,'^(\\S+?)_(\\S+?)_')
However it returns the first 3 words instead of what I designed it for. I cannot figure out why. Please could anyone help and explain? Thanks
UPDATE:
For anyone passing by, the code entered above works as it should, excepting on my R Console for Mac OS 3.0.0, R.app 1.60. I still do not know what is the issue there but it might be interest someone else to check. Will try and add a picture here.
Just relying on the stringr package.
library(stringr)
species_location<-c('Homo_sapiens_Lausanne_Switzerland', 'Solenopsis_invicta_California_US', 'Rattus_novaborensis_Copenhagen_Denmark', 'Candida_albicans_Crotch_Home')
word(species_location, 1,2, sep="_")
We can use str_extract
str_extract(species_location, "[^_]+_[^_]+")

r['spanFlexTable'](tab1,i=1,from=2,to=3) turn out to be an error

I want to use python's packages rpy2 to get a FlexTable and merge cells, the code is:
r['spanFlexTable'](tab1,i=1,from=2,to=3)
But from is a key word in python, can someone help me to solve this?
In this post the solution of postfixing an underscore (from_) may work:
Is it possible to escape a reserved word in Python?

invalid multibyte character crashes when script is loaded from source (umlauts / special characters)

EDIT:
Thx to suggestions from the mailing list I realized that the problem I got has nothing to do with Sweave or Latex. It´s some Mac OS X related issue. Whenever I run my script by selecting all and sending it to R it works.
When I use
source("myplainRcode.R")
i get the error message stated below
finally I got sweave working together with ggplot2 on my Mac OS X. I invoke Sweave inside R with
Sweave("myfile.Rnw")
which creates the desired latex output. Now that the basic tests work, I try to source my real world file and it crashes at the following line:
gl_bybranch = ddply(new_wans,.(period,Branchen),
function(X)data.frame(Geschäftslage=mean(X$sentiment)))
I guess it has either to do with the ".(period...)" or the "ä" . Unfortunately I can't change these labels because they are also used in legends. So, somewhere in my code these ugly umlauts will appear. Is there a way to escape them in Sweave? I can't believe that this is problem since Sweave is written by a German who probably have second most umlaut characters (behind Turkey).
The error message I get is: invalid multibyte character in Parser on line 195
Thx for any ideas in advance!
YAY ! i got it. Sorry for the noise everybody. I switched all three files (.Rnw, mysource.R , invokeSweave.R) to UTF-8 it finally worked. So everybody who works with Komodo on a Mac make sure to change your default encoding to UTF-8 !

Resources