修改用户输入数据
Modifying user input data
library(shiny)
ui <- fluidPage(
checkboxGroupInput("data", "Select data:",
c("Iris" = "iris",
"Cars" = "mtcars")),
plotOutput("myPlot")
)
server <- function(input, output) {
dat <- reactive({
switch()
})
output$myPlot <- renderPlot({
dat <- switch(input$data,
"iris" = iris,
"mtcars" = mtcars)
plot(Sepal.Width ~ Sepal.Length, data = get(input$data))
})
}
shinyApp(ui, server)
上面是一个简单的应用程序,提示用户 select 数据集,然后使用数据集绘制图形。我想修改它,以便用户可以指定一些值,t
,它也被传递到 plot
语句中。
library(shiny)
ui <- fluidPage(
checkboxGroupInput("data", "Select data:",
c("Iris" = "iris",
"Cars" = "mtcars")),
sidebarLayout(
sidebarPanel(
sliderInput("t",
"Scale:",
min = -5,
max = 5,
value = 2, step = 1,
width = "100%")
),
mainPanel( plotOutput("myPlot"))
)
)
server <- function(input, output) {
dat <- reactive({
switch()
})
output$myPlot <- renderPlot({
t = input$t
dat <- switch(input$data,
"iris" = iris,
"mtcars" = mtcars)
plot(Sepal.Width ~ Sepal.Length * t, data = get(input$data))
})
}
shinyApp(ui, server)
但是,我收到以下错误语句:variable lengths differ (found for 't')
。我希望实现的是将 t
列附加到用户输入数据集。我试过 dat$t = t
但似乎没有用。
如果您在 formula
(Sepal.Width ~ Sepal.Length * t
) 中进行计算,您需要 use I() to bracket the portions of the formula where the operators are used in their arithmetic sense:
plot(Sepal.Width ~ I(Sepal.Length * t), data = get(input$data))
更新:如果你想对数据进行多种操作,我建议创建一个反应函数(dat
)来计算数据根据用户输入,然后使用 dat()
返回的 data.frame
对象作为绘图中的输入数据:
server <- function(input, output) {
dat <- reactive({
if (input$data == "iris") {
df <- iris
# Do any calculations based on user input (here input$t)
df$Sepal.Length <- df$Sepal.Length * input$t
}
df
})
output$myPlot <- renderPlot({
plot(Sepal.Width ~ Sepal.Length, data = dat())
})
}
为了跨多个数据集进行这项工作,让 dat() 函数重命名依赖项(Sepal.Width
在 iris
,其他在 mtcars
中)和独立项(Sepal.Length
in iris
) 例如y
和 x
,然后在您的绘图中使用 plot(y~x, ...)
(或为每个数据集编写单独的绘图函数)。
library(shiny)
ui <- fluidPage(
checkboxGroupInput("data", "Select data:",
c("Iris" = "iris",
"Cars" = "mtcars")),
plotOutput("myPlot")
)
server <- function(input, output) {
dat <- reactive({
switch()
})
output$myPlot <- renderPlot({
dat <- switch(input$data,
"iris" = iris,
"mtcars" = mtcars)
plot(Sepal.Width ~ Sepal.Length, data = get(input$data))
})
}
shinyApp(ui, server)
上面是一个简单的应用程序,提示用户 select 数据集,然后使用数据集绘制图形。我想修改它,以便用户可以指定一些值,t
,它也被传递到 plot
语句中。
library(shiny)
ui <- fluidPage(
checkboxGroupInput("data", "Select data:",
c("Iris" = "iris",
"Cars" = "mtcars")),
sidebarLayout(
sidebarPanel(
sliderInput("t",
"Scale:",
min = -5,
max = 5,
value = 2, step = 1,
width = "100%")
),
mainPanel( plotOutput("myPlot"))
)
)
server <- function(input, output) {
dat <- reactive({
switch()
})
output$myPlot <- renderPlot({
t = input$t
dat <- switch(input$data,
"iris" = iris,
"mtcars" = mtcars)
plot(Sepal.Width ~ Sepal.Length * t, data = get(input$data))
})
}
shinyApp(ui, server)
但是,我收到以下错误语句:variable lengths differ (found for 't')
。我希望实现的是将 t
列附加到用户输入数据集。我试过 dat$t = t
但似乎没有用。
如果您在 formula
(Sepal.Width ~ Sepal.Length * t
) 中进行计算,您需要 use I() to bracket the portions of the formula where the operators are used in their arithmetic sense:
plot(Sepal.Width ~ I(Sepal.Length * t), data = get(input$data))
更新:如果你想对数据进行多种操作,我建议创建一个反应函数(dat
)来计算数据根据用户输入,然后使用 dat()
返回的 data.frame
对象作为绘图中的输入数据:
server <- function(input, output) {
dat <- reactive({
if (input$data == "iris") {
df <- iris
# Do any calculations based on user input (here input$t)
df$Sepal.Length <- df$Sepal.Length * input$t
}
df
})
output$myPlot <- renderPlot({
plot(Sepal.Width ~ Sepal.Length, data = dat())
})
}
为了跨多个数据集进行这项工作,让 dat() 函数重命名依赖项(Sepal.Width
在 iris
,其他在 mtcars
中)和独立项(Sepal.Length
in iris
) 例如y
和 x
,然后在您的绘图中使用 plot(y~x, ...)
(或为每个数据集编写单独的绘图函数)。