2016-06-15 200 views
2

「在ExtractVars無效的模型公式」 這是我的閃亮的UI圖像: enter image description here錯誤閃亮

global.R代碼:

library(shiny) 
library(gsheet) 
library(ggvis) 
library(ggplot2) 

urlA <- "https://docs.google.com/spreadsheets/d/1lVVGg2iRLtOaizrkaJHtPCyEnQiKAnJPyNj1kSSPXvY/edit?usp=sharing" 
urlB <- "Another URL" 
urlC <- "Another URL" 
urlD <- "Another URL" 
urlE <- "Another URL" 

p_A <- gsheet2tbl(urlA) 
p_B <- gsheet2tbl(urlB) 
p_C <- gsheet2tbl(urlC) 
p_D <- gsheet2tbl(urlD) 
p_E <- gsheet2tbl(urlE) 

paintingList <- list(p_A, p_B, p_C, p_D, p_E) 


A_date <- split(p_A, format(as.Date(p_A$date), "%Y-%m")) 
B_date <- split(p_B, format(as.Date(p_B$date), "%Y-%m")) 
C_date <- split(p_C, format(as.Date(p_C$date), "%Y-%m")) 
D_date <- split(p_D, format(as.Date(p_D$date), "%Y-%m")) 
E_date <- split(p_E, format(as.Date(p_E$date), "%Y-%m")) 

server.R代碼:

shinyServer(function(input, output) { 
    productInput <- reactive({ 
    switch(input$painting, 
     "A" = p_A, 
     "B" = p_B, 
     "C" = p_C, 
     "D" = p_D, 
     "E" = p_E) 
}) 

output$paintDate <- renderUI({ 
    if (input$painting == "A"){ 
    selectInput("pdate", "Select a date", names(split(p_A, format(as.Date(p_A$date), "%Y-%m")))) 
} else if(input$painting == "B") { 
    selectInput("pdate", "Select a date", names(split(p_B, format(as.Date(p_B$date), "%Y-%m")))) 
} else if(input$painting == "C"){ 
    selectInput("pdate", "Select a date", names(split(p_C, format(as.Date(p_C$date), "%Y-%m")))) 
} else if(input$painting == "D"){ 
    selectInput("pdate", "Select a date", names(split(p_D, format(as.Date(p_D$date), "%Y-%m")))) 
} else if(input$painting == "E"){ 
    selectInput("pdate", "Select a date", names(split(p_E, format(as.Date(p_E$date), "%Y-%m")))) 
} else { 
    return() 
} 

}) 

output$paintplot <- renderPlot({ 
    validate(need(input$pdate, "")) 
    plot(as.formula(paste("date ~ ", input$pdate)), data = A_date) 
}) 

ui.R代碼:

shinyUI(fluidPage(
includeCSS("css/main.css"), 
navbarPage("Plot", 
      tabPanel("ggPlot", 
        sidebarLayout(
         sidebarPanel(
         #add choices 
         selectInput("painting", "Select a option:", 
            choices = c("A", "B", "C", "D", "E")), 
         uiOutput("paintDate") 
        ), 

         mainPanel(
          plotOutput("paintplot")) 
        ) 
        ) 
      )) 
)) 

錯誤消息:

警告:錯誤terms.formula:在ExtractVars

我想在一列和顯示情節選擇數據無效模型公式。

如何在劇情中顯示我的選擇?

參考例子:http://shiny.rstudio.com/gallery/unicode-characters.html

請幫助我。謝謝。

回答

1

錯誤消息是由您的公式爲您的情節造成的。我不確定你以後的真實情況,但在查看了你的數據之後,我改變了一點情節命令,並有一些合理的工作。

因此,這是新的服務器代碼:

server.R

shinyServer(function(input, output) { 
    productInput <- reactive({ 
    switch(input$painting, 
      "A" = p_A, 
      "B" = p_B, 
      "C" = p_C, 
      "D" = p_D, 
      "E" = p_E) 
    }) 

    output$paintDate <- renderUI({ 
    if (input$painting == "A"){ 
     selectInput("pdate", "Select a date", names(split(p_A, format(as.Date(p_A$date), "%Y-%m")))) 
    } else if(input$painting == "B") { 
     selectInput("pdate", "Select a date", names(split(p_B, format(as.Date(p_B$date), "%Y-%m")))) 
    } else if(input$painting == "C"){ 
     selectInput("pdate", "Select a date", names(split(p_C, format(as.Date(p_C$date), "%Y-%m")))) 
    } else if(input$painting == "D"){ 
     selectInput("pdate", "Select a date", names(split(p_D, format(as.Date(p_D$date), "%Y-%m")))) 
    } else if(input$painting == "E"){ 
     selectInput("pdate", "Select a date", names(split(p_E, format(as.Date(p_E$date), "%Y-%m")))) 
    } else { 
     return() 
    } 

    }) 

    output$paintplot <- renderPlot({ 
    validate(need(input$pdate, "")) 
    df <- data.frame(A_date[[input$pdate]]) 
    plot(as.Date(df$date),df$pop) 
    }) 

}) 

和取得該地塊:

enter image description here

比我改變了所有的urlB其他, urlCglobal.RurlA相同,以使事情發揮作用所以我可以測試它。

+0

謝謝。就這樣。但如何判斷其他數據呢? 'df < - data.frame(A_date [[input $ pdate]]'是固定變量 –

+0

嗨,邁克懷斯,我突然開悟了,感謝您的幫助。 –