2017-02-03 102 views
0

我有一個簡單的Shiny應用程序繪製一些數據。該應用程序運行一個函數,接受5個參數並繪製結果。將字符串變量傳遞給Shiny中的過濾器

4個參數通過滑塊傳遞,它們都可以正常工作,但第5個參數是文本字符串。從我的UI

snipit:

selectInput("subsec", "Subsections", 
         c("Pies", "Milk", "Salad", "Bread"), 
         selected = TRUE, multiple = FALSE, 
         selectize=FALSE) 

其中 'subsec' 是我想傳遞給我的函數變量。從我的服務器

snipit:

Price_Score(input$s_ranks[1], input$s_ranks[2], 
input$s_index[1], input$s_index[2], input$subsec[1]) 

這似乎並沒有工作,沒有什麼陰謀,但如果我手動輸入的文本字符串如下正常工作

Price_Score(input$s_ranks[1], input$s_ranks[2], 
input$s_index[1], input$s_index[2], Milk) 

我怎樣才能把字符串傳過來?

我一個R函數中我做的改變變量的情況下,以小寫

sect <- tolower(deparse(substitute(sect))) 

,一些描述的下拉有空格他們也如太空侵略者。我如何將一個帶空格的字符串傳遞給我的函數?

在我的函數中,我有一些代碼基於4個數值參數操縱數據,第5個參數是一個字符串,過濾數據表的情節。繪製圖表的代碼如下:

 plot <- (ggplot(data, aes(rank, Move_Curve)) + 
     geom_line(size = 2, color = "blue") + 
     scale_y_continuous(breaks = seq(0, x_axis_max + x_axis_incriment, x_axis_incriment)) + 
     scale_x_continuous(breaks = seq(0, 20000 + 2000, 500)) + 
     geom_point(data = data[section_lower == sect 
           & `Price Index` > 0, .(rank, `Price Index`)], aes(y = `Price Index`)) + 
     labs(title = "Price Score Optimisation", x = "Product Rank", y = "Optimal Index") 
    ) 

    return(plot) 

在「節」變量是我想傳遞給函數,以便能夠篩選數據表的刺痛。

回答

0

這是通過串用空格一個選項:

selectInput("subsec", "Subsections", 
choices = list("Space invaders" = "space_invaders", "Pies" = "pies")) 

嘗試從input$subset刪除[1]獲得第一位的工作。

0

這MVE正常工作對我來說,預期:

library(shiny) 

ui <- fluidPage(
    selectInput(inputId = "subsec", "Subsections", 
       c("Pies", "Milk", "Salad", "Bread"), 
       selected = TRUE, multiple = FALSE, 
       selectize=FALSE), 
    textOutput("text") 
) 


server <- function(input, output) { 

    output$text <- renderText({ 
     sect <- tolower(input$subsec) 

     # Apply a function to the selected value 
     paste0('The answer is: ', sect) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

要注意的是:

  • 沒有必要使用subsec [1]選擇的值時
  • 無需deparse(substitute())
  • 在您提供的示例中,您沒有將字符串傳遞給Price_Score函數,而是傳遞對象Milk
+0

我可以打印選擇,因爲您已經顯示,但我想將值傳遞給一個函數來創建一個圖。這是我必須繪製的圖形 'output $ priceplot < - renderPlot({0}){輸入$ s_ranks [1],輸入$ s_ranks [2],輸入$ s_index [1],輸入$ s_index [2 ],輸入$ subsec) })' 我可以在屏幕上看到'subsec'值,但它沒有正確地將它傳遞給函數? – MidnightDataGeek

+0

運行時會出現什麼錯誤? – Pete900

+0

我會說這是'Price_Score'函數的一個問題,但不知道它是如何做的。 你說'Price_Score(輸入$ s_ranks [1],輸入$ s_ranks [2], 輸入$ s_index [1],輸入$ s_index [2],牛奶)'的作品。它是否打算通過「牛奶」而不是「牛奶」? – GGamba

0

很難建議任何東西沒有看到公式。包裝​​約input$subsec工作?

Price_Score(input$s_ranks[1], input$s_ranks[2], input$s_index[1], input$s_index[2], as.symbol(input$subsec)) 
相關問題