scs*_*217 1 r r-markdown shiny dplyr
我正在尝试使用dpdownr的rmarkdown v2输入选择来选择要报告的某些列.然后,将在给定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()))
```
Run Code Online (Sandbox Code Playgroud)
Error: non-numeric argument to mathematical function在调用renderDataTable和renderText之后,我得到了输出.
任何人都可以帮我弄清楚这里有什么问题吗?
更新
基于@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)
})
```
Run Code Online (Sandbox Code Playgroud)
我认为问题在于这一行:
test %>% select(A, input$sample.choice)
Run Code Online (Sandbox Code Playgroud)
最终解决这样的事情:
test %>% select(A, "V")
Run Code Online (Sandbox Code Playgroud)
但是dplyr需要这个,因为它使用非标准评估:
test %>% select(A, V)
Run Code Online (Sandbox Code Playgroud)
这是一种可能的解决方法:
test %>% select(A, matches(input$sample.choice))
Run Code Online (Sandbox Code Playgroud)