Print String and Variable Contents on the Same Line in R

Print string and variable contents on the same line in R

You can use paste with print

print(paste0("Current working dir: ", wd))

or cat

cat("Current working dir: ", wd)

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

Print string and reactive object in the same line in shiny app

What jumps out to me is renderText is usually paired with verbatimTextOutput. If you want to use uiOutput you should pair it with renderUI in the server code.

That being said, if you are expecting it to display an h4 heading in the UI, you won't get what you expect. h4 returns a shiny.tag object. paste0 will coerce these to characters. You more likely want to create your text then use it to create the h4.

output$auth_output <- renderUI({
h4(paste0("Name:", res_auth$user))
})

How do I print numeric values with a string in R

Use sprintf

sprintf("I want to print this number: %i", number)
#[1] "I want to print this number: 64"

or paste0

paste0("I want to print this number: ", number)
#[1] "I want to print this number: 64"

Print R data frame one line per row

Your comment on another answer suggests that you're doing this specifically to write out to a file - this answer does that, but the display may look different in the console depending on whether you're using R or Rstudio.

According to ?options there is a hard limit of 10,000 character width for the console, which can only be increased by recompiling R. If this is enough for your use case then you can do:

oldoptions <- options(width=10000)
options("width") #check that it's been set
#$width
#[1] 10000

sink("test.txt") #append = TRUE might be useful for subsequent writes
print(a) #Print the very wide dataframe
sink()

options(oldoptions) # reset the changed options

Note that initially I thought that this wasn't working when I inspected the file in Notepad - that was because (my setup of) Notepad has a limit on line length itself, and then wraps onto the next line. Viewing with something more sophisticated (Notepad++) shows that it did work sccessfully.

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))

Printing repetetively on the same line in R

You have the answer, it's just looping too quickly for you to see. Try:

for (i in 1:10) {Sys.sleep(1); cat("\r",i)}

EDIT: Actually, this is very close to @Simon O'Hanlon's answer, but given the confusion in the comments and the fact that it isn't exactly the same, I'll leave it here.



Related Topics



Leave a reply



Submit