r语言 - RMarkdown V2闪亮的文件和文件



我试图使用一个输入选择标记下降v2与dplyr选择某些列要报告。然后,所选列将用于给nls()函数的公式中,以确定某个常数c的值。

我的代码如下:
---
title: "Test"
author: "Author"
date: "Wednesday, June 18, 2014"
output: html_document
runtime: shiny
---
```{r, echo=TRUE}
require(dplyr)
#Some sample random data in a data.frame:
test=data.frame(A=runif(n=10),V=runif(n=10),N=runif(n=10))
#Input box to choose desired column
inputPanel(
  selectInput('sample.choice', label = 'Choose Columns',
              choices = c('V Sample'='V',
                          'N Sample'='N'),
              selected='N')
)
#Make a reactive data.frame to be used with the nls function
test2=reactive(test%.%select(A,input$sample.choice))
#Display the data.frame
renderDataTable(test2())
#Use nls solver to determine a constant with a given formula:
c.fit=reactive(nls(A ~ I(C*input$sample.choice),data=test2(),start=list(C=1)))
renderText(summary(c.fit()))
```

在调用renderDataTable和renderText之后,我得到输出Error: non-numeric argument to mathematical function

有谁能帮我弄清楚这里出了什么问题吗?

基于@wch评论,我已经修复了dplyr select函数。我也试图调用renderText,但summary.nls的输出是类型列表。这将不起作用,我需要运行renderPrint代替。下面是工作代码:

```{r, echo=TRUE}
require(dplyr)
test=data.frame(A=runif(n=10),V=runif(n=10),N=runif(n=10))
inputPanel(
  selectInput('sample.choice', label = 'Choose Columns',
              choices = c('V Sample'='V',
                          'N Sample'='N'),
              selected='N')
)
renderText(input$sample.choice)
test2=reactive(test%.%select(A,matches(input$sample.choice)))
renderDataTable(test2())
renderPrint({
  data=test2()
  formula=paste0('A ~ I(C*',input$sample.choice,')')
  c.fit=nls(formula,data=data,start=list(C=1))
  summary(c.fit)
})
```

我认为问题出在这一行:

test %>% select(A, input$sample.choice)

最终解析为如下所示:

test %>% select(A, "V")

但是dplyr需要这个,因为它使用非标准的求值:

test %>% select(A, V)

这是一个可能的解决方案:

test %>% select(A, matches(input$sample.choice))

最新更新