cephalopod
cephalopod

Reputation: 1906

how to create a string with special characters

I'm trying to create the following variable:

login_string <- '{"identifier":"wallaby", "password": "sea_dragon5"}'

Now, instead of storing the password in the script, I'd like the user to input the password with the following command:

input_password <- rstudioapi::askForPassword()

Then, I'm lost on how to use input_password and create the login_string. I tried paste0, which did not work - paste0("'{"identifier":"wallaby", "password": ""input_password,"}"')

Any ideas on how to do this?

Upvotes: 1

Views: 61

Answers (2)

akrun
akrun

Reputation: 887251

We can make use of the jsonlite to create this. Create a named list and use toJSON to convert it to json format

library(jsonlite)
login_string <- toJSON(list(identifier = "wallaby", 
           password = input_password), auto_unbox = TRUE)
cat(login_string)
# {"identifier":"wallaby","password":"random_password"}

data

input_password <- "random_password"

Upvotes: 1

acylam
acylam

Reputation: 18681

The idea is to separate raw text and variables by , in paste0:

input_password <- "random_password"
login_string <- paste0('{"identifier":"wallaby", "password":"', input_password, '"}')

cat(login_string)
# {"identifier":"wallaby", "password":"random_password"}

Upvotes: 1

Related Questions