2015-10-16 99 views
0
反應輸入

我現在用的是以下數據集:https://docs.google.com/spreadsheets/d/1C_P5xxzYr7HOkaZFfFiDhanqDSuSIrd2UkiC-6_G2q0/edit#gid=0麻煩與ShinyDashboard

我使用ShinyDashboard,我有一個selectInput,讓我選擇一個特定類型的直板(在糖果列在我的數據組)。

如何獲取Candy選項,然後製作包含每個購買月份所選糖果棒頻率的圖表?在我的server.R中,我不確定該CandyCount電抗元件中有什麼。

我的代碼如下:

## ui.R ## 
library(shinydashboard) 
library(rCharts) 


dashboardPage(
    dashboardHeader(title = "Dashboard"), 

    dashboardSidebar(
    width = 150, 
    sidebarMenu(
     menuItem("Dashboard", tabName = "dashboard", icon = icon("bar-chart")) 
    ) 
    ), 

    dashboardBody(
    sidebarPanel(
     htmlOutput("candy") 
    ), 
    mainPanel(
     showOutput("plot2", "polycharts") 
    ))) 

##server.R## 
server <- function(input, output, session) { 


    output$candy<- renderUI({ 
    selectInput(
     inputId = "candy", 
     label = "Candy: ", 
     choices = as.character(unique(dataset$Candy)), 
     selected = "Twix" 
    ) 
    }) 


    output$plot2 <- renderChart2({ 
    candySelect<- input$candy 
    df <- dataset[dataset$candy == candySelect,] 
    p2 <- rPlot(freq~purchase_month, data = df, type = 'line') 
    p2$guides(y = list(min = 0, title = "")) 
    p2$guides(y = list(title = "")) 
    p2$addParams(height = 300, dom = 'chart2') 
    return(p2) 
    }) 


    } 
+0

使用一個變量來存儲所選擇的糖果,'candyChosen < - input $ candy',然後通過選擇的糖果過濾您的'dataset'。 – tospig

+0

這些都應該發生在'reactive'元素的內部嗎? – Gary

+0

不一定。你可以在一個'renderPlot'函數裏面做,像'output $ candyPlot < - renderPlot({candyChosen < - input $ candy; })' – tospig

回答

1

如果您沒關係使用,你可以做ggplot這樣的事情:

編輯以具有動態提示

## ui.R ## 
library(shinydashboard) 
library(shinyBS) 
require(ggplot2) 

dataset <- read.csv("Sample Dataset - Sheet1.csv") 

ui <- dashboardPage(
    dashboardHeader(title = "Dashboard"), 

    dashboardSidebar(
    width = 150, 
    sidebarMenu(
     menuItem("Dashboard", tabName = "dashboard", icon = icon("bar-chart")) 
    ) 
), 

    dashboardBody(
    sidebarPanel(
     htmlOutput("candy") 
    ), 
    mainPanel(
     uiOutput("plotUI") 
    ) 
)) 

##server.R## 
server <- function(input, output, session) { 

    output$candy<- renderUI({ 
    selectInput(
     inputId = "candy", 
     label = "Candy: ", 
     choices = as.character(unique(dataset$Candy)), 
     selected = "Twix" 
    ) 
    }) 

    output$plotUI <- renderUI({ 
    if(is.null(input$candy)) return(NULL) 
    local({ 
     candySelect <- input$candy 
     str1 <- sprintf("The candybar you selected is: %s",candySelect) 
     str2 <- sprintf("More about %s <a>here</a>",candySelect) 
     print (str1) 
     popify(plotOutput('plot'),str1,str2) 
    }) 

    }) 

    observeEvent(input$candy,{ 
    if(is.null(input$candy)) return(NULL) 
    candySelect<- input$candy 
    print ('plot') 
    # Assuming only one entry for each mont per candybar 
    d <- dataset[dataset$Candy==candySelect,] 
    output$plot <- renderPlot({ 
     ggplot(data=d, aes(x=purchase_month,y=freq,group=Candy)) + 
     geom_line() + 
     ggtitle(candySelect) 
    }) 
    }) 

} 

shinyApp(ui = ui, server = server) 

我猜這應該工作,否則你可以使用jQuery綁定工具提示。

+0

完美工作。謝謝奧斯卡!我將如何添加一些交互式工具提示到這個ggplot? – Gary

+0

太棒了!我已經添加了一個動態生成的工具提示,希望能夠工作。 –

+0

@OskarForsmo它真的工作正常嗎?我只是嘗試在我的RStudio上,我看到的情況是每當'Popover'文本出現時,即當我改變Twix-> KitKat時,出現'Popover',然後當我更改KitKat - > Herskley時,出現'Popover' doest不顯示等 – Nicolabo