小编And*_*rew的帖子

Make .gitignore会忽略除少数文件之外的所有内容

我知道.gitignore文件隐藏了Git版本控制中指定的文件.我有一个项目(LaTeX),它在运行时会生成许多额外的文件(.auth,.dvi,.pdf,日志等),但我不希望这些文件被跟踪.

我知道我可以(也许应该)这样做所有这些文件放在项目中的一个单独的子文件夹中,因为我可以忽略该文件夹.

但是,有没有可行的方法将输出文件保存在项目树的根目录中,并使用.gitignore忽略除了我用Git跟踪的文件之外的所有内容?就像是

# Ignore everything
*

# But not these files...
script.pl
template.latex
# etc...
Run Code Online (Sandbox Code Playgroud)

git gitignore

1531
推荐指数
22
解决办法
42万
查看次数

收集多组列

我有来自在线调查的数据,受访者经历了1-3次问题循环.调查软件(Qualtrics)记录在多列,也就是说这个数据,Q3.2的调查将在列Q3.2.1.,Q3.2.2.以及Q3.2.3.:

df <- data.frame(
  id = 1:10,
  time = as.Date('2009-01-01') + 0:9,
  Q3.2.1. = rnorm(10, 0, 1),
  Q3.2.2. = rnorm(10, 0, 1),
  Q3.2.3. = rnorm(10, 0, 1),
  Q3.3.1. = rnorm(10, 0, 1),
  Q3.3.2. = rnorm(10, 0, 1),
  Q3.3.3. = rnorm(10, 0, 1)
)

# Sample data

   id       time    Q3.2.1.     Q3.2.2.    Q3.2.3.     Q3.3.1.    Q3.3.2.     Q3.3.3.
1   1 2009-01-01 -0.2059165 -0.29177677 -0.7107192  1.52718069 -0.4484351 -1.21550600
2   2 2009-01-02 -0.1981136 -1.19813815  1.1750200 -0.40380049 -1.8376094  1.03588482
3   3 …
Run Code Online (Sandbox Code Playgroud)

r reshape dplyr qualtrics tidyr

97
推荐指数
5
解决办法
8万
查看次数

我什么时候应该在PHP类中声明变量?

我是OOP范例的新手,所以对这个问题可能有一个简单的解释......

您是否总是需要在类中声明公共对象范围的变量?例如:

<?php

class TestClass
{
    var $declaredVar;

    function __construct()
    {
        $this->declaredVar = "I am a declared variable.";
        $this->undeclaredVar = "I wasn't declared, but I still work.";
    }

    function display()
    {
        echo $this->declaredVar . "<br />";
        echo $this->undeclaredVar;
        echo "<br /><br />"; 
    }
}

$test = new TestClass;
$test->display();

$test->declaredVar = "The declared variable was changed.";
$test->undeclaredVar = "The undeclared variable was changed.";

$test->display();

?>
Run Code Online (Sandbox Code Playgroud)

在这段代码中,即使$declaredVar是唯一声明的变量,$undeclaredVar也是可访问和可用的 - 它似乎就好像我已将其声明为public.

如果未声明的类变量总是可以像这样访问,那么预先声明它们的重点是什么?

php oop

46
推荐指数
3
解决办法
7万
查看次数

在Apache 1.3中使用htaccess作为root用户的子目录

我正在尝试部署使用Jekyll生成的网站,并希望将该网站保留在我自己的服务器上的子文件夹中,以使所有内容更有条理.

本质上,我想使用/jekyll作为根的内容,除非在实际的Web根目录中存在类似名称的文件.因此,像/jekyll/sample-page/会显示为http://www.example.com/sample-page/,而像/other-folder/将显示为http://www.example.com/other-folder.

我的测试服务器运行Apache 2.2,以下.htaccess(改编自http://gist.github.com/97822)完美无缺:

RewriteEngine On

# Map http://www.example.com to /jekyll.
RewriteRule ^$ /jekyll/ [L]

# Map http://www.example.com/x to /jekyll/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/jekyll/
RewriteRule ^(.*)$ /jekyll/$1

# Add trailing slash to directories without them so DirectoryIndex works.
# This does not expose the internal URL.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$ …
Run Code Online (Sandbox Code Playgroud)

apache .htaccess subdirectory jekyll

28
推荐指数
3
解决办法
4万
查看次数

Ruby的液体模板引擎中的模数(或缺乏模数)

我正在Jekyll网站上工作,我正在尝试输出嵌套在行div中的三个列div.Liquid使用他们的cycle过滤器非常容易:

{% for p in site.categories.post %}
    {% cycle 'add rows': '<div class="row">', nil, nil %}
        <div class="column">
            <a href="{{ p.url }}">{{ p.title }}</a>
        </div>
    {% cycle 'close rows': nil, nil, '</div>' %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

但是,只有当有3个,6个,9个等帖子时,这才真正有用.当帖子的总数不是三的倍数时,<div class="row">永远不会关闭 - for循环在结束标记作为close rows循环的一部分输出之前结束.

在Ruby,PHP或任何其他语言中,我可以使用模数运算符轻松修复此问题,因此除了close rows循环我将输出</div>if site.categories.size % 3 == 0.但是,Liquid,因为它是一种安全的模板语言,不支持模数.

<div class="row">当帖子总数不是三的倍数时,我还能做些什么来正确关闭?

ruby cycle liquid jekyll

22
推荐指数
3
解决办法
4211
查看次数

如何计算R中给定分位数的概率?

使用R,计算采样分布中给定概率的分位数是微不足道的:

x <- rnorm(1000, mean=4, sd=2)
quantile(x, .9) # results in 6.705755
Run Code Online (Sandbox Code Playgroud)

但是,我找不到一种简单的方法来反演计算样本中给定分位数的概率x.我最接近的是使用pnorm()我在创建样本时使用的相同均值和标准偏差:

pnorm(5, mean=4, sd=2) # results in 0.6914625
Run Code Online (Sandbox Code Playgroud)

但是,因为这是从完全正态分布而不是样本计算概率x,所以它并不完全准确.

是否有一个函数基本上反过来 quantile()?什么基本上让我做同样的事情,pnorm()但带样品?像这样的东西:

backwards_quantile(x, 5)
Run Code Online (Sandbox Code Playgroud)

我找到了这个ecdf()函数,但是无法找到一种方法来使它产生单个概率而不是完整的方程对象.

r probability

15
推荐指数
1
解决办法
1万
查看次数

如何从Perl设置文件权限?

我正在编写一个生成Bash脚本的Perl脚本.我正在使用open()一种模式>将所有内容输出到一个新文件.标准材料:

open (FILEOUT, ">", "rename.sh") or die "Can't create rename.sh";
Run Code Online (Sandbox Code Playgroud)

得到的.sh,它说我可以添加一个文件是只读的,随着444的八进制值在的perldoc +>(open (FILEOUT, "+>", "rename.sh")),使新创建的文件读写,或666.

有没有办法使新文件可执行(755或其他任何东西)使用open()?如果没有,为新文件设置文件权限的最佳方法是什么?

perl

13
推荐指数
1
解决办法
3万
查看次数

在另一个字符串向量中查找字符串向量的匹配项

我正在尝试创建一个新闻文章数据框的子集,其中至少提到一组关键字或短语的一个元素.

# Sample data frame of articles
articles <- data.frame(id=c(1, 2, 3, 4), text=c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod", "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,", "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo", "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"))
articles$text <- as.character(articles$text)

# Sample vector of keywords or phrases
keywords <- as.character(c("elit", "tempor incididunt", "reprehenderit"))

#   id                                                                         text
# 1 …
Run Code Online (Sandbox Code Playgroud)

grep r string-matching grepl

13
推荐指数
1
解决办法
5698
查看次数

Markdown(带有strip_tags)是否足以阻止XSS攻击?

我正在开发一个Web应用程序,允许用户在目录中键入项目的简短描述.我在我的textareas中允许Markdown,因此用户可以进行一些HTML格式化.

我的文本清理功能会在将任何输入的文本插入数据库之前将其删除:

public function sanitizeText($string, $allowedTags = "") {
    $string = strip_tags($string, $allowedTags);

    if(get_magic_quotes_gpc()) {
        return mysql_real_escape_string(stripslashes($string));
    } else {
        return mysql_real_escape_string($string);
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我存储在数据库中的所有内容都是Markdown - 没有其他HTML,甚至"基本HTML"(如此处的SO)是允许的.

允许降价会出现任何安全威胁吗?降价可以是XSSed,即使它没有标签吗?

php mysql xss markdown

10
推荐指数
3
解决办法
2757
查看次数

如何在R Markdown中使用Cairo PNG

使用Cairo保存R图形有很多优点(例如,请参见此处)。例如,保存PDF时,cairo_pdf设备会正确嵌入自定义字体。

通过cairo_pdf基于ggplot的图形,使用图形设备非常容易ggsave()

library(ggplot2)

ugly_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Some data about cars") +
  theme_gray(base_family = "Papyrus")
ugly_plot

ggsave(ugly_plot, filename = "ugly_plot.pdf", 
       width = 4, height = 2.5, device = cairo_pdf)
Run Code Online (Sandbox Code Playgroud)

cairo_pdfR Markdown中的设备与knitr一起使用也很容易-添加dev: cairo_pdf到YAML前端:

---
title: "Cairo stuff"
output:
  pdf_document:
    dev: cairo_pdf
---

```{r make-ugly-plot, fig.width=4, fig.height=2.5}
library(ggplot2)

ugly_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() + …
Run Code Online (Sandbox Code Playgroud)

r cairo ggplot2 knitr r-markdown

10
推荐指数
1
解决办法
535
查看次数