我能够创建一个采用数据框和变量名的函数。例如:
df <- data.frame(x1=rnorm(100, 1, 1),
y=runif(100,0,10))
test <- function(data, variable){
arguments <- as.list(match.call())
x <- as.character(arguments$variable)
t <- sum(data[,x])
return(t)
}
# This works
test(df, x1)
但同样的功能不起作用。例如:
---
title: "BUG"
runtime: shiny
output: html_document
---
# SET UP
```{r}
df <- data.frame(x1=rnorm(100, 1, 1),
y=runif(100,0,10))
test <- function(data, variable){
arguments <- as.list(match.call())
x <- as.character(arguments$variable)
t <- sum(data[,x])
return(t)
}
```
# THIS DOES NOT WORK
```{r}
selectInput("variable", "Variable:",
colnames(df))
test(df, input$variable)
```
我做错了什么?
此示例显示了ggplot2
---
title: "ggplot2 works"
runtime: shiny
output: html_document
---
# This works
```{r}
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point()
```
# This also works
```{r}
library(shiny)
inputPanel(
selectInput("x", "x:", c("wt", "mpg")),
selectInput("y", "y:", c("mpg", "wt"))
)
environment<-environment()
myplot <- ggplot(mtcars, aes(get(input$x), get(input$y)), environment = environment) + geom_point()
renderPlot({
myplot
})
```