我正在使用以下代码来倾斜Shiny R,当我运行此代码时,它给了我这个错误:
警告:历史记录错误。默认值:“ x”必须为数字 [没有可用的堆栈跟踪]
library(shiny)
ui <- fluidPage(
selectInput("Ind","Indipendent Variable",choices = names(mtcars)),
selectInput('Dep',' Dependent Variable',choices = names(mtcars)),
plotOutput("BoxPlot"),
plotOutput('Hist'))
server <- function(input, output, session) {
data1 <- reactive({input$Ind})
data2 <- reactive({input$Dep})
output$BoxPlot <- renderPlot({boxplot(get(data2()) ~ get(data1()) , data=mtcars)})
output$Hist <- renderPlot({hist(get(data1())})
}
shinyApp(ui, server)
有什么帮助,为什么会这样说?
答案 0 :(得分:4)
尽量不要将所有内容放在一行中,因为它不会提高可读性,可以根据需要使用Google's R Style Guide。要回答问题,您可以通过[[]]
来访问变量,如下所示:
library(shiny)
ui <- fluidPage(
selectInput("Ind","Indipendent Variable",choices = names(mtcars)),
selectInput('Dep',' Dependent Variable',choices = names(mtcars)),
plotOutput("BoxPlot"),
plotOutput('Hist')
)
server <- function(input, output, session) {
data1 <- reactive({
input$Ind
})
data2 <- reactive({
input$Dep
})
output$BoxPlot <- renderPlot({
boxplot(get(data2()) ~ get(data1()) , data=mtcars)
})
output$Hist <- renderPlot({
req(data1())
hist(mtcars[[data1()]])
})
}
shinyApp(ui, server)