在RShiny Web应用程序的数据框列名称中添加特殊字符

时间:2018-07-21 23:16:59

标签: r dataframe

我正在读取温度和相对湿度。当我执行下面的命令时,它给我一个错误

    function myFunction_Varify() {
      myFunction_firstName() && myFunction_lastName() && myFunction_zipCode();
    }

    function myFunction_firstName() {
      vFirstName = document.getElementById("xFirstName").value;
      if (vFirstName == "") {
        document.forms['checkout_form'].elements['first_name'].focus();
        alert("No FIRST NAME entered");
        return false;
      }
      return true;
    }

    function myFunction_lastName() {
      vLastName = document.getElementById("xLastName").value;
      if (vLastName == "") {
        document.forms['checkout_form'].elements['last_name'].focus();
        alert("No LAST NAME entered");
        return false;
      }
      return true;
    }

    function myFunction_zipCode() {
      vZipCode = document.getElementById("xZipCode").value;
      if (vZipCode == "") {
        document.forms['checkout_form'].elements['zip'].focus();
        alert("No ZIP CODE entered");
        return false;
      }
      return true;
    }

如何将测量单位显示为数据框列标题的一部分而不会出现错误?即使我使用反向刻度data.frame(Temp(F)=temperature, Humidity(%)=humidity) ,我也得到Temp.F,但仍在R Shiny中使用此数据帧。有什么解决方案?我希望以R Shiny呈现的列显示Temp(F)。在运行时在R Shiny中

Temp(F)

数据框显示在UI中,并且以下行不执行    colnames(dfmine)<-“带有%和其他内容的怪异(列)名称”

1 个答案:

答案 0 :(得分:1)

如果用反引号将“列名”(不同于真实的R名称)括起来,则可以访问包含特殊字符的列,否则这些列将使解析器跳闸。这意味着您每次要引用该列名称时都需要使用那些反引号。但是,data.frame函数不允许您执行此操作,除非您明确地告诉它以避免对列名进行清理:

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> dfmine
   weird..column..name.with...s.and.other.stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10

无法设置正确的参数check.names。现在“正确”地进行操作:

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10,
                        check.names=FALSE)
> dfmine$`weird (column) name with %'s and other stuff`
 [1]  1  2  3  4  5  6  7  8  9 10
> dfmine
   weird (column) name with %'s and other stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10

这通常不是一个好的策略,通常还有其他方法可以完成可能导致您朝该策略方向发展的任何事情。达到这一点的另一种方法是分配一个有效的名称,然后使用普通字符值重新分配:

> dfmine <- data.frame(`weird (column) name with %'s and other stuff`= 1:10)
> colnames(dfmine) <- "weird (column) name with %'s and other stuff"
> dfmine
   weird (column) name with %'s and other stuff
1                                             1
2                                             2
3                                             3
4                                             4
5                                             5
6                                             6
7                                             7
8                                             8
9                                             9
10                                           10