How to print R variables in middle of String
The problem is that R does not evaluate any expression withing the quotes - it is just printing the string you defined. There are multiple ways around it. For example, you can use the sprintf
function (untested because you do not provide a reproducible example):
cat(sprintf("<set name=\"%s\" value=\"%f\" ></set>\n", df$timeStamp, df$Price))
R variable inside a string
variable2 <- paste0("I am argument:",variable1,"text continue")
Insert variable within character string
It seems this is what you are after.
for (i in 1 : length(file.list)) {
file.name.variable <- file.list[i]
url <- paste0("https://accounts.profunds.com/etfdata/ByFund/",
file.name.variable, "-historical_nav.csv")
destfile <- paste0("C:/R Projects/Data/etf_aum/",
file.name.variable, "csv")
download.file(url, destfile, mode="wb")
}
printing dates in the middle of a string in R
Try this:
paste0("http://www.stevel.com/log/?xid=2275644&dd=", Sys.Date() - 6:0)
giving this character vector:
[1] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-15"
[2] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-16"
[3] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-17"
[4] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-18"
[5] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-19"
[6] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-20"
[7] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-21"
If you wanted to cat
them out on 7 successive lines of the R console rather than output a character vector then if v
is the character vector above then any of the following will do:
for(e in v) cat(e, "\n", sep = "")
cat(paste(paste0(v, "\n"), collapse = ""))
cat(paste(v, collapse = "\n"), "\n", sep = "")
or if its not a problem to have an extra space at the end of some lines then sep=""
in the first and third above could be omitted. Alternately the gsubfn package has a cat0
function which is the same same as cat
except the default sep
is ""
.
In this case the loop seems substantially clearer than the non-loop solutions below it.
Print values for multiple variables on the same line from within a for-loop
Try out cat
and sprintf
in your for loop.
eg.
cat(sprintf("\"%f\" \"%f\"\n", df$r, df$interest))
See here
Format print statement
This will get you partway there:
x <- c(aa1,bb,cc)
options(useFancyQuotes = FALSE)
cat(paste(dQuote(x), collapse=" "), "\n")
# "abcdefghijklmnopqrstuvwxyz" "10" "222"
I say "partway" because the code above doesn't take care of line-wrapping: if you hand it a very long vector of characters strings, they will all be cat'd to a single very long non-wrapping line...
Edited to show one method of wrapping the lines:
You can get line-wrapping by using a combination of strwrap()
(thanks @mnel) and gsub()
, :
xs <- c(x,x,x,x)
cat(gsub("\" \"", "\" \"",
strwrap(paste(dQuote(xs), collapse=" "), 70)), sep="\n")
# "abcdefghijklmnopqrstuvwxyz" "10" "222" "abcdefghijklmnopqrstuvwxyz"
# "10" "222" "abcdefghijklmnopqrstuvwxyz" "10" "222"
# "abcdefghijklmnopqrstuvwxyz" "10" "222"
(The call to gsub()
is needed if more than one space is needed between the vector elements, because strwrap()
destroys whitespace in the input.)
Print raw string from variable? (not getting the answers)
I had a similar problem and stumbled upon this question, and know thanks to Nick Olson-Harris' answer that the solution lies with changing the string.
Two ways of solving it:
Get the path you want using native python functions, e.g.:
test = os.getcwd() # In case the path in question is your current directory
print(repr(test))This makes it platform independent and it now works with
.encode
. If this is an option for you, it's the more elegant solution.If your string is not a path, define it in a way compatible with python strings, in this case by escaping your backslashes:
test = 'C:\\Windows\\Users\\alexb\\'
print(repr(test))
Related Topics
How to Run Lm Regression for Every Column in R
Plot Random Effects from Lmer (Lme4 Package) Using Qqmath or Dotplot: How to Make It Look Fancy
Row/Column Counter in 'Apply' Functions
"Factor Has New Levels" Error for Variable I'm Not Using
Check If Each Row of a Data Frame Is Contained in Another Data Frame
How to Plot a Contour Line Showing Where 95% of Values Fall Within, in R and in Ggplot2
Trying to Use Dplyr to Group_By and Apply Scale()
How to Get Pixel Data from an Image Using R
Data.Table Inner/Outer Join with Na in Join Column of Type Double Bug
How to Rotate Legend Symbols in Ggplot2
Do I Need to Normalize (Or Scale) Data for Randomforest (R Package)
Aesthetics Must Either Be Length One, or the Same Length as the Dataproblems
Implementation of Parallel Coordinates
Daily Time Series with Ts.. How to Specify Start and End
R Reshape a Vector into Multiple Columns
How to Conditionally Replace Values in R Data Frame Using If/Then Statement