0
請運行此代碼。當你點擊數據表中的一行時,你會看到「a」列中的數字被繪製出來。這是使用回調和observeEvent完成的。這工作正常。使用click.dt與滑塊進行過濾後,點擊不更新
library(shiny)
library(DT)
runApp(shinyApp(
ui = fluidPage(
DT::dataTableOutput('table') ,
selectInput("NUMBER", "Number:", c(1,100,1000000),selected = 1),
plotOutput("p")
),
server = function(input, output, session) {
NUMBER = reactive({
input$NUMBER
})
output$table <- DT::renderDataTable({
datatable(data.frame(a = c(85,7),b=c(444,2)), rownames = FALSE, selection = 'none', callback=
JS("table.on('click.dt', 'tr', function() {
var data=table.row(this).data();
Shiny.onInputChange('rows',data[0]);
});")
)}
)
observeEvent(input$rows, {
print(input$rows)
print(class(input$rows) )
a = as.numeric(input$rows)
print(a)
print(NUMBER())
new_number = a #THIS WORKS!!!
#new_number = a * as.numeric(NUMBER()) #THIS DOES NOT WORK multiple a by the number in the slider when the SLIDER Is CHANGED
output$p = renderPlot({
# browser()
plot(new_number)
})
})}
))
但是你也看到一個「NUMBER」滑塊輸入。當用戶更改滑塊時,我希望繪圖更新,以便繪圖顯示滑塊中所選數字的倍數。所以:
註釋掉該行:
new_number = a
並取消這一行:
new_number = a * as.numeric(NUMBER())
現在重新運行代碼,你會看到,當你從滑塊選擇不同的電話號碼,但沒有任何反應。這是因爲
new_number = a * as.numeric(NUMBER())
是在ObserveEvent函數內,並且表格沒有被點擊......只是滑塊被改變了。那麼如何提供NUMBER()以便它可以工作?
謝謝。
感謝,如果我有多個選擇輸入?說我有NUMBER1和類別應該怎麼observeEvent設立observeEvent(輸入$號,輸入$分類{........輸出$ P = renderplot()})? – user3022875