简体   繁体   English

如何用特殊字符创建一个字符串

[英]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 . 然后,我迷失了如何使用input_password和创建login_string I tried paste0 , which did not work - paste0("'{"identifier":"wallaby", "password": ""input_password,"}"') 我尝试了paste0 - paste0("'{"identifier":"wallaby", "password": ""input_password,"}"')

Any ideas on how to do this? 有关如何执行此操作的任何想法?

The idea is to separate raw text and variables by , in paste0 : 我们的想法是原始文本和变量的分离,paste0

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

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

We can make use of the jsonlite to create this. 我们可以利用jsonlite来创建它。 Create a named list and use toJSON to convert it to json format 创建一个命名list并使用toJSON将其转换为json格式

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM