how to create a quoted expression from strings

后端 未结 3 968
遥遥无期
遥遥无期 2021-01-31 12:27

Given a vector of strings, I would like to create an expression without the quotation marks.

# eg, I would like to go from 
c(\"string1\", \"string2\")

# to...         


        
3条回答
  •  独厮守ぢ
    2021-01-31 12:49

    I found these answers helpful but incomplete for using variables and multiple lines within the expression. To create a quoted expression from strings, with variables and multiple lines make use of quote(), atop() and subsititute():

      # Prepare variables
      samp_skewness = round(skewness(dv),2)
      samp_kurtosis = round(kurtosis(dv),2)
      samp_var = round(var(dv))
      samp_summ <- summary(dv)
      num_samples = length(dv)
    
      # Prepare quotes containing math annotations
      q1 = quote(paste(mu,"="))
      q2 = quote(paste(sigma,"="))
      q3 = quote(paste(gamma[1],"="))
      q4 = quote(paste(gamma[2],"="))
    
    # Use subsitition to construct the expression, passing in the variables and quoted expressions
      title = substitute(atop(paste("Top Title, # samples: ", ns), 
                paste(e1,v1,", ",e2,v2,", ",e3,v3,", ",e4,v4)),
                list(ns=num_samples,v1=round(samp_summ['Mean']),v2=samp_var,
                v3=samp_skewness,v4=samp_kurtosis,e1=q1,e2=q2,e3=q3,e4=q4))
    

    In ggplot: ...

    labs(title = title) +
    

    ...

提交回复
热议问题